Close

Mostly done! *Victory Fanfare*

A project log for Simon Says w/ Arduino Uno R4

Developing Simon Says game with 0 libraries!

eric-byrdEric Byrd 09/26/2025 at 07:500 Comments

This is just a quick late night/early morning update.

 I've successfully completed the project. It's currently 12:46 AM and I am pretty tired. A few adjustments were made with the logic in my code. For example, I realized that my seed value was only getting generated once because it was outside of my for loop. A silly mistake that I can work towards improving over time. That being said, conceptually, I feel like this project gave me a better understanding of how PRNGs are generated and that understanding will take me quite a long way with my future projects. Here is an uncommented, rough draft of my working code. I plan on cleaning things up and optimizing at a later date.

const int BUZZER_PIN = 3;

//Input for Red Button PIN
const int RED_BUTTON_PIN = 5;
const int GREEN_BUTTON_PIN = 4;
const int YELLOW_BUTTON_PIN = 6;

//External output for Red LED
const int RED_LED_PIN = 11;
const int GREEN_LED_PIN = 12;
const int YELLOW_LED_PIN = 13;

void setup() {
  // initialize output pins
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(YELLOW_LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  // initialize input pins
  pinMode(RED_BUTTON_PIN, INPUT);
  pinMode(GREEN_BUTTON_PIN, INPUT);
  pinMode(YELLOW_BUTTON_PIN, INPUT);

  //Initialize Serial Baud rate to 9600
  Serial.begin(9600);
}

int XORShift(int x){
  x ^= x << 13;
  x ^= x >> 17;
  x ^= x << 5;

  return x;
}

void VictoryFanfare(){
    digitalWrite(RED_LED_PIN, HIGH);
    tone(BUZZER_PIN, 523.25, 133);
    delay(133);
    digitalWrite(GREEN_LED_PIN, HIGH);
    tone(BUZZER_PIN, 523.25, 133);
    delay(133);
    digitalWrite(YELLOW_LED_PIN, HIGH);
    tone(BUZZER_PIN, 523.25, 133);
    delay(133);
    tone(BUZZER_PIN, 523.25, 400);
    delay(400);
    tone(BUZZER_PIN, 415.30, 400);
    delay(400);
    tone(BUZZER_PIN, 466.16, 400);
    delay(400);
    digitalWrite(YELLOW_LED_PIN, LOW);
    digitalWrite(GREEN_LED_PIN, LOW);
    digitalWrite(RED_LED_PIN, LOW);  
    tone(BUZZER_PIN, 523.25, 133);
    delay(133);
    delay(133);
    tone(BUZZER_PIN, 466.16, 133);
    delay(133);
    tone(BUZZER_PIN, 523.25, 1200);
    delay(1200);
  }

// the loop function runs over and over again forever
void loop() {
  const int maxLen = 4;
  int simonOutput[maxLen];
  int simonInput[maxLen];
  bool nextTurn;
  bool match = false;
  int i, j, k;

  Serial.println("-----output start-----");\
  for (i = 0; i < maxLen; i++)
  {
    int randomNum = XORShift(analogRead(A0)) % 3;
    switch(randomNum){
      //Red = 0
      case 0:
        simonOutput[i] = randomNum;
        digitalWrite(RED_LED_PIN, HIGH);
        tone(BUZZER_PIN, 310);
        Serial.print(simonOutput[i]);
        delay(150);
        noTone(BUZZER_PIN);
        digitalWrite(RED_LED_PIN, LOW);
        delay(250);
        break;
      case 1:
      simonOutput[i] = randomNum;
        digitalWrite(GREEN_LED_PIN, HIGH);
        tone(BUZZER_PIN, 415);
        Serial.print(simonOutput[i]);
        delay(150);
        noTone(BUZZER_PIN);
        digitalWrite(GREEN_LED_PIN, LOW);
        delay(250);
        break;
      case 2:
      simonOutput[i] = randomNum;
        digitalWrite(YELLOW_LED_PIN, HIGH);
        tone(BUZZER_PIN, 252);
        Serial.print(simonOutput[i]);
        delay(150);
        noTone(BUZZER_PIN);
        digitalWrite(YELLOW_LED_PIN, LOW);
        delay(250);
        break;
    }
  }
  Serial.println("-----output end-----");
  
Serial.println("-----input start-----");
  for(j = 0; j < maxLen; j++){
    nextTurn = false;
    while (nextTurn == false){
      if(digitalRead(RED_BUTTON_PIN) == HIGH){
        simonInput[j] = 0;
        Serial.print(simonInput[j]);
        digitalWrite(RED_LED_PIN, HIGH);
        tone(BUZZER_PIN, 310);
        delay(150);
        noTone(BUZZER_PIN);
        digitalWrite(RED_LED_PIN, LOW);
        delay(300);
        nextTurn = true;
      }
      if(digitalRead(GREEN_BUTTON_PIN) == HIGH){
        simonInput[j] = 1;
        Serial.print(simonInput[j]);
        digitalWrite(GREEN_LED_PIN, HIGH);
        tone(BUZZER_PIN, 415);
        delay(150);
        noTone(BUZZER_PIN);
        digitalWrite(GREEN_LED_PIN, LOW);
        delay(300);
        nextTurn = true;
      }
      if(digitalRead(YELLOW_BUTTON_PIN) == HIGH){
        simonInput[j] = 2;
        Serial.print(simonInput[j]);
        digitalWrite(YELLOW_LED_PIN, HIGH);
        tone(BUZZER_PIN, 252);
        delay(150);
        noTone(BUZZER_PIN);
        digitalWrite(YELLOW_LED_PIN, LOW);
        delay(300);
        nextTurn = true;
      }
    }
  }
  Serial.println("-----input end-----");

  for(k = 0; k < maxLen; k++){
    if (simonOutput[k] == simonInput[k]){
      match = true;
    }
    else{
      match = false;
      break;
    }
  }
  if (match == false){
    Serial.println("SORRY, INCORRECT MATCH");
    tone(BUZZER_PIN, 44);
    delay(1000);
  }
  else{
    Serial.println("CORRECT MATCH! YOU WIN!");
  // Phrase 1
  tone(BUZZER_PIN, 523.25, 133);
  VictoryFanfare();
  }
}

Discussions