Ok, here's my very much untested solution, but I think it should at least provide a starting point.The main differences are:Code:
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)def relay_control():pull(block) # Get the debounce time (in ticks).mov(y, osr) # Save debounce time in Y for later use.wrap_target()label('wait_low')mov(x, y) # Prepare X for debounce counter.wait(0, pin, 0) # Wait for sensor LOW level.label('loop1')jmp(x_dec, 'loop1') # Wait for debounce time.jmp(pin, 'wait_low') # If the pin is HIGH, the detected LOW state was a fluke, go back to waiting for LOW.set(pins, 1) # Turn on relay.label('wait_high')mov(x, y) # Prepare X for debounce counter.wait(1, pin, 0) # Wait for the pin to go HIGH.label('loop2')jmp(x_dec, 'loop2') # Wait for debounce time.jmp(pin, 'high_ok') # Pin is still HIGH, accept.jmp('wait_high') # Pin is LOW, go back to waiting for the sensor pin to go HIGH.label('high_ok')set(pins, 0) # Turn relay off.sm = rp2.StateMachine(i, relay_control, in_base=sensor_pin, set_base=relay_pin, jmp_pin=sensor_pin)sm.put(31_250_000) # Load debounce time.sm.active(1)
The debounce time can be changed by putting another value to the state machine before starting it. If there's something I'm missing let me know and I'll try to get back to it tomorrow.
- As hippy noted, the wait polarities are switched to match the comments.
- The state machine runs full speed, so the debounce time matches comments.
- The debounce is redone so it checks if the sensor pin state didn't change for the set time before switching the relay on/off instead of switching immediately and waiting later.
This feels like the closest thing so far! Right now with no changes, triggering the sensor and keeping it triggered (pressing the button on the test breadboard) toggles the relay on after the debounce delay, but then off again after another debounce delay. If the button is released before the second debounce delay, the relay stays on after releasing the button. Ultimately, the goal is to see the relay start when the sensor is triggered (after the debounce delay), turn off with no sensor input (after the debounce delay), and remain in its current state if there are any changes to the sensor state that is less than the debounce delay. So if it's 'on' and it sees a short 'off', it doesn't swap states, and vice versa.
Statistics: Posted by Darkhand — Sun Aug 04, 2024 5:43 pm