Turning a led with a random time

Hi, i'm having some problem programming the arduino.

So this is how i want it to work: You turn it on, a time passes, then a led turns on, you push the button and a lcd shows you how long you were pressing the button.
Now the problem is that i want it to turn on on a random time, from like 3 seconds to 12 seconds. Any help would be great :slight_smile:

I am a beginner with Arduino so take this with a grain of salt, but you could try this:

void setup() {
   readyToStart = true;
}

void loop() {
   if(readyToStart) { //Ready to turn the light on
      readyToStart = false;
      delay(random(3000, 12000)); //Delay a random number of seconds
      digitalWrite(LEDPIN, HIGH);
   }
}

Note that this is just an idea. There is probably a better way to do this.

need a LOW in there too :slight_smile:

Thanks, here is my program if anyone needs:

const int buttonPin = 6; // Digital pin that button is connected
const int ledPin1 = 4; // Digital pin that the second led is connected
const int ledPin = 5; // Digital pin that Led is connected

// variables will change:
int buttonState = 0; // variable for the button

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin, INPUT);
}

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

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
digitalWrite(ledPin, HIGH); // The led1 is on
if (buttonState == HIGH) { // if pushed then:
// turn LED on:

delay(random(3000, 12000)); // random time
digitalWrite(ledPin, LOW); // the led1 is off
digitalWrite(ledPin1, HIGH); // the led2 is on
delay (2000); // delay so you can see the light
digitalWrite(ledPin1, LOW); // off
}
else {
// turn LED off:
digitalWrite(ledPin, LOW); // if not pushed
digitalWrite(ledPin1, LOW); // -II-
}
}

now i was wondering how i would count the seconds it takes for you to push, and display it on the serial monitor or even on a lcd :slight_smile:

To calculate the time it takes you to push the button do something like

int start = millis();

// A bunch of other code until you get to the stop

int end = millis();

int total_time = end - start(); // This gives millis total between start and stop.

millis() returns an unsigned long, not an int. So, adjust accordingly.

could you give me a example using "int total_time = end - start();" and the other code