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)
- 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.
Statistics: Posted by horuable — Sat Aug 03, 2024 8:56 pm