-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (34 loc) · 1.91 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import machine
import time
# set up a pin as footswitch input
footswitch = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
#set up a servo output
servo = machine.Pin(27) #assigns pin as servo signal
servo_pwm = machine.PWM(servo) #declares servo pin as Pulse Width Modulation
servo_pwm.freq(50)
#Setup the external LED as an output
LED = machine.Pin(16,machine.Pin.OUT)
#select duty_u16 values for 'foot_up' and 'foot_down' servo positions
foot_down = 4000 #4000/65535 = 6% duty cycle
foot_up = 6000 #9000/65535 = 9% duty cycle
#timing parameters (ms)
timeout=2500 #time foot will be lifted for if uninterupted by footswitch
#code ('IRQhandler') to run when main code interrupted by falling edge (footswitch release)
def footswitchIRQHandler_unload(pin):
if pin == footswitch:
start=time.ticks_ms()#start timer at point stimulation is started
print('foot up until timeout or footswitch loaded')
#while timer is less then specified timeout period and footswitch is unloaded
while (time.ticks_ms()-start)<timeout and footswitch.value()==0:
LED.value(1) #LED is on
servo_pwm.duty_u16(foot_up) #foot set to lifted position
time.sleep_ms(1) #this momentary sleep avoids the simulation slowing down in wokwi simulator
#assign the trigger and IRQhandler to run when loaded footswitch becomes unloaded
footswitch.irq(trigger = machine.Pin.IRQ_FALLING,
handler = footswitchIRQHandler_unload)
while True: #Main code which runs unless interrupted
LED.value(0) #LED is off
servo_pwm.duty_u16(foot_down) #foot set to dropped position
print('foot down') #print statements to communicate what's happening in the program
print('footswitch value:',footswitch.value(),"\n")
time.sleep(1) #cycles main loop at 1Hz