# Project#3 - Blinking both LEDs when a button is pressed # Connections: Add jumpers to the LED headers (Labelled G16BLU and G17RED) to the left of the coin battery. # Also add jumpers to the GP15 S1 header and GP14 S@ headers to the right of the coin battery socket. import machine # Think of "import" as importing a library. This library allows for us to use GPIOs import utime # This "library" allows for us to tell time if connected to the internet, and use delays like 'utime.sleep(1)' # This is a new addition to the last piece of code Button = machine.Pin(15, machine.Pin.IN) #Setup the SEL1 button pin to GPIO 15. Set it as an input and name it "Button" Button2 = machine.Pin(14, machine.Pin.IN) #Setup the SEL2 button pin to GPIO 14. Set it as an input and name it "Button2" # LEDRED = machine.Pin(16, machine.Pin.OUT) # Set up GPIO16 as an output, and name it LEDRED LEDBLU = machine.Pin(17, machine.Pin.OUT) # Do the same for GPIO17 LEDRED.value(0) # Turn the red LED off. If you set it to (1), then you'd be turning it on LEDBLU.value(0) # Turn the blue LED off. If you set it to (1), then you'd be turning it on while True: # Loop forever. We've exchanged our FOR/range with a While loop. Because the condition is set, the following code will loop forever. if Button.value() == 0: # If during this forever loop we press and hold button#1 (S1), the LEDs will blink while you hold it down. LEDRED.value(1) # Turn the red LED on. Notice the indent here. This naturally happens after a colon. It is necessary to compile utime.sleep(0.01) # Wait 10ms (0.01 seconds) LEDRED.value(0) # Turn the red LED off utime.sleep(0.01) # Wait another 0.01 seconds LEDBLU.value(1) # Do the same for the Blue LED utime.sleep(0.01) LEDBLU.value(0) utime.sleep(0.01) # Challenge: Add a range (talked about in project#1) that cycles the LEDs 100 times each time the button is pressed. # While we've added both buttons into the mix within this program, we are only using one. # The buttons are held high to 3v through a pull-up resistor, so when you press the button, you see 0v (0) # 1 = high/3.3v & 0 = low/0v