from machine import Pin, PWM
import time
import utime
# Define the GPIO pin number
output_pin = 15 # Change this to the desired GPIO pin
input_pin = 14
ICP_ready_signal = 16
laser_firing = 21
wait_trig = 20
# Configure the on time of the pulse in microseconds
on_time = 50
# Configure the button pin
button = Pin(ICP_ready_signal, Pin.IN, Pin.PULL_UP)
# Setup LEDs pin as output
led = Pin(laser_firing, Pin.OUT)
wait_trig_led = Pin(wait_trig, Pin.OUT)
#Set LEDs Off
wait_trig_led.value(0)
led.value(0)
# Configure PWM on the specified pin
pwm = PWM(Pin(output_pin))
# Get the desired frequency from user input or any other source
desired_frequency = int(input("Enter the desired frequency (Hz): "))
# Configure the input pin for pulse counting
count_pin = Pin(input_pin, Pin.IN)
pulse_count = 0
# Function to increment the pulse count on rising edge
def count_pulses(pin):
global pulse_count
pulse_count += 1
# Attach the interrupt to the rising edge of the input pin
count_pin.irq(trigger=Pin.IRQ_RISING, handler=count_pulses)
# Set the desired number of pulses
desired_pulses = int(input("Enter the desired number of pulses: ")) # Change this to the desired number of pulses
sleep_time = 1 / desired_frequency
# Use PWM or PIO
desired_system = input("Use PWM or PIO? ")
try:
while True:
# Let us know the system Laser is Waiting for Trigger
wait_trig_led.value(1)
# Prompt to press the button to generate pulses
print("Press the button to generate pulses.")
# Wait for the button press
while button.value() == 1:
pass
# Turn of Wait for Trigger LED
wait_trig_led.value(0)
# Once button is pressed, generate pulses
print("Button pressed! Generating pulses...")
# Turn on the firing LED
led.value(1)
while pulse_count < desired_pulses:
if 10 <= desired_frequency <= 1000:
print("Using PWM!")
# Calculate duty cycle for a 50-microsecond pulse at the specified frequency
duty_cycle = on_time / (1e6 / desired_frequency) * 100
# Ensure the duty cycle is within the valid range (0 to 100)
duty_cycle = max(0, min(duty_cycle, 100))
#Set the Freqency
pwm.freq(desired_frequency)
# Set the duty cycle
pwm.duty_u16(int(duty_cycle * 655.35)) # Scale to the 16-bit PWM range
else:
print("Using Software")
# If the frequency is outside the valid range, use a software-based approach
period = 1 / desired_frequency
pulse_duration = 50e-6 # 50 microseconds
# Define the pin for the software-based approach
pin = Pin(output_pin, Pin.OUT)
# Set the initial state of the pin
pin_state = 0
while pulse_count < desired_pulses:
pin.value(pin_state) # Toggle the pin state
time.sleep(pulse_duration)
pin_state = 1 - pin_state # Toggle the pin state
time.sleep(period - pulse_duration)
# Cleanup: Deinitialize the pin
#pin.deinit()
print("Generated the desired number of pulses.")
# Turn off the LED
led.value(0)
# Reset pulse count and stop pulsing
pulse_count = 0
pwm.deinit()
except KeyboardInterrupt:
# If you want to gracefully exit on keyboard interrupt
pass
finally:
# Cleanup: Deinitialize the pin
pwm.deinit()
# Detach the interrupt
count_pin.irq(trigger=0)