The idea
We created an LED-based game using an RGB led and a button. The RGB LED alternates rapidly between several colors, and the player must try to click the button exactly when the LED is yellow. If they are successful, they get a point. Each time they are successful, we update their score. The goal is to get to 10 points as fast as possible. When the player reaches 10 points, they win, and we let them know how long it took them to beat the game.
The process
We had to figure out how to connect the process of rapidly switching the LED color with taking input from a button. We started by connecting the RGB LED in a circuit and tested creating different colors by alternating the inputs to each of the red, green, and blue LEDs. We then connected the button and wrote code to detect what the current color was whenever the button was pressed. Finally, we wrote code to keep track of the score display messages to the user.
The hardware/software
Hardware: We used a single push button, a single RGB LED, 4 resistors, an Arduino, and several wires. The red part of the LED connects to pin 4, the green part connects to pin 3, and the blue part connects to pin 2; each connection goes through a single resistor. The cathode of the led connects to ground. The push button is connected to pin 7, and in a circuit with ground and 5V through a resistor.
Software: Our code reads the button input to detect if it is currently being pressed. If it is being pressed, it reads the values of the R, B, and G pins and determines whether the current color is yellow. If the current color is yellow, it updates the score and uses Serial.print to display a message. Then, we wait until the user is no longer pressing the button to continue playing the game (so they can't just rack up points by holding the button on yellow). Then, we use a switch statement to pick the new color for the LED using an index variable that changes each time loop() is run. The outer branch of the code checks each time if the user has reached a score of 10, and if they have, the game ends and we tell them how long it took them to win.
What would we add?
If we could build this in person, we would implement a game play with a more complicated input sensor than a button. For example, we might use a sound sensor that could detect when someone claps, so that instead of timing the colors with a button press we would time it with a player clapping.
Log in or sign up for Devpost to join the conversation.