Skip to content

Commit dc42b2b

Browse files
committed
updating content
1 parent de5248c commit dc42b2b

File tree

5 files changed

+337
-0
lines changed

5 files changed

+337
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import machine
2+
3+
sda=machine.Pin(0) # row one on our standard Pico breadboard
4+
scl=machine.Pin(1) # row two on our standard Pico breadboard
5+
i2c=machine.I2C(0, sda=sda, scl=scl)
6+
7+
print('i2c result:', i2c)
8+
address = i2c.scan()
9+
print('Scan result:', address)
10+
11+
decimal = address[0]
12+
hex = hex(decimal)
13+
14+
print('The VL53L0X has expected values of decimal 41 and hex 0x29')
15+
print("Device found at decimal: ", i2c.scan(), decimal, hex)
16+
17+
if (decimal == 41):
18+
print('PASS')
19+
else:
20+
print('FAIL')
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# VL53L0X Display Test
2+
3+
from machine import ADC, I2C, Pin, PWM
4+
import VL53L0X
5+
import ssd1306
6+
from utime import sleep
7+
8+
WIDTH = 128
9+
HEIGHT = 64
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+
sda=machine.Pin(0) # row one on our standard Pico breadboard
22+
scl=machine.Pin(1) # row two on our standard Pico breadboard
23+
i2c=machine.I2C(0, sda=sda, scl=scl, freq=400000)
24+
print('i2c:', i2c)
25+
26+
tof = VL53L0X.VL53L0X(i2c)
27+
TOF_MAX_VALUE = 200 # dist in cm anything beyond this we will ignore
28+
TOF_MIN_OFFSET = 35 # subtract from raw sensor
29+
TOF_SCALE = 0.5 # scale the result
30+
31+
pot = ADC(26)
32+
33+
# returns a value from 0 to 127
34+
def read_pot_128():
35+
return pot.read_u16() >> 9
36+
37+
# map a value from one rante into another range
38+
# checks for divide by zero
39+
def map(value, istart, istop, ostart, ostop):
40+
# check ff (istop - istart)
41+
if (istop - istart) == 0:
42+
return ostop
43+
else:
44+
return int(ostart + (ostop - ostart) * ((value - istart) / (istop - istart)))
45+
46+
def vline_dash(x, y, length, dash_length, color):
47+
number_dashes = length // dash_length
48+
for i in range(number_dashes):
49+
oled.vline(x, y + i*dash_length, dash_length // 2, color)
50+
51+
def update_display(dist, potValue):
52+
oled.fill(0)
53+
if dist < TOF_MAX_VALUE:
54+
print('dist:', dist)
55+
oled.text('Pot:'+ str(potValue), 0, 46, 1)
56+
oled.text('Distance:'+ str(round(dist))+'cm', 0, 56, 1)
57+
# convert to a scale of 0 to 127
58+
tofX = map(dist, 0, TOF_MAX_VALUE, 0, WIDTH)
59+
oled.vline(tofX, 0, HEIGHT-20, 1)
60+
else:
61+
print('out of range:', dist)
62+
oled.text('Out of range', 0, 0, 1)
63+
vline_dash(potValue, 0, HEIGHT-20, 7, 1)
64+
oled.show()
65+
66+
def dist_cm():
67+
tof_value = tof.read()
68+
# these constants need clibration
69+
dist_in_cm = (tof_value - TOF_MIN_OFFSET) * TOF_SCALE
70+
if dist_in_cm < 0:
71+
return 0
72+
elif dist_in_cm > TOF_MAX_VALUE:
73+
return TOF_MAX_VALUE
74+
else:
75+
return dist_in_cm
76+
77+
tof.start()
78+
while True:
79+
# get distace in cm
80+
dist = dist_cm()
81+
potValue = read_pot_128()
82+
update_display(dist, potValue)
83+
sleep(.1)
84+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# VL53L0X Display Test
2+
3+
from machine import ADC, I2C, Pin, PWM
4+
import VL53L0X
5+
import ssd1306
6+
from utime import sleep
7+
8+
# Display Setup
9+
WIDTH = 128
10+
HEIGHT = 64
11+
SCL = machine.Pin(2)
12+
SDA = machine.Pin(3)
13+
RES = machine.Pin(4)
14+
DC = machine.Pin(5)
15+
CS = machine.Pin(6)
16+
spi=machine.SPI(0, sck=SCL, mosi=SDA)
17+
# print(spi)
18+
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
19+
20+
# Tof Setup
21+
sda=machine.Pin(0) # row one on our standard Pico breadboard
22+
scl=machine.Pin(1) # row two on our standard Pico breadboard
23+
i2c=machine.I2C(0, sda=sda, scl=scl, freq=400000)
24+
# print('i2c:', i2c)
25+
tof = VL53L0X.VL53L0X(i2c)
26+
TOF_MAX_VALUE = 200 # dist in cm anything beyond this we will ignore
27+
TOF_MIN_OFFSET = 35 # subtract from raw sensor
28+
TOF_SCALE = 0.5 # scale the result
29+
30+
# ADC Setup
31+
pot = ADC(26)
32+
33+
# Motor Setup
34+
MAX_MOTOR = 65025
35+
# lower right pins with USB on top
36+
FOR_PIN = 18
37+
REV_PIN = 19
38+
39+
forward = PWM(Pin(FOR_PIN))
40+
reverse = PWM(Pin(REV_PIN))
41+
forward.freq(50)
42+
forward.freq(50)
43+
# turn off the both motors
44+
forward.duty_u16(0)
45+
reverse.duty_u16(0)
46+
47+
# returns a value from 0 to 127
48+
def read_pot_128():
49+
return pot.read_u16() >> 9
50+
51+
# map a value from one rante into another range
52+
# checks for divide by zero
53+
def map(value, istart, istop, ostart, ostop):
54+
# check ff (istop - istart)
55+
if (istop - istart) == 0:
56+
return ostop
57+
else:
58+
return int(ostart + (ostop - ostart) * ((value - istart) / (istop - istart)))
59+
60+
def vline_dash(x, y, length, dash_length, color):
61+
number_dashes = length // dash_length
62+
for i in range(number_dashes):
63+
oled.vline(x, y + i*dash_length, dash_length // 2, color)
64+
65+
def update_display(dist, potValue):
66+
oled.fill(0)
67+
if dist < TOF_MAX_VALUE:
68+
print('dist:', dist)
69+
#oled.text('Pot:'+ str(potValue), 0, 46, 1)
70+
#oled.text('Distance:'+ str(round(dist))+'cm', 0, 56, 1)
71+
tofX = map(dist, 0, TOF_MAX_VALUE, 0, WIDTH)
72+
oled.vline(tofX, 0, HEIGHT-10, 1)
73+
74+
if tofX > potValue:
75+
oled.text('Drive Forward!', 0, 56, 1)
76+
forward.duty_u16(MAX_MOTOR)
77+
reverse.duty_u16(0)
78+
else:
79+
oled.text('Backup!', 0, 56, 1)
80+
forward.duty_u16(0)
81+
reverse.duty_u16(MAX_MOTOR)
82+
# convert to a scale of 0 to 127
83+
84+
else:
85+
print('out of range:', dist)
86+
oled.text('Out of range', 0, 56, 1)
87+
# draw the vertical line x, y, length, pixels on, color
88+
vline_dash(potValue, 0, HEIGHT-8, 7, 1)
89+
oled.show()
90+
91+
def dist_cm():
92+
tof_value = tof.read()
93+
# these constants need clibration
94+
dist_in_cm = (tof_value - TOF_MIN_OFFSET) * TOF_SCALE
95+
if dist_in_cm < 0:
96+
return 0
97+
elif dist_in_cm > TOF_MAX_VALUE:
98+
return TOF_MAX_VALUE
99+
else:
100+
return dist_in_cm
101+
102+
tof.start()
103+
while True:
104+
# get distace in cm
105+
dist = dist_cm()
106+
potValue = read_pot_128()
107+
update_display(dist, potValue)
108+
sleep(.1)
109+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# VL53L0X Display Test
2+
3+
from machine import ADC, I2C, Pin, PWM
4+
import VL53L0X
5+
import ssd1306
6+
from utime import sleep
7+
8+
# Display Setup
9+
WIDTH = 128
10+
HEIGHT = 64
11+
SCL = machine.Pin(2)
12+
SDA = machine.Pin(3)
13+
RES = machine.Pin(4)
14+
DC = machine.Pin(5)
15+
CS = machine.Pin(6)
16+
spi=machine.SPI(0, sck=SCL, mosi=SDA)
17+
# print(spi)
18+
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
19+
20+
# Tof Setup
21+
sda=machine.Pin(0) # row one on our standard Pico breadboard
22+
scl=machine.Pin(1) # row two on our standard Pico breadboard
23+
i2c=machine.I2C(0, sda=sda, scl=scl, freq=400000)
24+
# print('i2c:', i2c)
25+
tof = VL53L0X.VL53L0X(i2c)
26+
TOF_MAX_VALUE = 200 # dist in cm anything beyond this we will ignore
27+
TOF_MIN_OFFSET = 35 # subtract from raw sensor
28+
TOF_SCALE = 0.5 # scale the result
29+
30+
# ADC Setup
31+
pot = ADC(26)
32+
33+
# Motor Setup
34+
MAX_MOTOR = 65025
35+
# lower right pins with USB on top
36+
FOR_PIN = 18
37+
REV_PIN = 19
38+
39+
forward = PWM(Pin(FOR_PIN))
40+
reverse = PWM(Pin(REV_PIN))
41+
forward.freq(50)
42+
forward.freq(50)
43+
# turn off the both motors
44+
forward.duty_u16(0)
45+
reverse.duty_u16(0)
46+
47+
# returns a value from 0 to 127
48+
def read_pot_128():
49+
return pot.read_u16() >> 9
50+
51+
# map a value from one rante into another range
52+
# checks for divide by zero
53+
def map(value, istart, istop, ostart, ostop):
54+
# check ff (istop - istart)
55+
if (istop - istart) == 0:
56+
return ostop
57+
else:
58+
return int(ostart + (ostop - ostart) * ((value - istart) / (istop - istart)))
59+
60+
def vline_dash(x, y, length, dash_length, color):
61+
number_dashes = length // dash_length
62+
for i in range(number_dashes):
63+
oled.vline(x, y + i*dash_length, dash_length // 2, color)
64+
65+
def update_display(dist, potValue):
66+
oled.fill(0)
67+
if dist < TOF_MAX_VALUE:
68+
print('dist:', dist)
69+
#oled.text('Pot:'+ str(potValue), 0, 46, 1)
70+
#oled.text('Distance:'+ str(round(dist))+'cm', 0, 56, 1)
71+
tofX = map(dist, 0, TOF_MAX_VALUE, 0, WIDTH)
72+
oled.vline(tofX, 0, HEIGHT-10, 1)
73+
74+
if tofX > potValue:
75+
oled.text('Drive Forward!', 0, 56, 1)
76+
forward.duty_u16(MAX_MOTOR)
77+
reverse.duty_u16(0)
78+
else:
79+
oled.text('Backup!', 0, 56, 1)
80+
forward.duty_u16(0)
81+
reverse.duty_u16(MAX_MOTOR)
82+
# convert to a scale of 0 to 127
83+
84+
else:
85+
print('out of range:', dist)
86+
oled.text('Out of range', 0, 56, 1)
87+
# draw the vertical line x, y, length, pixels on, color
88+
vline_dash(potValue, 0, HEIGHT-8, 7, 1)
89+
oled.show()
90+
91+
def dist_cm():
92+
tof_value = tof.read()
93+
# these constants need clibration
94+
dist_in_cm = (tof_value - TOF_MIN_OFFSET) * TOF_SCALE
95+
if dist_in_cm < 0:
96+
return 0
97+
elif dist_in_cm > TOF_MAX_VALUE:
98+
return TOF_MAX_VALUE
99+
else:
100+
return dist_in_cm
101+
102+
tof.start()
103+
while True:
104+
# get distace in cm
105+
dist = dist_cm()
106+
potValue = read_pot_128()
107+
update_display(dist, potValue)
108+
sleep(.1)
109+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from machine import Pin, PWM
2+
from time import sleep
3+
4+
# lower right pins with USB on top
5+
FORWARD_PIN = 18
6+
REVERSE_PIN = 19
7+
8+
forward = PWM(Pin(FORWARD_PIN))
9+
reverse = PWM(Pin(REVERSE_PIN))
10+
forward.freq(50)
11+
forward.freq(50)
12+
13+
forward.duty_u16(0)
14+
reverse.duty_u16(0)
15+
print('Motor Stopped')

0 commit comments

Comments
 (0)