Skip to content
Snippets Groups Projects
Commit c8b0b2d0 authored by nikhil b lal's avatar nikhil b lal
Browse files

simplified code

parent 49efeeae
No related branches found
No related tags found
No related merge requests found
from machine import Pin, PWM, Timer
import time
from time import sleep, sleep_us
# constants
spike_duration = 3.5 # spike duration, ms
pulse_duration = 300 # dwell (including spike time), ms
total_cycle = 2000 # period of the cycle, ms
pulse_duration = 500 # dwell (including spike time), ms
total_cycle = 3000 # period of the cycle, ms
# establish pins
led_pin = Pin(27, Pin.OUT)
......@@ -14,10 +14,8 @@ sol_pin = Pin(29, Pin.OUT)
timer = Timer() # timer object
# initiate variables
i = 1 # counter increment, ms
counter = -i # counter, ms
duty = 0 # duty cycle, %
duty_percent = 7
duty_percent = 15
hold_duty = round(duty_percent/100*65535) # reduced duty cycle, %
# pwm object, off
......@@ -25,36 +23,31 @@ pwm_led = PWM(led_pin, freq=10000, duty_u16=duty)
pwm_sol = PWM(sol_pin, freq=100000, duty_u16=duty)
def pwm_control(timer):
global counter, duty
counter += i
if counter < spike_duration:
# spike pulse, 100% duty cycle
duty = 65535
elif counter < pulse_duration:
# hold, X% duty cycle
duty = hold_duty
elif counter < total_cycle:
# 0% duty cycle
duty = 0
else:
# counter rest
counter = -i;
print("reset counter")
pwm_led.duty_u16(duty) # set the duty cycle of the led
pwm_sol.duty_u16(duty) # set the duty cycle of the solenoid
# on cycle
pwm_led.duty_u16(65535) # set the duty cycle of the led
pwm_sol.duty_u16(65535) # set the duty cycle of the solenoid
sleep_us(int(spike_duration*1000))
# pwm cycle
pwm_led.duty_u16(hold_duty) # set the duty cycle of the led
pwm_sol.duty_u16(hold_duty) # set the duty cycle of the solenoid
sleep_us(int((pulse_duration-spike_duration)*1000))
# off cycle
pwm_led.duty_u16(0) # set the duty cycle of the led
pwm_sol.duty_u16(0) # set the duty cycle of the solenoid
print("initiate runtime")
try:
# timer initialize
timer.init(freq=(1/i)*1e3, mode=Timer.PERIODIC, callback=pwm_control)
timer.init(period=total_cycle, mode=Timer.PERIODIC, callback=pwm_control)
# allocate for extra application functionality
while True:
pass
sleep(10)
except KeyboardInterrupt:
# catch a soft reboot or keyboard interrupt
......@@ -91,4 +84,4 @@ finally:
print("purge complete")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment