rowan_mcalpin
Published © GPL3+

LED Whack-A-Mole V2

Imagine you are sitting at your workbench, satisfied, playing a game you made yourself. That's what this project shows you how to do.

BeginnerFull instructions provided1,089
LED Whack-A-Mole V2

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Analog joystick (Generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×7
High Brightness LED, White
High Brightness LED, White
×4
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Green
5 mm LED: Green
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×4
Jumper wires (generic)
Jumper wires (generic)
×15

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram

Code

Arduino Code

Arduino
/*
Whack-A-LED
Original project by Courtlandjensen
Code by Rowan McAlpin and Courtlandjensen
Circuitry by Rowan McAlpin and Courtlandjensen
GNU General Public License version 3
Copyleft 2021
*/

#include <EEPROM.h>

// USER VARIABLES
// EDIT THESE TO CHANGE THE PACE OF THE GAME
int lossesBeforeEnd = 3; // how many times you can miss an LED before you lose
int startingDifficulty = 1000; // time in milliseconds the first light waits before turning off. 1000 is 1 second.
int changeAmount = 50; //time in milliseconds the wait time increases or decreases each round.


// Joystick variables
int sX = A0;     //joystick x axis, analog input
int sY = A1;     //joystick y axis, analog input
int sSX;         //state of x, reading from sX
int sSY;         //state of y, reading from sY
int sS;          //converted state (may not be most efficient)

// Game variables
int rNum;        //random int choosing the random light to turn on
int wins=0;      //counting consecutive wins
int losses=0;
int highScore=0; //saving the highest score of consecutive wins

// Difficulty constants, time to react
int currentDifficulty=startingDifficulty;

int ledPins[]={5,2,4,3,6,7,8}; //initializing led's
int pinCount=7;                //number of led pins

// More random randoms
void reseedRandom( void )
{
  static const uint32_t HappyPrime = 732;
  union
  {
    uint32_t i;
    uint8_t b[4];
  }
  raw;
  int8_t i;
  unsigned int seed;
  
  for ( i=0; i < sizeof(raw.b); ++i )
  {
    raw.b[i] = EEPROM.read( i );
  }

  do
  {
    raw.i += HappyPrime;
    seed = raw.i & 0x7FFFFFFF;
  }
  while ( (seed < 1) || (seed > 2147483646) );

  randomSeed( seed );  

  for ( i=0; i < sizeof(raw.b); ++i )
  {
    EEPROM.write( i, raw.b[i] );
  }
}

void setup() {
  reseedRandom();              // EVEN MORE
  randomSeed(random(10000));   // RANDOM RANDOMS
  digitalWrite(1, LOW);
  pinMode(sX, INPUT);
  pinMode(sY, INPUT);

//  randomSeed(analogRead(0));
//  randomSeed(random(9999));

  pinMode(ledPins[0], OUTPUT);
  pinMode(ledPins[1], OUTPUT);
  pinMode(ledPins[2], OUTPUT);
  pinMode(ledPins[3], OUTPUT);
  pinMode(ledPins[4], OUTPUT);
  pinMode(ledPins[5], OUTPUT);
  pinMode(ledPins[6], OUTPUT);
}

void dead() {
  for (int j=0; j<pinCount; j++) {
    digitalWrite(ledPins[j], HIGH);
  }
  delay(1000);
  for (int k=0; k<pinCount; k++) {
    digitalWrite(ledPins[k], LOW);
  }
  exit(0);
}

void loop() {
  rNum=random(4); //generating random choice
  delay(1000);
  digitalWrite(ledPins[rNum], HIGH); //lighting the randomly chosen bulb
  delay(currentDifficulty); //difficulty
  
  //stick stuff
  //512 MAGIC NUMBER  128 256 384 512 640 768 896 1024
  sSX = analogRead(sX); //reading x axis input
  delay(100);
  sSY = analogRead(sY); //reading y axis input

  //converting y and x inputs to 4 possibilities. 0 and 1023 are the maximum values on each axis of the joystick, as measured.
  sS=0;
//  switch (sSX) {
//  case 0:
//  sS=1;      // Left
//  break;
//  case 1024:
//  sS=2;      // Right
//  break;
//}
//switch (sSY) {
//  case 0:
//  sS=3;      // Up
//  break;
//  case 1024:++
//  sS=4;      // Down
//  break;
//}
  if (sSX > 768) {
    sS=2; //RIGHT
  } else if (sSX < 384) {
    sS=1; //LEFT
  }
  if (sSY > 768) {
    sS=3; //UP
  } else if (sSY < 384) {
    sS=4; //DOWN
  }

  digitalWrite(ledPins[rNum], LOW); //turning off the randomly selected bulb, after the joystick choice was made
  if (sS-1==rNum) //checking if the user input the correct direction of the lit bulb
  {
    wins++;
    currentDifficulty-=changeAmount;
    for (int k=0; k<=3; k++) {     //blinking green light indicating correct choice
    digitalWrite(ledPins[4], HIGH);
    delay(50);
    digitalWrite(ledPins[4], LOW);
    delay(50);
    }
  }
  else
  {
    losses++;
    currentDifficulty+=changeAmount;
    if (wins>highScore) { //if the consecutive wins are more than the previous highscore, the new highscore is set.
        highScore=wins;
        wins=0;
    }
    for (int i=0; i<=3; i++) {       //blinking red light indicating incorrect choice
      digitalWrite(ledPins[5], HIGH);
      delay(50);
      digitalWrite(ledPins[5], LOW);
      delay(50);
    }
    for (int w=0; w<highScore; w++) { //displaying via counting and blinking the high score on a yellow bulb.
        digitalWrite(ledPins[6], HIGH);
        delay(200);
        digitalWrite(ledPins[6], LOW);
        delay(200);
    }
    if(losses==lossesBeforeEnd) {
      dead();
    }  
  }  
  }

Credits

rowan_mcalpin

rowan_mcalpin

2 projects • 0 followers

Comments