Simon Says

Simon Says

A fun little game to help banish boredom

  • 4 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

We all get bored once in a while. Have you ever wanted to find something fun to do when you find yourself with nothing to do? This game tried to help eliminate boredom by creating a fun, entertaining game of Simon Says. Next time you find yourself with nothing to do, this game will help fix the situation.

Code

CodeArduino
#include <LiquidCrystal.h> //so that the LCD display can be used

LiquidCrystal lcd(1, 2, 3, 4, 5, 6); //setup LCD display

int level = 0; //game level being played
int gameLights[100]; //array of integers that represent which LEDs should be lit up
int playerLights[100]; //array of integers to represent the LEDs and the order the player presses them
int speedLights = 1000; //difficulty of the level
int flag = 0; //keeps track of if there is an input for the simon says game
int game = 0; //if game is still being played
int input = 0; //count on LCD screen for number of times buttons are pressed

/*
 * setup - assign input and output ports
 */
void setup(){
  lcd.begin(16, 2); //setup LCD's number of columns and rows
  pinMode(10, INPUT); //blue LED button
  pinMode(11, INPUT); //red LED button
  pinMode(12, INPUT); //white LED button
  pinMode(13, INPUT); //start button
  pinMode(7, OUTPUT); //blue LED
  pinMode(8, OUTPUT); //red LED
  pinMode(9, OUTPUT); //white LED
}

/*
 * lightsTest - if buttons are pressed, LEDs will light up and input will go up
 */
void lightsTest(){
  lcd.clear();
  if (input == 0){
    lcd.print("Press to start");
  }
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  if (digitalRead(10) == LOW){
    digitalWrite(7, LOW);
  }
  else{
    digitalWrite(7, HIGH);
    input = input + 1;
  }
  if (digitalRead(11) == LOW){
    digitalWrite(8, LOW);
  }
  else{
    digitalWrite(8, HIGH);
    input = input + 1;
  }
  if (digitalRead(12) == LOW){
    digitalWrite(9, LOW);
  }
  else{
    digitalWrite(9, HIGH);
    input = input + 1;
  }
  if (digitalRead(13) == HIGH){
    lcd.clear();
    lcd.print("BEGIN");
  }
}

/*
 * randomSequence - generates random numbers to fill gameLights with which LEDs should be lit
 */
void randomSequence(){
  randomSeed(millis());
  for (int i = 0; i < 100; i++){
    gameLights[i] = random(7,10);
  }
}

/*
 * displayLights - displays the lights for each level
 */
void displayLights(){
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  for (int i = 0; i < level; i++){
    digitalWrite(gameLights[i], HIGH);
    delay(speedLights);
    digitalWrite(gameLights[i], LOW);
    delay(300);
  }
}

/*
 * gameOver - if the input given by the player is incorrect, lights will flash and the game will end
 */
void gameOver(){
  for (int i = 0; i < 5; i++){
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    delay(400);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    delay(300);
    lcd.clear();
    lcd.print("Game Over");
  }
  level = 1;
  speedLights = 1000;
}

/*
 * waitForInput - wait for the player to insert a sequence of lights
 */
void waitForInput(int x){
  if (digitalRead(10) == LOW){
        digitalWrite(7, LOW);
      }
      else{
        digitalWrite(7, HIGH);
        playerLights[x] = 7;
        flag = 1;
      }
      if (digitalRead(11) == LOW){
        digitalWrite(8, LOW);
      }
      else{
        digitalWrite(8, HIGH);
        playerLights[x] = 8;
        flag = 1;
      }
      if (digitalRead(12) == LOW){
        digitalWrite(9, LOW);
      }
      else{
        digitalWrite(9, HIGH);
        playerLights[x] = 9;
        flag = 1;
      }
}

/*
 * lights - runs waitForInput on loop throughout the playerLights array
 */
void lights(){
  lcd.clear();
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  for (int i = 0; i < level; i++){
    if (playerLights[i] == 1){
      waitForInput(i);
    }
    else{
      if (playerLights[i] != gameLights[i]){
        gameOver();
      }
    }
  }
}

    /*while (flag = 0){
      if (digitalRead(10) == LOW){
        digitalWrite(7, LOW);
      }
      else{
        digitalWrite(7, HIGH);
        playerLights[i] = 7;
        flag = 1;
      }
      if (digitalRead(11) == LOW){
        digitalWrite(8, LOW);
      }
      else{
        digitalWrite(8, HIGH);
        playerLights[i] = 8;
        flag = 1;
      }
      if (digitalRead(12) == LOW){
        digitalWrite(9, LOW);
      }
      else{
        digitalWrite(9, HIGH);
        playerLights[i] = 9;
        flag = 1;
      }
    }*/

/*
 * goodGame - continue the game at the end of the level or end it if level = 100
 */
void goodGame(){
  if (level < 100){
    level++;
    if (speedLights >= 250){
      speedLights -= 50;
    }
  }
  if (level == 100){
    lcd.clear();
    lcd.print("You won!");
    for (int i = 0; i < 20; i++){
      digitalWrite(7, HIGH);
      delay(100);
      digitalWrite(7, LOW);
      delay(100);
      digitalWrite(8, HIGH);
      delay(100);
      digitalWrite(8, LOW);
      delay(100);
      digitalWrite(9, HIGH);
      delay(100);
      digitalWrite(9, LOW);
      delay(100);
    }
    return;
  }
}

/*
 * loop - main code
 */
void loop(){
  lightsTest();  
  if (input != 0){
    lcd.clear();
    lcd.print(input);
    delay(100);
  }
  if (digitalRead(13) == HIGH){
    input = 0;
  }
}

  //lights();
  //if (game = 0){
  /*  if (level == 0){
      randomSequence();
      lcd.clear();
      lcd.print("Press to play!");
    }
    if (digitalRead(13) == HIGH && level == 0){
      level++;
      lcd.clear();
    }
    if (level != 0){
      lcd.clear();
      lcd.print("Level");
      lcd.setCursor(0, 1);
      lcd.print(level);
      displayLights();
      lights();
    }*/
  //}
  /*if (game = 1){
    lcd.clear();
    lcd.print("Game Over");
    game = 0;
  }*/
  /*if (level != 0){
    lcd.clear();
    lcd.print("Level");
    lcd.setCursor(0, 1);
    lcd.print(level);
    delay(100);
    displayLights();
    level++;
  }
  if (digitalRead(13) == HIGH || level != 1){
    lcd.clear();
    lcd.print("Level");
    lcd.setCursor(0, 1);
    lcd.print(level);
    delay(100);
    displayLights();
    waitPlayer();
  }*/

Schematics

Schematics
Screen shot 2018 04 23 at 11 10 00 am tsywgq2t9v

Comments

Add projectSign up / Login