goldingkenny
Published

Simon Says With Joystick

An LED Simon Says game with a joystick and buzzer.

BeginnerShowcase (no instructions)186
Simon Says With Joystick

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×13
Male/Female Jumper Wires
Male/Female Jumper Wires
×4
Analog joystick (Generic)
×1
Buzzer
Buzzer
×1
Resistor 220 ohm
Resistor 220 ohm
×2
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
Breadboard (generic)
Breadboard (generic)
×1
LED, Blue
LED, Blue
×1

Story

Read more

Schematics

Schematics

IMPORTANT: Connect the white wires to the joystick module. The x pin on the module should be connected to pin A4 on the uno and y pin should be connected to pin A5.

Code

Simon_Says_Game

C/C++
// Simon Says with joystick
// By: Kenny Golding

const int buzzPin = 6;
const int redPin = 8;
const int greenPin = 9;
const int yelwPin = 10;
const int bluePin = 11;
const int buttonPin = 7;
const int xPin = A4;
const int yPin = A5;

int xPos = 0;
int yPos = 0;
int rounds = 0;
int buttonState = 0;
int sequence[100];

boolean playGame = true;

void setup() {
  pinMode(buzzPin, OUTPUT),(redPin, OUTPUT), (greenPin, OUTPUT), (yelwPin, OUTPUT), (bluePin, OUTPUT);
  pinMode(buttonPin, INPUT);
  randomSeed(analogRead(0));
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  if((playGame == false)&&(buttonState == HIGH)){
    playGame = true;
    rounds = 0;
  }
  
  while(playGame){
    delay(1000);
    sequence[rounds] = random(0, 4);
    
    for(int i=0; i<=rounds; i++){
      lightColor(sequence[i]);
    }
    
    for(int i=0; i<=rounds; i++){
      int input = readInput();
    
      while(input == 5){
        input = readInput();
      }
    
      if (input == sequence[i]){
        lightColor(input);
      }else{
        lightColor(input);
        delay(200);
        for(int i=0; i<=rounds; i++){
          lightUpAll();
        }
      
        playGame = false;
        break;
      }
    }
    
    
    rounds++;
    
    if(rounds == 25){ //Change this number to determine how many rounds to win.
    
      for(int i=0; i<5; i++){
        for(int j=0; j<4; j++){
          digitalWrite(buzzPin, HIGH);
          digitalWrite(j+8, HIGH);
          delay(100);
          
          if(j >= 2){
            digitalWrite(buzzPin, LOW);
          }
        }
        for(int j=0; j<4; j++){
          digitalWrite(buzzPin, HIGH);
          digitalWrite(j+8, LOW);
          delay(100);
          
          if(j >= 2){
            digitalWrite(buzzPin, LOW);
          }
        }
      }
      playGame = false;
      break;
    }
  }
}

int readInput(){
  xPos = analogRead(xPin);
  yPos = analogRead(yPin);
  
  if(yPos <= 123){
    return 0;
  }else if(yPos >= 900){
    return 2;
  }else if(xPos <= 123){
    return 3;
  }else if(xPos >= 900){
    return 1;
  }else{
    return  5;
  }
    
}

void lightColor(int color) {
    if(color == 0){
      lightUp(8);
    }else if(color == 1){
      lightUp(9);
    }else if(color == 2){
      lightUp(10);
    }else {
      lightUp(11);
    }
}

void lightUp(int pin){
  digitalWrite(pin, HIGH);
  digitalWrite(buzzPin, HIGH);
  delay(200);
  digitalWrite(pin, LOW);
  digitalWrite(buzzPin, LOW);
  delay(500);
}

void lightUpAll(){
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, HIGH);
  digitalWrite(yelwPin, HIGH);
  digitalWrite(bluePin, HIGH);
  digitalWrite(buzzPin, HIGH);
  delay(200);
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(yelwPin, LOW);
  digitalWrite(bluePin, LOW);
  digitalWrite(buzzPin, LOW);
  delay(500);
}

Credits

goldingkenny

goldingkenny

0 projects • 1 follower

Comments