Simon Says RED/YELLOW/GREEN

This project is combining the first assignment with the second. The switch is activated by attaching a piece of foil on the bottom of my foot, which is attached to wire that is attached to 5V. Each of the colored foils will light up a colored LED if a foot steps on it.

The main goal of the project is to create a game where you copy exactly what the program does. There are 5 levels to this game and the game gets harder as you progress. The program will light up a random LED and when it finishes displaying the pattern and the player (you) will have to step on the correct colored tin foil in order to pass the round. The first round will start with one color pattern, then 2 colors if you pass, and so on and so forth. If you step on the incorrect color, you have failed and the game will restart.

Going into this project, I had trouble because the program would jump ahead and think that I’ve already finished the pattern when in fact, I haven’t. It was fixed by adding a delay and adding conditions to check whether the pattern has been stepped on already.


int level = 1;
int pattern[5];
int prevRead[] = {LOW, LOW, LOW};

void setup() {
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  Serial.begin(9600);
}

void loop() {
    for (int i = 0; i < level; i++) {
      int color = random(3, 6);
      digitalWrite(color, HIGH);
      delay(500);
      digitalWrite(color, LOW);
      delay(500);
      pattern[i] = color;
    }

  for (int i = 0; i < level; i++) {
    Serial.println(pattern[i]);
  }

  Serial.println("======");

  int i = 0;
  while (i < level) {
    bool prevAllClear = true;
    for (int pin = 0; pin < 3; pin++) {
      if (prevRead[pin] == HIGH) {
        prevAllClear = false;
      }
    }

    bool currentTouch = false;
    for (int pin = 0; pin < 3; pin++) {
      prevRead[pin] = digitalRead(pin + 3);
      if (prevRead[pin] == HIGH) 
        currentTouch = true;
    }

    if (prevAllClear && currentTouch) {
      if (digitalRead(pattern[i]) == HIGH) {
        i++;
        Serial.println("good job!");
      } else {
        level = 0;
        Serial.println("wrong!");
      }
    } 

    for (int pin = 0; pin < 3; pin++) {
      prevRead[pin] = digitalRead(pin + 3);
    }

    delay(100);
  }

  delay(500);

  digitalWrite(6, HIGH);
  delay(1000);
  digitalWrite(6, LOW);

  level++;
  
  if (level == 6) {
    while (1) {
      digitalWrite(3, HIGH);
      delay(200);
      digitalWrite(3, LOW);

      digitalWrite(4, HIGH);
      delay(200);
      digitalWrite(4, LOW);

      digitalWrite(5, HIGH);
      delay(200);
      digitalWrite(5, LOW);

      digitalWrite(6, HIGH);
      delay(200);
      digitalWrite(6, LOW);
    }
  }
}

 

Leave a Reply