Skip to content

Commit 885fbdc

Browse files
committed
updating content
1 parent 6031c14 commit 885fbdc

File tree

5 files changed

+640
-4
lines changed

5 files changed

+640
-4
lines changed

src/kits/h-bridge/21-stop-motors.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from machine import Pin, PWM
2+
from utime import sleep
3+
4+
MAX_POWER_LEVEL = 65025
5+
# lower right pins with USB on top
6+
FORWARD_PIN = 8
7+
REVERSE_PIN = 9
8+
9+
forward = PWM(Pin(FORWARD_PIN))
10+
reverse = PWM(Pin(REVERSE_PIN))
11+
12+
# set the frequencey to be 50 Khz
13+
forward.freq(50)
14+
reverse.freq(50)
15+
16+
# turn off the PWM
17+
reverse.duty_u16(0)
18+
forward.duty_u16(0)

src/kits/h-bridge/23-switch-test.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# swith status with no debounce logic
12
from machine import Pin
23

34
builtin_led = Pin(25)
@@ -17,6 +18,8 @@
1718
ll_val = 0
1819
ur_val = 0
1920
lr_val = 0
21+
22+
counter = 0
2023
while True:
2124
# only print on change in the button_presses value
2225

@@ -27,6 +30,7 @@
2730
else: print("Upper Right = Off")
2831
builtin_led.toggle()
2932
ul_val = new_ul_switch
33+
counter += 1
3034

3135
new_ll_switch = ll_switch.value()
3236
if ll_val != new_ll_switch:
@@ -35,6 +39,7 @@
3539
else: print("Lower Left = Off")
3640
builtin_led.toggle()
3741
ll_val = new_ll_switch
42+
counter += 1
3843

3944

4045
new_ur_switch = ur_switch.value()
@@ -44,11 +49,13 @@
4449
else: print("Upper Right = Off")
4550
builtin_led.toggle()
4651
ur_val = new_ur_switch
52+
counter += 1
4753

4854
new_lr_switch = lr_switch.value()
49-
if lr_val != new_ll_switch:
55+
if lr_val != new_lr_switch:
5056
if new_lr_switch == 0:
51-
print("Lower Left = On")
52-
else: print("Lower Left = Off")
57+
print("Lower Right = On")
58+
else: print("Lower Right = Off")
5359
builtin_led.toggle()
54-
ll_val = new_ll_switch
60+
lr_val = new_lr_switch
61+
counter += 1
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# swith status with no debounce logic
2+
from machine import Pin
3+
from utime import sleep, ticks_ms
4+
5+
builtin_led = Pin(25)
6+
# Upper left swith
7+
UL_PIN = 2
8+
LL_PIN = 3
9+
UR_PIN = 14
10+
LR_PIN = 13
11+
12+
ul_switch = Pin(UL_PIN, Pin.IN, Pin.PULL_UP)
13+
ll_switch = Pin(LL_PIN, Pin.IN, Pin.PULL_UP)
14+
ur_switch = Pin(UR_PIN, Pin.IN, Pin.PULL_UP)
15+
lr_switch = Pin(LR_PIN, Pin.IN, Pin.PULL_UP)
16+
17+
# get current values as global variables
18+
ul = ul_switch.value()
19+
ll = ll_switch.value()
20+
ur = ur_switch.value()
21+
lr = lr_switch.value()
22+
23+
# old values
24+
ul_old = 0
25+
ll_old = 0
26+
ur_old = 0
27+
lr_old = 0
28+
29+
last_time = 0
30+
switch_counter = 0
31+
# this function gets called every time the button is pressed
32+
def switch_changed_handler(pin):
33+
global last_time, switch_counter, ul, ll, ur, lr
34+
new_time = ticks_ms()
35+
# if it has been more that 1/5 of a second since the last event, we have a new event
36+
if (new_time - last_time) > 200:
37+
if 'GPIO2' in str(pin):
38+
if ul == 0:
39+
ul = 1
40+
else: ul = 0
41+
if 'GPIO3' in str(pin):
42+
if ll == 0:
43+
ll = 1
44+
else: ll = 0
45+
if 'GPIO14' in str(pin):
46+
if ur == 0:
47+
ur = 1
48+
else: ur = 0
49+
if 'GPIO13' in str(pin):
50+
if lr == 0:
51+
lr = 1
52+
else: lr = 0
53+
switch_counter +=1
54+
last_time = new_time
55+
56+
# now we register the handler function when the button is pressed
57+
# https://docs.micropython.org/en/latest/library/machine.Pin.html
58+
ul_switch.irq(trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), handler = switch_changed_handler)
59+
ll_switch.irq(trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), handler = switch_changed_handler)
60+
ur_switch.irq(trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), handler = switch_changed_handler)
61+
lr_switch.irq(trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), handler = switch_changed_handler)
62+
63+
old_switch_counter = 0
64+
counter = 0
65+
print('Startup:', ul, ll, ur, lr)
66+
while True:
67+
# only print on change in the button_presses value
68+
if old_switch_counter != switch_counter:
69+
# print('Switch counter: ', switch_counter)
70+
old_switch_counter = switch_counter
71+
72+
if ul != ul_old:
73+
if ul == 0:
74+
print("Upper Left = On")
75+
else: print("Upper Left = Off")
76+
builtin_led.toggle()
77+
ul_old = ul
78+
counter += 1
79+
80+
if ll != ll_old:
81+
if ll == 0:
82+
print("Lower Left = On")
83+
else: print("Lower Left = Off")
84+
builtin_led.toggle()
85+
ll_old = ll
86+
counter += 1
87+
88+
if ur != ur_old:
89+
if ur == 0:
90+
print("Upper Right = On")
91+
else: print("Upper Right = Off")
92+
builtin_led.toggle()
93+
ur_old = ur
94+
counter += 1
95+
96+
if lr != lr_old:
97+
if lr == 0:
98+
print("Lower Right = On")
99+
else: print("Lower Right = Off")
100+
builtin_led.toggle()
101+
lr_old = lr

0 commit comments

Comments
 (0)