# Project#4 - Let's talk DIP Switches and functions # 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. # The DIP swtiche channels are tied to GPIOs 18-thrrough 21 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)' 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" # DIPA = machine.Pin(21, machine.Pin.IN) #Setup DIP Switch Channel-A as an output DIPB = machine.Pin(20, machine.Pin.IN) #Setup DIP Switch Channel-B as an output DIPC = machine.Pin(19, machine.Pin.IN) #Setup DIP Switch Channel-C as an output DIPD = machine.Pin(18, machine.Pin.IN) #Setup DIP Switch Channel-D as an output # 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 def DIPStates(): # Sweet! Our first (Very simple) function. We can call this at any time by typing "DIPstates() # If prints the dstates of each of the four DIP switch channels to the shell. The main code is below print("DIP Switch Status Check:") # Print this to the shell if DIPA.value() == True: # Check to see if DIP channel A is high/true. print("DIP Channel-A is OFF...") # If so, print this to the shell else: # Otherwise, print the following to the shell print("DIP Channel-A is ON...")# This piece of code is coped and pasted for each of the four DIP switches if DIPB.value() == True: # print("DIP Channel-B is OFF...") else: print("DIP Channel-B is ON...") if DIPC.value() == True: print("DIP Channel-C is OFF...") else: print("DIP Channel-C is ON...") if DIPD.value() == True: print("DIP Channel-D is OFF...") else: print("DIP Channel-D is ON...") # This is the main code 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 the SW1 button, then the dipstates function will be called DIPStates() # Call the DIPstates function print("Wait one second") # Print this to the shell utime.sleep(1) # Wait one second # In this piece of code, we call a function, use the "print" function for the rist time, and read the DIP switch states. # As you can see, a lot of this basic codeing is very similar to Arduino coding.