RGB LED random programming

Hello,
I've written a program where When a 'start' button is pressed, 5 random LEDs should light up in blue. The user then has to select the correct buttons for each LED that is lit up blue. When a button for an LED thats blue is clicked, the LED should them green when clicked. I've run this code and whenever I press the start button, nothing is happening. Would appreciate some help with this. By the way I'm using arduino mega.
Heres my code:

// Pin definitions
const int buttonPin[] = {40, 41, 42, 43, 44, 45, 46, 47, 48, 49}; // Array of button pins
const int ledGreenPin[] = {12, 9, 6, 3, 23, 26, 29, 32, 35, 38}; // Array of green LED pins
const int ledBluePin[] = {13, 10, 7, 4, 22, 25, 28, 31, 34, 37}; // Array of blue LED pins

const int startButtonPin = 50; // Start button pin

// Variables
int blueLEDs[5]; // Array to store indices of LEDs that will light up blue
int selectedButton = -1; // Variable to store the index of the selected button
bool gameStarted = false; // Flag to indicate if the game has started

void setup() {
  Serial.begin(9600);

  // Initialize LED and button pins
  for (int i = 0; i < 10; i++) {
    pinMode(ledGreenPin[i], OUTPUT);
    pinMode(ledBluePin[i], OUTPUT);
    digitalWrite(ledGreenPin[i], LOW);
    digitalWrite(ledBluePin[i], LOW);
    pinMode(buttonPin[i], INPUT_PULLUP);
  }

  pinMode(startButtonPin, INPUT_PULLUP);

  // Seed random number generator
  randomSeed(analogRead(0));
}

void loop() {
  if (!gameStarted && digitalRead(startButtonPin) == LOW) {
    startGame();
  }

  if (gameStarted) {
    // Check if any button is pressed
    for (int i = 0; i < 10; i++) {
      if (digitalRead(buttonPin[i]) == LOW) {
        selectedButton = i;
        break;
      }
    }

    // If a button is pressed, check if it corresponds to a blue LED
    if (selectedButton != -1) {
      for (int i = 0; i < 5; i++) {
        if (selectedButton == blueLEDs[i]) {
          // Button corresponds to a blue LED
          digitalWrite(ledGreenPin[selectedButton], LOW); // Turn off green LED
          digitalWrite(ledBluePin[selectedButton], HIGH); // Turn blue LED on
          delay(500); // Delay for visual feedback
          digitalWrite(ledBluePin[selectedButton], LOW); // Turn off blue LED
          selectedButton = -1; // Reset selected button
          break;
        }
      }
    }
  }
}

void startGame() {
  // Turn off all LEDs
  for (int i = 0; i < 10; i++) {
    digitalWrite(ledGreenPin[i], LOW);
    digitalWrite(ledBluePin[i], LOW);
  }
  
  // Generate 5 random blue LEDs
  for (int i = 0; i < 5; i++) {
    blueLEDs[i] = random(10);
    digitalWrite(ledBluePin[blueLEDs[i]], HIGH); // Turn blue LED on
  }

  gameStarted = true;
}

The LEDS are RGB LEDS

That's not what I see here:

          // Button corresponds to a blue LED
          digitalWrite(ledGreenPin[selectedButton], LOW); // Turn off green LED
          digitalWrite(ledBluePin[selectedButton], HIGH); // Turn blue LED on
          delay(500); // Delay for visual feedback
          digitalWrite(ledBluePin[selectedButton], LOW); // Turn off blue LED

If the button is one of the blue led, you here turn off the green (?), then turn blue on for half a second then turn it off. Based on your description it should be like:

          // Button corresponds to a blue LED
          digitalWrite(ledBluePin[selectedButton], LOW); // Turn off blue LED
          delay(500); // Delay for visual feedback
          digitalWrite(ledGreenPin[selectedButton], HIGH); // Turn on green LED

In this case, some serial debug printing could help (e.g. printing some variable value changes starting from "gameStarted" and "selectedButton").

I don't have time to create a simulation of your project at the moment, but if you add serial prints and post here both the "new" sketch together with the serial output, I think we can tell you more than this.

I had a few minutes, so I tested this code on Wokwi, and looks like ok, give it a try (check the first lines I added to make things easier to draw).

// To reduce the number of LEDs
const byte MAX_LED = 10; // Mine was 4
// Number of blue LEDs
const byte MAX_BLUE = 5; // Mine was 2

// Pin definitions
const int buttonPin[] = {40, 41, 42, 43, 44, 45, 46, 47, 48, 49}; // Array of button pins
const int ledGreenPin[] = {12, 9, 6, 3, 23, 26, 29, 32, 35, 38}; // Array of green LED pins
const int ledBluePin[] = {13, 10, 7, 4, 22, 25, 28, 31, 34, 37}; // Array of blue LED pins

const int startButtonPin = 50; // Start button pin

// Variables
int blueLEDs[5]; // Array to store indices of LEDs that will light up blue
int selectedButton = -1; // Variable to store the index of the selected button
bool gameStarted = false; // Flag to indicate if the game has started

void setup() {
  Serial.begin(9600);

  // Initialize LED and button pins
  for (int i = 0; i < MAX_LED; i++) {
    pinMode(ledGreenPin[i], OUTPUT);
    pinMode(ledBluePin[i], OUTPUT);
    digitalWrite(ledGreenPin[i], LOW);
    digitalWrite(ledBluePin[i], LOW);
    pinMode(buttonPin[i], INPUT_PULLUP);
  }

  pinMode(startButtonPin, INPUT_PULLUP);

  // Seed random number generator
  randomSeed(analogRead(0));

  Serial.println("RUNNING");
}

void loop() {
  if (!gameStarted && digitalRead(startButtonPin) == LOW) {
    startGame();
  }

  if (gameStarted) {
    // Check if any button is pressed
    for (int i = 0; i < MAX_LED; i++) {
      if (digitalRead(buttonPin[i]) == LOW) {
        selectedButton = i;
        break;
      }
    }

    // If a button is pressed, check if it corresponds to a blue LED
    if (selectedButton != -1) {
      for (int i = 0; i < MAX_BLUE; i++) {
        if (selectedButton == blueLEDs[i]) {
          // Button corresponds to a blue LED
          digitalWrite(ledBluePin[selectedButton], LOW); // Turn off blue LED
          delay(500); // Delay for visual feedback
          digitalWrite(ledGreenPin[selectedButton], HIGH); // Turn on green LED
          selectedButton = -1; // Reset selected button
          break;
        }
      }
    }
  }
}

void startGame() {
  // Turn off all LEDs
  for (int i = 0; i < MAX_LED; i++) {
    digitalWrite(ledGreenPin[i], LOW);
    digitalWrite(ledBluePin[i], LOW);
  }
  
  // Generate random blue LEDs
  for (int i = 0; i < MAX_BLUE; i++) {
    blueLEDs[i] = random(MAX_LED);
    digitalWrite(ledBluePin[blueLEDs[i]], HIGH); // Turn blue LED on
    Serial.print("Blue ");Serial.print(i); Serial.print("=");Serial.println(blueLEDs[i]);
  }

  gameStarted = true;
  Serial.println("STARTED");
}

PS: I couldn't share the Wokwi project because that site is pretty slow today, don't know why, and failed to save my project!

Ok, Wokwi seems to be back so here it is (this is a limited test with just 4 leds and buttons, and 2 blue ones, I'm too lazy to draw the full 10 LEDs and buttons...:wink: ):

It's almost the same code as your and it seems ok to me, so let me know why you said you had problems.

Hi, so there's cases where only one LED turns on and cases where when it turns on and the button is pressed, it doesnt turn green.

I'm sorry but that's not what I see on the simulator, there's no "random" behaviour in the code so I think you need to better explain what you mean with that "cases" (e.g. only one blue LED even if MAX_BLUE is 5?). I think you either haven't tried my code or it's more likely kinda hardware issue.

Anyway, to make some more tests please load my code (where you can see I also added some serial debug printouts) on your hardware and post here what you receive on Serial Monitor, together with a description of the actions you made.

I was able to figure it out and everything worked perfectly. I had to adjust the code a bit and redo my wiring but it all worked out. Thank you so much for your help!

I'm happy to see you solved. :sunglasses:

PS: if you want to help others understand (and do similar solutions if encounter similar issues) it's common and good practice to share here the "final" code you made.

Here's my final code:

//Total amount of LEDS
const byte MAX_LED = 10;
// Number of blue LEDs
const byte MAX_BLUE = 5;

// Pin definitions
const int buttonPin[] = {40, 41, 42, 43, 44, 45, 46, 47, 48, 49}; // Array of button pins
const int ledGreenPin[] = {12, 9, 6, 3, 23, 25, 27, 29, 31, 33}; // Array of green LED pins
const int ledBluePin[] = {13, 10, 7, 4, 22, 24, 26, 28, 30, 32}; // Array of blue LED pins

const int startButtonPin = 50; // Start button pin

// Variables
int blueLEDs[5]; // Array to store indices of LEDs that will light up blue
int selectedButton = -1; // Variable to store the index of the selected button

void setup() {
  Serial.begin(9600);

  // Initialize LED and button pins
  for (int i = 0; i < MAX_LED; i++) {
    pinMode(ledGreenPin[i], OUTPUT);
    pinMode(ledBluePin[i], OUTPUT);
    digitalWrite(ledGreenPin[i], LOW);
    digitalWrite(ledBluePin[i], LOW);
    pinMode(buttonPin[i], INPUT_PULLUP);
  }

  pinMode(startButtonPin, INPUT_PULLUP);

  // Seed random number generator
  randomSeed(analogRead(0));

  Serial.println("RUNNING");
}

void loop() {
  if (digitalRead(startButtonPin) == LOW) {
    startGame();
  }

  //if (gameStarted) {
    // Check if any button is pressed
    for (int i = 0; i < MAX_LED; i++) {
      if (digitalRead(buttonPin[i]) == LOW) {
        selectedButton = i;
        //break;
      }
   // }

    // If a button is pressed, check if it corresponds to a blue LED
    if (selectedButton != -1) {
      for (int i = 0; i < MAX_BLUE; i++) {
        if (selectedButton == blueLEDs[i]) {
          // Button corresponds to a blue LED
          digitalWrite(ledBluePin[selectedButton], LOW); // Turn off blue LED
          delay(500); // Delay for visual feedback
          digitalWrite(ledGreenPin[selectedButton], HIGH); // Turn on green LED
          selectedButton = -1; // Reset selected button
          //break;
        }
      }
    }
  }
}

void startGame() {
  // Turn off all LEDs
  for (int i = 0; i < MAX_LED; i++) {
    digitalWrite(ledGreenPin[i], LOW);
    digitalWrite(ledBluePin[i], LOW);
  }
  
  // Generate 5 random blue LEDs without repetition
  for (int i = 0; i < MAX_BLUE; i++) {
    bool isUnique;
    do {
      blueLEDs[i] = random(MAX_LED); // Pick a random LED
      isUnique = true;
      // Check if the LED is already chosen
      for (int j = 0; j < i; j++) {
        if (blueLEDs[j] == blueLEDs[i]) {
          isUnique = false;
          break;
        }
      }
    } while (!isUnique);
    digitalWrite(ledBluePin[blueLEDs[i]], HIGH); // Turn on the blue LED
  }

  Serial.println("STARTED");
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.