Week 7: Reaction Game with LED Lights and Buttons

 

MAIN IDEA OF GAME

For this week’s assignment, I decided to create a small reaction game where the user will have to press the button corresponding to the color of the LED light that will be lightened up in a random order within a small interval, which is 3 seconds. When the right button is pressed, it will turn off that LED light and another one will light up again. 
Sketch of original idea

SETTING IT UP

When it came to setting up the hardware part on the breadboard and microcontroller, surprisingly I did not encounter any difficulties as my setup was overall not that complex or confusing.  However,  it  is  after a couple days that I realized I had misplaced one of my pins for the red button one row lower than it should be and this caused me to spend hours figuring out why that one button wouldn’t work.

Also, as seen in the first picture above of my original idea, I originally wanted to place the buttons that are the same color as the LED lights in the same row. But later, I decided it would be more interesting and kind of more difficult for the user if the buttons were not in the same row as the LED lights that were the same color.


CODING 

This was where I had the most difficulties in mainly because I was not able to code in this new environment aside from Processing, as we’ve been coding on that for so long now.

Coding for randomly lighting up LED lights

For this section of code for the LED lights to light up randomly, I struggled with it originally as I just simply just told it to choose a random number and turn on that random number in correspondence to the LED light, have a little delay, then light up another random one. But, later, I realized that there was so much more needed in it for it to work, such as the fact that during the delay, it won’t do anything so that means that my buttons will not be able to work, therefore I could not use the delay function.

Luckily, I found the “Blink without Delay” example in Arduino and I incorporated that into my own code while configuring it to include the random element.

Coding for Button Press

When coding for the buttons to turn off the corresponding LED lights, I had the most difficulty in understanding the concept about previous and current button states, where after the button is pressed and released in the current frame, I have to set LOW to the previous button state so that the next time it can fit the condition to be a new button press again. (Not sure if I explained it clearly, but that’s the gist.)

My Code

const int GreenLed = 2;
const int RedLed = 3;
const int YellowLed = 4;

int GreenLedState = LOW;
int RedLedState = LOW;
int YellowLedState = LOW;

const int Redbutton = 10;
const int Yellowbutton = 9;
const int Greenbutton = 8;

int prevGreenButtonState = LOW;
int prevRedButtonState = LOW;
int prevYellowButtonState = LOW;

int randomNum; //pick random light to turn on (from pin 2 to pin 4)

unsigned long previousMillis = 0; // will store last time LED was updated

const long interval = 3000; //interval at which to blink (milliseconds)

void setup() {
  // setup all LED pins as outputs
  for (int i = 2; i < 5; i++) {
    pinMode(i, OUTPUT);
  }

  Serial.begin(9600);
  Serial.println(randomNum);

  //setup all button pins as inputs
  for (int i = 8; i < 11; i++) {
    pinMode(i, INPUT);
  }

}
void loop() {
  LEDLightSequence();
  Greenbuttonpress();
  Redbuttonpress();
  Yellowbuttonpress();
}

void LEDLightSequence() {//code to light up random LED light
  randomNum = random(2, 5);//pick random light to turn on (from pin 2 to pin 4)
  Serial.println(randomNum);
  unsigned long currentMillis = millis();
  // check to see if it's time to blink the LED; that is,
  //save the last time you blinked the LED

  //if the difference between the current time and last time you blinked the LED is bigger than
  //the interval at which you want to blink the LED.
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    //if the LED is off turn it on and vice versa
    if (randomNum == GreenLed && GreenLedState == LOW) {
      GreenLedState = HIGH;
    } else {
      GreenLedState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(GreenLed, GreenLedState);

    if (randomNum == RedLed && RedLedState == LOW) {
      RedLedState = HIGH;
    } else {
      RedLedState = LOW;
    }
    digitalWrite(RedLed, RedLedState);

    if (randomNum == YellowLed && YellowLedState == LOW) {
      YellowLedState = HIGH;
    } else {
      YellowLedState = LOW;
    }
    digitalWrite(YellowLed, YellowLedState);
  }
}

void Greenbuttonpress() {
  int currentGreenbutton = digitalRead(Greenbutton); // check if green button is pressed
  if (currentGreenbutton == HIGH && prevGreenButtonState == LOW) { //if the randm number chosen is 2, the Green LED light is lit and the greenbutton was pressed, turn off the green led light
    if (GreenLedState == HIGH) {
      GreenLedState = LOW;
    }
  }

  digitalWrite(GreenLed, GreenLedState);
  prevGreenButtonState = currentGreenbutton;
}

void Redbuttonpress() {
  int currentRedButton = digitalRead(Redbutton); // check if green button is pressed
  if (currentRedButton == HIGH && prevRedButtonState == LOW) { //if current red button is pressed and the previous red button in the last frame wasn't pressed (wasn't pressed before)
    if (RedLedState == HIGH) { //if the red led light is on
      RedLedState = LOW; //turn the red led light off
    }
  }
  digitalWrite(RedLed, RedLedState);
  prevRedButtonState = currentRedButton; //after button is pressed (HIGH) and released (LOW) in current frame, set LOW to prevButtonstate so next time it can be newly pressed again
}

void Yellowbuttonpress() {
  int currentYellowButton = digitalRead(Yellowbutton); // check if green button is pressed
  if (currentYellowButton == HIGH && prevYellowButtonState == LOW) { //if current red button is pressed and the previous red button in the last frame wasn't pressed (wasn't pressed before)
    if (YellowLedState == HIGH) { //if the red led light is on
      YellowLedState = LOW; //turn the red led light off
    }
  }
  digitalWrite(YellowLed, YellowLedState);
  prevYellowButtonState = currentYellowButton; //after button is pressed (HIGH) and released (LOW) in current frame, set LOW to prevButtonstate so next time it can be newly pressed again
}

 

FINAL PRODUCT

One thought on “Week 7: Reaction Game with LED Lights and Buttons”

Leave a Reply