From c8b0b2d06538337898d3a3fc26673cf3e53c21a8 Mon Sep 17 00:00:00 2001
From: nikhil b lal <nikhil@macbook-air.media.mit.edu>
Date: Mon, 22 Apr 2024 19:42:42 -0400
Subject: [PATCH] simplified code

---
 code/main.py | 51 ++++++++++++++++++++++-----------------------------
 1 file changed, 22 insertions(+), 29 deletions(-)

diff --git a/code/main.py b/code/main.py
index 183f585..ba4b5a7 100644
--- a/code/main.py
+++ b/code/main.py
@@ -1,10 +1,10 @@
 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
+    
-- 
GitLab