Randomly Lighting LED's, then turning them off

Hi, I'm kinda new to this stuff, doing it for a design project

Basically i have this set-up as pictured below, and am trying to get the LED's to light up randomly, then stay on for a given amount of time, then be turned off by its corresponding push-button. I also want to be to record if the button isnt pushed in the given time, and if the 'player/participant' fails 3 times, then its game-over.

So far i have this fairly simple code, and just need to try and advance it;

//BUTTON's
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 1; // the number of the pushbutton pin
const int buttonPin2 = 2;
const int buttonPin3 = 3;

//all 3 buttons together
const int maxbuttonpins = 3;
int buttonpin[maxbuttonpins] ;

//LED'S
const int REDLEDPIN1 = 11; // the number of the LED pin
const int REDLEDPIN2 = 12;
const int REDLEDPIN3 = 13;

//all 3 LEDs together
const int maxLedPins = 3;
int ledpins[maxLedPins] = {REDLEDPIN1, REDLEDPIN2, REDLEDPIN3};

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(REDLEDPIN1, OUTPUT);
pinMode(REDLEDPIN2, OUTPUT);
pinMode(REDLEDPIN3, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(REDLEDPIN1, LOW);
digitalWrite(REDLEDPIN2, LOW);
digitalWrite(REDLEDPIN3, LOW);

}
else {
// KEEP LED ON:
digitalWrite(REDLEDPIN1, HIGH);
digitalWrite(REDLEDPIN2, LOW);
digitalWrite(REDLEDPIN3, LOW);

}
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin2);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(REDLEDPIN1, LOW);
digitalWrite(REDLEDPIN2, LOW);
digitalWrite(REDLEDPIN3, LOW);

}
else {
// KEEP LED ON:
digitalWrite(REDLEDPIN1, LOW);
digitalWrite(REDLEDPIN2, HIGH);
digitalWrite(REDLEDPIN3, LOW);

}
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin3);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(REDLEDPIN1, LOW);
digitalWrite(REDLEDPIN2, LOW);
digitalWrite(REDLEDPIN3, LOW);

}
else {
// KEEP LED ON:
digitalWrite(REDLEDPIN1, LOW);
digitalWrite(REDLEDPIN2, LOW);
digitalWrite(REDLEDPIN3, HIGH);

}
}

Any help would be thoroughly appreciated

O'm

Some notes:
Don't use Pin 1 if you don't have to. Pins 0 and 1 are used for hardware Serial and are very handy for talking to the PC.

You did well putting the LED pin numbers in an array but you left the Button pin number array empty.

At the top of the loop() you might want to put the current button and led states into one number:

int buttons = 0;
int leds = 0;
for (int i=0; i<maxbuttonpins; i++)
    {
    if (digitalRead(buttonpin[i])
        buttons |= (1<<i);
    if (digitalRead(ledpins[i])
        leds |= (1<<i);
    }

Then you can do things like:
if (buttons == 0) // No buttons are pushed
if (leds == 0) // No LED's are lit
if (buttons == leds) // The button (or buttons) that correspond to the lit LED (or LEDS) are pressed
if ((buttons & leds) != 0) // At least one button corresponding to a lit LED is pressed

If no LED's are lit and no buttons are pressed you probably want to:
delay some random interval
pick an LED at random and turn it on

If an LED is lit and the corresponding button is not pressed, check the timer
if the timer has expired, turn off the LED and note the failure. Keep an array of fails per button.
If the number of fails has hit the limit, blink the LED for the failing user. GAME OVER

If an LED is lit and the corresponding button is pressed, turn off the LED (or turn them all off if that's easier)

You may want to check for buttons pressed that DON'T correspond to a lit LED and count those as failures because the other people should not be pressing their buttons. If you do that, make sure the person who just extinguished a light is not penalized for having the button still down after the light has gone off.

You have an array of LED pins. Why don't you use it?

Create functions for the various conditions you want the LEDs to represent (only LED 1 on, only 2 on, etc.) and call those functions.

am trying to get the LED's to light up randomly

Where? The code you posted does specific things in response to specific button presses. Randomly turning on LEDs is possible.

Should there be just one LED on at a time, or can there be multiple LEDs lit?

then stay on for a given amount of time

Look at the Blink Without Delay example.

then be turned off by its corresponding push-button.

Very easy.

I also want to be to record if the button isnt pushed in the given time

The Blink Without Delay example will get you going in the right direction.

and if the 'player/participant' fails 3 times, then its game-over.

Since the Arduino is always doing something, this gets a bit trickier. Counting the number of fails is easy. Stopping turning on the LEDs is easy. Doing nothing is easy.

Resuming doing something will probably require another switch or creative use of the switches that you already have (think ctrl-alt-delete).

ok thanks guys, will try them out and get back to u if i have any more problems

Blinking LEDs randomly.....
My VBP way (Very Bad Programming).
It does the trick.

/*
  BlinkRandom
  For a 3x3 LED matrix with "rainbowLEDs"
  Turns on a random LED on for some time, then some other random LED (can even be the same) off for some time, repeatedly.
   */
 
void setup() {                
  // initialize the digital pins as an output.
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
 
}

void loop() {
  digitalWrite(random(5,14), HIGH);   // sets a random LED on
  delay(random(100,3000));              // wait for some time (between 0.1 and 3 seconds)
  digitalWrite(random(5,14), LOW);    // sets a random LED off
  delay(random(10,3000));              // wait for some time (between 0.1 and 3 seconds)
}

All you have to add is a way to read a button to stop it all.....
(I think.....)

I am very close to getting it right now, and have the folllowing code

void loop() {
//turn off all LEds
  for (int i=0; i < maxledPins; i++) {
   digitalWrite (ledPins[i], LOW);
}

// turn random LED on  
digitalWrite(ledPins[random(maxledPins)], HIGH); 

//calulate time since last loop was completed
unsigned long loopTime = millis() - startTime; 

//if loop time is more than 500 then record it as an error
if (loopTime > 500){
  buttonFAILcounter ++; 
      Serial.println("on");
      Serial.print("fails:  ");
      Serial.println(buttonFAILcounter, DEC); 
      Serial.println("off"); 
}

//if button1 is pushed when led1 is on, go back to the begining
if (ledPin1 == HIGH && buttonPin1 == HIGH) {
   int maxledPins = 0;
   int maxbuttonPins = 0;
}

//otherwise log it as a mistake
else { 
      buttonFAILcounter ++; 
      Serial.println("on");
      Serial.print("fails:  ");
      Serial.println(buttonFAILcounter, DEC);
      Serial.println("off"); 
}

//if button2 is pushed when led2 is on, go back to the begining
if (ledPin2 == HIGH && buttonPin2 == HIGH) {
   int maxledPins = 0;
   int maxbuttonPins = 0;
}

//otherwise log it as a mistake
else { 
      buttonFAILcounter ++; 
      Serial.println("on");
      Serial.print("fails:  ");
      Serial.println(buttonFAILcounter, DEC);
      Serial.println("off"); 
}
//if button3 is pushed when led3 is on, go back to the begining
if (ledPin3 == HIGH && buttonPin3 == HIGH) {
   int maxledPins = 0;
   int maxbuttonPins = 0;
}

//otherwise log it as a mistake
else { 
      buttonFAILcounter ++; 
      Serial.println("on");
      Serial.print("fails:  ");
      Serial.println(buttonFAILcounter, DEC);
      Serial.println("off"); 
      
      }

startTime = millis();

if (buttonFAILcounter % 3 == 0) {
  (maxledPins, LOW);
  delay (500000);
}
 }

I am basically looking for the LED's to be turned on randomly, then only be turned off by their corresponding button. if they dont press the button within the alotted time it goes down as a fail, and if they press the wrong one it goes down as a fail.

Any help truely appreciated
thnks Ao'm

I don't understand what you are trying to accomplish with:

   int maxledPins = 0;
   int maxbuttonPins = 0;

Your comment says "go back to the beginning" but loop() will do that. Setting "maxledPins" to zero is going to mess up the code that relies on that count:

//turn off all LEds
  for (int i=0; i < maxledPins; i++) {
   digitalWrite (ledPins[i], LOW);
}

// turn random LED on  
digitalWrite(ledPins[random(maxledPins)], HIGH);

Those bits of code will never work again once maxledPins is set to zero.

You can save a lot of redundant code by using the arrays and loops we told you about earlier, rather then having separate code for the three buttons.

Your timer is not going to work properly if you set the start time at the bottom of the loop. Each time through the loop will take only a short amount of time and your timer will never reach 1/2 second.

If you want to keep the led on till the button is pushed or the time expires you should not start the loop() with "turn off all the LEDs".

If you want a single LED to stay on till the button is pushed or the time expires you should not light a random LED every time through the loop.

ok thnks again,

If you want a single LED to stay on till the button is pushed or the time expires you should not light a random LED every time through the loop.

so if i dont use a random LED every time what should i use? because an array would bring them on in a certain order every loop, whether as i want it kinda random

cheers

Ao'm