Skip to content

Commit 17f036e

Browse files
committed
updating content
1 parent e29081e commit 17f036e

23 files changed

+741
-9
lines changed
410 KB
Binary file not shown.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from machine import Pin
2+
import ssd1306
3+
import utime
4+
5+
WIDTH = 128
6+
HEIGHT = 64
7+
8+
# default is data on GP7 and clock on GP6
9+
10+
SCL = machine.Pin(2)
11+
SDA = machine.Pin(3)
12+
RES = machine.Pin(4)
13+
DC = machine.Pin(5)
14+
CS = machine.Pin(6)
15+
16+
spi=machine.SPI(0, sck=SCL, mosi=SDA)
17+
print(spi)
18+
19+
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
20+
21+
oled.fill(0)
22+
oled.text("Dan McCreary", 0, 0, 1)
23+
oled.text("MicroPython Robt", 0, 10, 1)
24+
oled.text("264K RAM!", 0, 20, 1)
25+
oled.text("128X64 OLED", 0, 28, 1)
26+
oled.text("ssd1306 SPI", 0, 36, 1)
27+
oled.text("$4 + $17 = $21", 0, 45, 1)
28+
oled.text("Dan McCreary", 0, 54, 1)
29+
oled.show()
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from machine import Pin,PWM
2+
import VL53L0X
3+
from time import sleep
4+
import ssd1306
5+
from neopixel import NeoPixel
6+
7+
# OLED Display dimentions
8+
HEIGHT = 64
9+
WIDTH = 128
10+
MAX_TOF_VALUE = 1200
11+
12+
SCL = machine.Pin(2)
13+
SDA = machine.Pin(3)
14+
RES = machine.Pin(4)
15+
DC = machine.Pin(5)
16+
CS = machine.Pin(6)
17+
18+
spi=machine.SPI(0, sck=SCL, mosi=SDA)
19+
print(spi)
20+
21+
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
22+
23+
sda=machine.Pin(0) # row one on our standard Pico breadboard
24+
scl=machine.Pin(1) # row two on our standard Pico breadboard
25+
i2c=machine.I2C(0, sda=sda, scl=scl, freq=400000)
26+
print('i2c:', i2c)
27+
28+
tof = VL53L0X.VL53L0X(i2c)
29+
tof.start()
30+
31+
x=0
32+
def update_display(distance):
33+
global x
34+
print(x, distance)
35+
if distance > 63:
36+
distance = 63
37+
oled.pixel(x,HEIGHT - int(distance) - 1, 1)
38+
if x > WIDTH - 3:
39+
oled.scroll(-1,0)
40+
else:
41+
x += 1
42+
oled.show()
43+
44+
# time of flight calibration parameters
45+
zero_dist = 65 # distance measure when an object is about 1/2 cm away
46+
max_dist = 350 # max distance we are able to read
47+
scale_factor = .2
48+
49+
# get the normalized time-of-flight distance
50+
def get_distance():
51+
global zero_dist, scale_factor
52+
tof_distance = tof.read()
53+
if tof_distance > max_dist:
54+
return tof_distance
55+
# if our current time-of-flight distance is lower than our zero distance then reset the zero distance
56+
if tof_distance < zero_dist:
57+
zero_dist = tof_distance
58+
return int((tof_distance - zero_dist) * scale_factor)
59+
60+
# a list of our prior distance measurements for graphing mode
61+
distances=[]
62+
63+
valid_distance = 0
64+
mode = 0
65+
# loop forever
66+
def main():
67+
global mode, valid_distance
68+
while True:
69+
distance = get_distance()
70+
if distance < MAX_TOF_VALUE:
71+
update_display(distance)
72+
sleep(.05)
73+
74+
75+
# This allows us to stop the sound by doing a Stop or Control-C which is a keyboard intrrupt
76+
print('Running Chart Time of Flight')
77+
78+
try:
79+
main()
80+
except KeyboardInterrupt:
81+
print('Got interupt')
82+
finally:
83+
# Optional cleanup code
84+
print('Powering down sound')
85+
tof.stop()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from machine import Pin, PWM, ADC
2+
from utime import sleep
3+
4+
# lower right pins with USB on top
5+
FORWARD_PIN = 19
6+
REVERSE_PIN = 18
7+
8+
# ADC0 is GPIO 26. Connect to row 10 the right side.
9+
pot = ADC(26)
10+
11+
forward = PWM(Pin(FORWARD_PIN))
12+
reverse = PWM(Pin(REVERSE_PIN))
13+
14+
POWER_LEVEL = 65025
15+
16+
def main():
17+
while True:
18+
pot_value = pot.read_u16()
19+
print('pot val', pot_value)
20+
forward.duty_u16(pot_value)
21+
sleep(.1)
22+
23+
try:
24+
main()
25+
except KeyboardInterrupt:
26+
print('Got ctrl-c')
27+
finally:
28+
# Cleanup code
29+
print('Cleaning up')
30+
print('Powering motor now.')
31+
forward.duty_u16(0)
32+
reverse.duty_u16(0)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from machine import Pin, Timer, PWM, ADC
2+
from utime import sleep
3+
import ssd1306
4+
5+
# this is the built-in LED on the Pico
6+
led = Pin('LED', Pin.OUT)
7+
8+
# ADC0 is GPIO 26. Connect to row 10 the right side.
9+
pot = ADC(26)
10+
11+
def read_pot_u8():
12+
return pot.read_u16() >> 8
13+
14+
counter = 0
15+
while True:
16+
pot_value = pot.read_u16() # read the 8 bit value from the pot
17+
print(pot_value)
18+
# print("pot_value:", delay)
19+
high_bits = pot_value >> 9
20+
led.toggle()
21+
sleep(.1)
22+
counter += 1

src/kits/motor-pot/01-blink.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Setup - run once
2+
from machine import Pin # Get the Pin function from the machine module.
3+
from time import sleep # Get the sleep library from the time module.
4+
5+
# This is the built-in green LED on the Pico.
6+
BUILT_IN_LED_PIN = 25
7+
# change this to the following named pin on the "W"
8+
# BUILT_IN_LED_PIN = Pin("LED", Pin.OUT)
9+
10+
# The line below indicates we are configuring this as an output (not input)
11+
led = machine.Pin(BUILT_IN_LED_PIN, machine.Pin.OUT)
12+
13+
# Main loop: Repeat the forever...
14+
while True:
15+
led.high() # turn on the LED
16+
sleep(0.2) # leave it on for 1/2 second
17+
led.low() # Turn off the LED
18+
sleep(0.2) # leave it off for 1/2 second
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from machine import ADC, Pin
2+
from utime import sleep
3+
4+
# this is the built-in LED on the Pico
5+
led = Pin(25, Pin.OUT)
6+
7+
# ADC0 is GPIO 26. Connect to row 10 the right side
8+
pot = ADC(26)
9+
10+
MAX_DELAY = .5 # seconds
11+
12+
# global variables
13+
delay = 0
14+
15+
# repeat forever
16+
while True:
17+
pot_value = pot.read_u16() # read the value from the pot
18+
delay = pot_value/65025 * MAX_DELAY
19+
print("delay:", delay)
20+
if delay > 0:
21+
print("frequency (toggles per second):", 1/delay)
22+
led.high() # turn on the LED
23+
sleep(delay) # leave it on for 1/2 second
24+
led.low() # Turn off the LED
25+
sleep(delay) # leave it off for 1/2 second
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from machine import PWM, Pin
2+
from utime import sleep
3+
4+
# Motor Setup
5+
MAX = 65025
6+
# lower right pins with USB on top
7+
FOR_PIN = 16
8+
REV_PIN = 17
9+
10+
11+
forward = PWM(Pin(FOR_PIN))
12+
reverse = PWM(Pin(REV_PIN))
13+
forward.freq(50)
14+
# turn off the reverse motor
15+
reverse.duty_u16(0)
16+
17+
while True:
18+
print('forward')
19+
forward.duty_u16(MAX)
20+
sleep(2)
21+
22+
print('stop')
23+
forward.duty_u16(0)
24+
sleep(2)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from machine import PWM, Pin
2+
from utime import sleep
3+
4+
# Motor Setup
5+
# motors just barely turn at this power level
6+
MAX = 65025
7+
# lower right pins with USB on top
8+
FOR_PIN = 16
9+
REV_PIN = 17
10+
11+
12+
forward = PWM(Pin(FOR_PIN))
13+
reverse = PWM(Pin(REV_PIN))
14+
forward.freq(50)
15+
reverse.freq(50)
16+
17+
while True:
18+
print('forward')
19+
forward.duty_u16(MAX)
20+
reverse.duty_u16(0)
21+
sleep(2)
22+
23+
print('stop')
24+
forward.duty_u16(0)
25+
sleep(2)
26+
27+
print('reverse')
28+
reverse.duty_u16(MAX)
29+
sleep(2)
30+
31+
print('stop')
32+
reverse.duty_u16(0)
33+
sleep(2)
34+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from machine import PWM, Pin, ADC
2+
from utime import sleep
3+
4+
# this is the built-in LED on the Pico
5+
led = Pin(25, Pin.OUT)
6+
7+
# ADC0 is GPIO 26. Connect to row 10 the right side
8+
pot = ADC(26)
9+
10+
# Motor Setup
11+
# motors just barely turn at this power level
12+
MAX = 65025
13+
# lower right pins with USB on top
14+
FOR_PIN = 16
15+
REV_PIN = 17
16+
17+
18+
forward = PWM(Pin(FOR_PIN))
19+
reverse = PWM(Pin(REV_PIN))
20+
forward.freq(50)
21+
reverse.freq(50)
22+
23+
while True:
24+
pot_value = pot.read_u16()
25+
print('pot value:', pot_value)
26+
forward.duty_u16(pot_value)
27+
reverse.duty_u16(0)
28+
sleep(.1)
29+

0 commit comments

Comments
 (0)