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 from machine import Pin, PWM, Timer
import time from time import sleep, sleep_us
# constants # constants
spike_duration = 3.5 # spike duration, ms spike_duration = 3.5 # spike duration, ms
pulse_duration = 300 # dwell (including spike time), ms pulse_duration = 500 # dwell (including spike time), ms
total_cycle = 2000 # period of the cycle, ms total_cycle = 3000 # period of the cycle, ms
# establish pins # establish pins
led_pin = Pin(27, Pin.OUT) led_pin = Pin(27, Pin.OUT)
...@@ -14,10 +14,8 @@ sol_pin = Pin(29, Pin.OUT) ...@@ -14,10 +14,8 @@ sol_pin = Pin(29, Pin.OUT)
timer = Timer() # timer object timer = Timer() # timer object
# initiate variables # initiate variables
i = 1 # counter increment, ms
counter = -i # counter, ms
duty = 0 # duty cycle, % duty = 0 # duty cycle, %
duty_percent = 7 duty_percent = 15
hold_duty = round(duty_percent/100*65535) # reduced duty cycle, % hold_duty = round(duty_percent/100*65535) # reduced duty cycle, %
# pwm object, off # pwm object, off
...@@ -25,36 +23,31 @@ pwm_led = PWM(led_pin, freq=10000, duty_u16=duty) ...@@ -25,36 +23,31 @@ pwm_led = PWM(led_pin, freq=10000, duty_u16=duty)
pwm_sol = PWM(sol_pin, freq=100000, duty_u16=duty) pwm_sol = PWM(sol_pin, freq=100000, duty_u16=duty)
def pwm_control(timer): def pwm_control(timer):
global counter, duty
counter += i # on cycle
if counter < spike_duration: pwm_led.duty_u16(65535) # set the duty cycle of the led
# spike pulse, 100% duty cycle pwm_sol.duty_u16(65535) # set the duty cycle of the solenoid
duty = 65535 sleep_us(int(spike_duration*1000))
elif counter < pulse_duration: # pwm cycle
# hold, X% duty cycle pwm_led.duty_u16(hold_duty) # set the duty cycle of the led
duty = hold_duty pwm_sol.duty_u16(hold_duty) # set the duty cycle of the solenoid
sleep_us(int((pulse_duration-spike_duration)*1000))
elif counter < total_cycle:
# 0% duty cycle # off cycle
duty = 0 pwm_led.duty_u16(0) # set the duty cycle of the led
pwm_sol.duty_u16(0) # set the duty cycle of the solenoid
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
print("initiate runtime") print("initiate runtime")
try: try:
# timer initialize # 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 # allocate for extra application functionality
while True: while True:
pass pass
sleep(10)
except KeyboardInterrupt: except KeyboardInterrupt:
# catch a soft reboot or keyboard interrupt # catch a soft reboot or keyboard interrupt
...@@ -91,4 +84,4 @@ finally: ...@@ -91,4 +84,4 @@ finally:
print("purge complete") 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