User Tools

Site Tools


pulse_gen

Pulse Generator for testing PLB2 RL RF switch

import machine
import time

# Configure the output pins (change the pin number as needed)
led_B = machine.Pin(25, machine.Pin.OUT)
led_G = machine.Pin(16, machine.Pin.OUT)
led_R = machine.Pin(17, machine.Pin.OUT)

sw = machine.Pin(3, machine.Pin.OUT)
sw.off()
gnd = machine.Pin(4, machine.Pin.OUT)
gnd.off()

# Configure the input pins for buttons (GPIO 1 and 2)
btn_minus = machine.Pin(1, machine.Pin.IN, machine.Pin.PULL_UP)  # Button to decrease T_ON
btn_plus = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP)   # Button to increase T_ON

# Initialize LED states
led_B.on()
led_G.on()
led_R.on()

# Timing variables
T_OFF = 10  # Default OFF time
T_ON = 1    # Default ON time
T_STP = 0.1 # Step increment
T_ON_MAX = 30
T_ON_MIN = 1
T_OFF_MAX = 30
T_OFF_MIN = 1
T_STP_MAX = 2.5
T_STP_MIN = 0.1

# Function to display current settings and get new values
def get_new_value(param_name, current_value, min_value, max_value):
    while True:
        try:
            new_value = float(input(f"Set {param_name} (current: {current_value:.1f}, range: {min_value}-{max_value}): "))
            # Ensure new_value is within the specified range and in 0.1 steps
            if min_value <= new_value <= max_value and (new_value * 10) % 1 == 0:
                return new_value
            else:
                print(f"Please enter a value between {min_value} and {max_value} in 0.1 steps.")
        except ValueError:
            print("Invalid input. Please enter a number.")

# Button handler to sort actions
def button_handler(pin):
    global T_ON, T_OFF, T_STP

    # Check if both buttons are pressed
    if not btn_plus.value() and not btn_minus.value():
        print("Both buttons pressed. Entering configuration mode...")
        led_B.off()
        led_G.on()
        led_R.on()
        # Enter configuration mode
        T_ON = get_new_value("T_ON", T_ON, T_ON_MIN, T_ON_MAX)
        T_OFF = get_new_value("T_OFF", T_OFF, T_OFF_MIN, T_OFF_MAX)
        T_STP = get_new_value("T_STP", T_STP, T_STP_MIN, T_STP_MAX)
        print("Exiting configuration mode...")
    else:
        # Check which button is pressed
        if not btn_plus.value():  # Button to increase T_ON
            if T_ON < T_ON_MAX:
                T_ON += T_STP
                if T_ON > T_ON_MAX:  # Cap at T_ON_MAX
                    T_ON = T_ON_MAX
                print(f"T_ON increased to {T_ON:.1f} seconds")
        elif not btn_minus.value():  # Button to decrease T_ON
            if T_ON > T_ON_MIN:
                T_ON -= T_STP
                if T_ON < T_ON_MIN:  # Cap at T_ON_MIN
                    T_ON = T_ON_MIN
                print(f"T_ON decreased to {T_ON:.1f} seconds")

# Attach interrupts for the buttons
btn_plus.irq(trigger=machine.Pin.IRQ_FALLING, handler=button_handler)
btn_minus.irq(trigger=machine.Pin.IRQ_FALLING, handler=button_handler)

# Main loop to blink the LED
while True:
    led_B.on()
    
    led_G.off()
    led_R.on()
    sw.on()
    print(f"LED ON for {T_ON:.1f} seconds (1..30 secs)")
    time.sleep(T_ON)  # Keep it on for the specified time

    led_G.on()
    led_R.off()
    sw.off()
    
    print(f"LED OFF for {T_OFF} seconds")
    time.sleep(T_OFF)  # Keep it off for the specified time
pulse_gen.txt · Last modified: 2024/09/24 13:26 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki