Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
chukwunonso_j
Published

Arduino Arcade style LED Game

Create a simple game from LED's

IntermediateShowcase (no instructions)1,410
Arduino Arcade style LED Game

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×1
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
any push-button would do
×1
Arduino UNO
Arduino UNO
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1

Story

Read more

Schematics

breadboard_Diagram.png

Code

ledGame.ino

Java
/*
  LED Game
  Chukwunonso Elumeze
  03/07/2021
*/
#include <millisDelay.h>
//The pin buttons for the leds
const int ledTwo = 2;
const int ledThree = 3;
const int ledCorrect = 4; //Led to aim for
const int ledFive = 5;
const int ledSix = 6;
int redPin = 8;
int greenPin = 10;
int bluePin = 11;
//delay values
int delay_Speed = 2000;
int delay_Result = 500;
const int button = 13; //button Pin
int buttonState;       //button state
int difficult = 1;     //difficulty
millisDelay testDelay;
// the setup function runs once when you press reset or power the board
void setup()
{
  // initialize the pins as output.
  pinMode(ledTwo, OUTPUT);
  pinMode(ledThree, OUTPUT);
  pinMode(ledCorrect, OUTPUT);
  pinMode(ledFive, OUTPUT);
  pinMode(ledSix, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(button, INPUT);
}
// the loop function runs over and over again forever
void loop()
{
  int amount = 2;
  while (amount < 7)
  {
    //displays the LED for each pin
    displayGame(amount);
    amount++;
  }
}

void setDifficulty(int state_Of_Difficulty)
{
  //sets the delay to a value depending on the level of the player
  delay_Speed = 2000 / state_Of_Difficulty;
}
void getButtonState()
{
  //gets the state of the button
  buttonState = digitalRead(button);
}
//if the button was clicked at the wrong time
void badInput()
{
  if (buttonState == HIGH)
  {
    failedTheLevel(); //display the LED that shows you failed
    if (difficult != 1)
      difficult = difficult - 1;    //reset the difficulty
  }
}
//if the button was clicked at the right time
void goodInput()
{
  if (buttonState == HIGH)
  {
    passedTheLevel();          //display the LED that shows you passed
    difficult = difficult + 1; //increase the difficulty
  }
}
//Switches an LED ON/OFF to show that you passed the level
void passedTheLevel()
{
  setColor(0, 255, 0);
  delay(delay_Result);
  setColor(0, 0, 0);
}
//Switches an LED ON/OFF to show that you failed the level
void failedTheLevel()
{ setColor(255, 0, 0);
  delay(delay_Result);
  setColor(0, 0, 0);
}
//Switches on an LED and checks if the button was pressed
void displayGame(int ledTest)
{
  digitalWrite(ledTest, HIGH);
  testDelay.start(delay_Speed);
  while (testDelay.justFinished() == false)
  {
    getButtonState();
    if (ledTest != ledCorrect)
      badInput();
    else
      goodInput();
  };
  digitalWrite(ledTest, LOW);
  setDifficulty(difficult);
}
//Displays the Color on the RGB LED
void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

Credits

chukwunonso_j
1 project • 0 followers

Comments