# digital_input.py

# Raspberry Pi Pico - Digital Input demo

# Read a digital I/O line, drive the onboard LED, and print messages to the
# serial console only when the input state changes.

import board
import time
from digitalio import DigitalInOut, Direction, Pull

#---------------------------------------------------------------
# Set up the hardware: script equivalent to Arduino setup()

# Set up built-in green LED for output.
led = DigitalInOut(board.LED)  # GP25
led.direction = Direction.OUTPUT

# Set up a digital input on GP15, which is physically pin 20 at one corner of the board.
# E.g., this may be attached to a switch or photointerrupter with associated pullup resistor.
switch = DigitalInOut(board.GP15)
switch.direction = Direction.INPUT

#---------------------------------------------------------------
# Run the main loop: script equivalent to Arduino loop()

# The following logic detects input transitions using a state machine with two
# possible states.  The state index reflects the current estimated state of the
# input.
state_index = False

while True:
    if state_index is False:
        if switch.value is True:
            led.value = True
            state_index = True
            print("On")

    elif state_index is True:
        if switch.value is False:
            led.value = False
            state_index = False
            print("Off")
