header
Connect Two LEDs

Connect two LEDs to pins 6 and 7 on our board. Be sure to also connect a resistor and ground (GND).

connect two leds

The first step in our game is to blink the leds in a random order, so let's write the code to do that.

// Use arrays to hold our pin numbers and the pattern.
// This will let us use a loop to go through the values.
int ledPin[] = { 67 };
int pattern[5];  // This array will hold the pattern we generate.

void setup() {
  pinMode(ledPin[0], OUTPUT);
  pinMode(ledPin[1], OUTPUT);
}

void loop() {
  // Generate random pattern
  for (int i = 0i < 5i++) {
    int number = random(0,2);
    pattern[i] = number;
    digitalWrite(ledPin[number], HIGH);
    delay(500);
    digitalWrite(ledPin[number], LOW);
    delay(500);
  }
  
  // Wait two seconds and replay the pattern to make sure it's right
  delay(2000);
  for (int j = 0j < 5j++) {
    digitalWrite(ledPin[pattern[j]], HIGH);
    delay(500);
    digitalWrite(ledPin[pattern[j]], LOW);
    delay(500);
  }
  delay(2000);
}

After uploading the program, the two leds should randomly blink five times. After waiting two seconds, the pattern should repeat. This is us checking that we've stored the values correctly in our pattern[] array. However, there is a problem. Press the reset button on the Arduino and watch the pattern. Then, press it again and check the pattern again. Are they the same or different? If the patterns are always the same, might this be a problem for our game?

Look at the reference page for the random command. In the Note section, our exact problem is described. And they offer a fix. We simply need to add a randomSeed command on an unconnected pin. So add that to our code and see if that fixes things.

// Use arrays to hold our pin numbers and the pattern.
// This will let us use a loop to go through the values.
int ledPin[] = { 67 };
int pattern[5];  // This array will hold the pattern we generate.

void setup() {
  pinMode(ledPin[0], OUTPUT);
  pinMode(ledPin[1], OUTPUT);
  randomSeed(analogRead(0));  // Ensures that pattern is different at every reset
}

void loop() {
  // Generate random pattern
  for (int i = 0i < 5i++) {
    int number = random(0,2);
    pattern[i] = number;
    digitalWrite(ledPin[number], HIGH);
    delay(500);
    digitalWrite(ledPin[number], LOW);
    delay(500);
  }
  
  // Wait two seconds and replay the pattern to make sure it's right
  delay(2000);
  for (int j = 0j < 5j++) {
    digitalWrite(ledPin[pattern[j]], HIGH);
    delay(500);
    digitalWrite(ledPin[pattern[j]], LOW);
    delay(500);
  }
  delay(2000);
}

Now that we have our random pattern generator working, let's take out our replaying of the pattern and make our program easier to read. We are going to create a function called generate_pattern and move the code to it. Your code should look like this:

// Use arrays to hold our pin numbers and the pattern.
// This will let us use a loop to go through the values.
int ledPin[] = { 67 };
int pattern[5];  // This array will hold the pattern we generate.

void setup() {
  pinMode(ledPin[0], OUTPUT);
  pinMode(ledPin[1], OUTPUT);
  randomSeed(analogRead(0));  // Ensures that pattern is different at every reset
}

void loop() {
  generate_pattern();
}

void generate_pattern() {
  // Generate random pattern
  for (int i = 0i < 5i++) {
    int number = random(0,2);
    pattern[i] = number;
    digitalWrite(ledPin[number], HIGH);
    delay(500);
    digitalWrite(ledPin[number], LOW);
    delay(500);
  }
}

You'll notice that the pattern appears to just keep going. Technically, it is just looping over the generating of the pattern, but we can't see that because we don't know when the game is starting. So, let's fix that now. We'll play a short tone to start the game.

Step 2. Play a start tone

Game Home