001g_Stop_LED_Game

Project Overview:

-Create a game that can stop an LED with the push of a button. Increase the speed and award points to the user.

Code:

int led3 =  3;      // the number of the LED pin

int led4 =  4;      // the number of the LED pin

int led5 =  5;      // the number of the LED pin

int ledGreen =  6;      // the number of the LED pin

int led7 =  7;      // the number of the LED pin

int led8 =  8;      // the number of the LED pin

int led9 =  9;      // the number of the LED pin

int btn = 2;

 

boolean Direction = true;

int score = 0;

int highscore = 0;

long previousOnTime = 0;

long previousOffTime = 0;

 

int ArrayofLeds[7] = {led3, led4, led5, ledGreen, led7, led8, led9};

 

void IsGreen();

void NotGreen();

void BlinkLedOn(int);

void BlinkLedOff(int);

 

void setup() 

{

  pinMode (btn, INPUT);

  pinMode(led3, OUTPUT);

  pinMode(led4, OUTPUT); 

  pinMode(led5, OUTPUT);

  pinMode(ledGreen, OUTPUT); 

  pinMode(led7, OUTPUT);

  pinMode(led8, OUTPUT); 

  pinMode(led9, OUTPUT);

}

 

void loop() 

{   

   int counter = 0;

   int LedSpeed = 200;

   int LedbtnPressed = 500;

   Serial.begin(9600);

   boolean Led = true;

   boolean Pressed = false;

   long PressedTime = 0;

   boolean PressedLed = true;

   boolean OnGreen = false;

   boolean BlinkGreen = false;

   boolean BlinkRed = false;

   int LedOnGreen = 300;

   long OnGreenTime = 0;

   int GreenCounter = 0;

  for(;;)

  {

    unsigned long currentMillis = millis();

    unsigned long currentOnTime = millis();

    unsigned long currentOffTime = millis();

    unsigned long PressedMillis = millis();

    unsigned long OnGreenMillis = millis();

    if (Direction == true)

    { 

      if (currentOnTime - previousOnTime > LedSpeed && Led == true && Pressed == false && PressedLed == true)

      {

        previousOnTime = currentOnTime;

        BlinkLedOn(counter);

        Led = false;       

      }

      if (digitalRead(btn) == HIGH)

      {

        Pressed = true;

        PressedLed = true;

        if (counter == 3)

        {

          OnGreen = true;

        }                   

      }

      if (PressedMillis - PressedTime > LedbtnPressed && Pressed == true && PressedLed == true)

      {

        PressedTime = PressedMillis;

        BlinkLedOn(counter);  

        PressedLed = false;

        Pressed = false;

      }

      

      if (PressedMillis - PressedTime > LedbtnPressed && Pressed == false && PressedLed == false)

      {

        BlinkLedOff(counter);

        Pressed = false;

        PressedLed = true;

        if (OnGreen == true)

        {

          BlinkGreen = true;

          score += 5;

          LedSpeed -= 10;

          Serial.println("Your Score");

          Serial.println(score);

        }

        else

        {

          BlinkRed = true;

          if (score > highscore)

          {

            highscore = score;

            Serial.println("New High Score");

            Serial.println(highscore);

          }

          else

          {

            Serial.println("High Score");

            Serial.println(highscore);

            Serial.println("Your Score");

            Serial.println(score);

          }

          score = 0;

          LedSpeed = 200;

        }

      }

      if (OnGreen == true && Pressed == false && PressedLed == true && BlinkGreen == true)

      {

        IsGreen();

        OnGreen = false;

        BlinkGreen = false;

      }

      else if (OnGreen == false && Pressed == false && PressedLed == true && BlinkRed == true)

      {

        NotGreen();

        BlinkRed = false;

      }

      

      if (currentOffTime - previousOffTime > (LedSpeed * 2) && Led == false && Pressed == false && PressedLed == true)

      {

        previousOffTime = currentOffTime;

        BlinkLedOff(counter);  

        Led = true;

        counter++;   

      }

      if (counter == 6)

      {  

        Direction = false;

      }

    }  

    

    if (Direction == false)

    { 

      if (currentOnTime - previousOnTime > LedSpeed && Led == true && Pressed == false && PressedLed == true)

      {

        previousOnTime = currentOnTime;

        BlinkLedOn(counter);

        Led = false;       

      }

      if (digitalRead(btn) == HIGH)

      {

        Pressed = true;

        PressedLed = true;

        if (counter == 3)

        {

          OnGreen = true;

        }                   

      }

      if (PressedMillis - PressedTime > LedbtnPressed && Pressed == true && PressedLed == true)

      {

        PressedTime = PressedMillis;

        BlinkLedOn(counter);  

        PressedLed = false;

        Pressed = false;

      }

      

      if (PressedMillis - PressedTime > LedbtnPressed && Pressed == false && PressedLed == false)

      {

        BlinkLedOff(counter);

        Pressed = false;

        PressedLed = true;

        if (OnGreen == true)

        {

          BlinkGreen = true;

          score += 5;

          LedSpeed -= 10;

          Serial.println("Your Score");

          Serial.println(score);

        }

        else

        {

          BlinkRed = true;

          if (score > highscore)

          {

            highscore = score;

            Serial.println("New High Score");

            Serial.println(highscore);

          }

          else

          {

            Serial.println("High Score");

            Serial.println(highscore);

            Serial.println("Your Score");

            Serial.println(score);

          }

          score = 0;

          LedSpeed = 200;

        }

      }

      if (OnGreen == true && Pressed == false && PressedLed == true && BlinkGreen == true)

      {

        IsGreen();

        OnGreen = false;

        BlinkGreen = false;

      }

      else if (OnGreen == false && Pressed == false && PressedLed == true && BlinkRed == true)

      {

        NotGreen();

        BlinkRed = false;

      }

      

      if (currentOffTime - previousOffTime > (LedSpeed * 2) && Led == false && Pressed == false && PressedLed == true)

      {

        previousOffTime = currentOffTime;

        BlinkLedOff(counter);  

        Led = true;

        counter--;   

      }

      if (counter == 0)

      {  

        Direction = true;

      }

    } 

  }

}

 

void BlinkLedOn(int counter)

{

  if (counter == 0)

  {

    digitalWrite(led3, HIGH);

  }

  else if (counter == 1)

  {

    digitalWrite(led4, HIGH);

  }

  else if (counter == 2)

  {

    digitalWrite(led5, HIGH);

  }

  else if (counter == 3)

  {

    digitalWrite(ledGreen, HIGH);

  }

  else if (counter == 4)

  {

    digitalWrite(led7, HIGH);

  }

  else if (counter == 5)

  {

    digitalWrite(led8, HIGH);

  }

  else if (counter == 6)

  {

    digitalWrite(led9, HIGH);

  }

}

 

void BlinkLedOff(int counter)

{

  if (counter == 0)

  {

    digitalWrite(led3, LOW);

  }

  else if (counter == 1)

  {

    digitalWrite(led4, LOW);

  }

  else if (counter == 2)

  {

    digitalWrite(led5, LOW);

  }

  else if (counter == 3)

  {

    digitalWrite(ledGreen, LOW);

  }

  else if (counter == 4)

  {

    digitalWrite(led7, LOW);

  }

  else if (counter == 5)

  {

    digitalWrite(led8, LOW);

  }

  else if (counter == 6)

  {

    digitalWrite(led9, LOW);

  }

}

 

void IsGreen()

{

  for (int i = 0; i < 3; i++)

  {

    digitalWrite(ArrayofLeds[3], HIGH);

    delay(300);

    digitalWrite(ArrayofLeds[3], LOW);

    delay(300);

  }

}

 

void NotGreen()

{

  for (int i = 0; i < 3; i++)

  {

    digitalWrite(ArrayofLeds[0], HIGH);  

    digitalWrite(ArrayofLeds[1], HIGH); 

    digitalWrite(ArrayofLeds[2], HIGH); 

    digitalWrite(ArrayofLeds[4], HIGH);

    digitalWrite(ArrayofLeds[5], HIGH);

    digitalWrite(ArrayofLeds[6], HIGH); 

    delay(300);

    digitalWrite(ArrayofLeds[0], LOW);  

    digitalWrite(ArrayofLeds[1], LOW); 

    digitalWrite(ArrayofLeds[2], LOW); 

    digitalWrite(ArrayofLeds[4], LOW);

    digitalWrite(ArrayofLeds[5], LOW);

    digitalWrite(ArrayofLeds[6], LOW); 

    delay(300);

  }

}

 

Schematic:

Layout:

What I learned / Problems

What I learned:

  • I learned how to use the mills() function to track the time on the Arduino. This was used to replace the delay in the code to make the LEDs blink back and forth.

Problems:

  • I found it difficult to make the LEDs blink back and forth without the delay function.