After tax, the total was $14.03, which brings my total cost for this experiment to $137.41.
But now that (I think) I have enough pieces to put together my Simon game, I need to decide on how exactly it's going to work.
- There will be 5 buttons. One button will be the start/reset button and the other four will be the input buttons. The reset button will be connected to pin 4, and the four input buttons will be connected in the following manner: A-12, B-16, C-20, D-21. All buttons will connect to ground when pressed and will be set as pull-up buttons.
- There will be 5 LEDs. I happened to have four different colors from the kit, so I will use a repeat color for the mode indicator on pin 22. The others will be connected to pins in the following manner: Red-5, Yellow-6, Green-13, Blue-19. (Note: Remember that the short leg connects to ground.)
It kind of looks like a mess because of all the wires, but I don't care. This isn't about making a pretty device; it's about making a functional one.
I needed to write a short program to test all of my connections and the basic code concepts, so I decided to simply make the buttons into toggle switches for the corresponding LEDs and have the reset button act as the kill button. This was not too difficult to do using the ideas from the code that I had already learned previously.
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
def LEDtoggle(channel):
if channel == A:
GPIO.output(Red, not GPIO.input(Red))
if channel == B:
GPIO.output(Yel, not GPIO.input(Yel))
if channel == C:
GPIO.output(Gre, not GPIO.input(Gre))
if channel == D:
GPIO.output(Blu, not GPIO.input(Blu))
# Label buttons and pins
kill_switch = 4
A = 12
B = 16
C = 20
D = 21
ModeInd = 22
Red = 5
Yel = 6
Gre = 13
Blu = 19
# Set GPIO pins
GPIO.setup(kill_switch, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(A, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(B, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(C, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(D, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(ModeInd, GPIO.OUT)
GPIO.setup(Red, GPIO.OUT)
GPIO.setup(Yel, GPIO.OUT)
GPIO.setup(Gre, GPIO.OUT)
GPIO.setup(Blu, GPIO.OUT)
# Set GPIO Interrupts
GPIO.add_event_detect(A, GPIO.FALLING, callback=LEDtoggle, bouncetime=200)
GPIO.add_event_detect(B, GPIO.FALLING, callback=LEDtoggle, bouncetime=200)
GPIO.add_event_detect(C, GPIO.FALLING, callback=LEDtoggle, bouncetime=200)
GPIO.add_event_detect(D, GPIO.FALLING, callback=LEDtoggle, bouncetime=200)
try:
GPIO.output(ModeInd, GPIO.HIGH)
GPIO.wait_for_edge(kill_switch, GPIO.FALLING)
GPIO.output(ModeInd, GPIO.LOW)
except KeyboardInterrupt:
GPIO.cleanup()
GPIO.cleanup()
I'm not going to make a video of this because it's quite boring. All that happens is that the correct lights turn on and off as I press the buttons. But it's good enough to consider this to be a successful test of the connections.The construction of the code itself is going to end up being a bit haphazard. I first want to work on the simple things like recording the input when I press buttons and flashing the right LEDs when I do that. I think I'm going to need to set up a number of functions that activate and deactivate the interrupts at different points during the program so that I don't have code being interrupted by other code when I don't want it to happen. I will also need to learn a thing or two about lists to store the randomly generated sequence and the user input sequence. I may also want to think about using lists to do something with the labeling scheme to simplify it a bit. And I still need to learn how to use pygame to make a nice graphical interface for the game. But these are all projects for another night.

No comments:
Post a Comment