This assignment prompted our class to create a game that was 2 player and had a sort of timed LED blink function using certain components including LEDs, Arduino, and buttons. The inspiration for my game stemmed from my desire to use a randomized feature. This led me to think of the main concept for my game’s design: a button and an LED would be placed in front of each player and then one of them would be randomly selected to light up and the player who’s LED it was must then press their button. After realizing that this would probably be too easy and not very competitve I added in the dependent blink times on each player’s LED. By this I mean that once a player’s LED lights up they must hit their button before 500 milliseconds, however, if they are quicker than that to hit their button then that time is recorded and used for the next randomly selected LED’s blink. This allows players to strategize how quickly they want to hit their button. The quicker a player hits the button on their turn, the more difficult it will be for the next player to hit their button before it blinks off, but their is also the chance that the original player’s LED gets selected again and then they have only that short time to hit their own button before the blink turns off. In this way the faster the player hits their button the quicker the competitor must be and the quicker that original player must be as well.
The game begins with the yellow LED in the center on. Either player then starts the game by pressing their button, which picks a random player to go first. The randomly selected player then must press their button before the LED in front of them blinks off. If they are too late and run out of time, their red LED lights up signaling to the scorekeeper that this player now has one strike. If they do in fact hit their button before their LED blinks off then the time that the LED was lit is recorded and a player is randomly selected again. The recorded blink time is then used as the time the current player has to push their button before time runs out and their red LED turns on and they receive a strike. The idea is that the players or a scorekeeper would keep track of the red LED strikes given to players and the first player to reach 3 strikes loses giving the win to the other player. Pictured below is the overall setup and a closeup of the LED and button structure on the breadboard.
Before I began to program this game, I drew up the following state machine to assist in my thought process as I moved forward:
Next, after drawing up this state diagram I started coding. This began with including a bounce library to be used on the buttons. Next I declared the 5 led variables, the buttons and the debouncers. Following those variables I declared the enumerated states as are written on my state diagram. From there I declared the current state, and several “long” variables to be used later for randomizing and timing. The next portion of the code is the setup which contains the pinMode for each led and button, the current state of the program, a timerStart, startTime and endTime, and two syntaxes which I learned online in order to use the random function: Serial.begin() and randomSeed(analogRead(A0)). After all of that code which essentially prepared the Arduino to perform the functions necessary, I wrote the loop code which communicates the actions of each component. My loop section is split up into the three parts of “display state”, “update/read inputs”, and “update state based on inputs”. The “display state” section explains what will happen within each state, for example which LED’s will be on and which ones off. The “update/read inputs” section does as it sounds and reads inputs including the current time, and if either button has been pressed. Lastly, the “update state based on inputs” uses the input data to move through a sequence of “if” and “else if” statements to determine what to do next as the game progresses. So, these three sections together make up the “loop” portion which, paired with the “setup” portion and code before “setup”, make up the entirety of this reaction game’s programming.
Difficulties and Learning moments
Besides for becoming more familiar with the abilities of the Arduino programming tools, a new function which I learned during this assignment is the random function. I used this function so that the player’s LED’s were randomly selected and the turns were unexpected, increasing the competitive aspect of the game that I described in the inspiration above. I learned how to use the random function on the Arduino website and encountered one main difficulty in the process. I had most all of the components of the random function’s syntax that I needed, however, I would run the program on my Arduino and get the exact same output overtime. This led me to some troubleshooting and realizing that in order to get different random outputs each time, I had to attach the random function to an empty pin input. This would allow the function to read the empty pin each time through the loop and I learned through research that an empty pin is considered “floating” and returns a noise value. The noise allows “random” to use different sequences of random numbers each time the sketch is run. Pictured below is the final use of the random function to actually produce the random return which moves the program along. The other large difficulty I had in this project had to do with the timing. I had a hard time getting the LEDs to all behave as I wanted, but, with some troubleshooting and asking questions, they eventually worked. I want to continue to grow in familiarity with the timing functions of Arduino as I seem to have a hard time fully grasping them.
So, this reaction challenge grew my comfort with Arduino programming and troubleshooting while also teaching me the random function.