Random time delay for on led

Need help.

It is necessary to create a code to turn on a led by button, but after pressing the button the led should light up in the time interval (200 to 3000) 0,2 sec a 3 sec

how do I use the RANDOM function in this code?

int botao = 7;

int led = 13;

bool estadoLed = 0;

void setup()

{

pinMode(botao, INPUT_PULLUP); // define o pino do botao como entrada "INPUT"

pinMode(led, OUTPUT);

}

void loop()

{

if (digitalRead(botao) == LOW) // Se o botão for pressionado

{

estadoLed = !estadoLed; // troca o estado do LED

digitalWrite(led, estadoLed);

while (digitalRead(botao) == LOW);

delay(100);

}

}

    digitalWrite(LEDPin, HIGH);
    delay(random(200, 3001));
    digitalWrite(LEDPin, LOW);

I'm sorry, it still keeps giving error. could you send the complete code?
Thanks a lot for the help

Please post your sketch

When is our assignment due to be handed in ?

Sorry, I left out a ')'. Try it now.

int botao = 7;
int led = 13;
bool estadoLed = 0;

void setup()
{
pinMode(botao, INPUT_PULLUP); // define o pino do botao como entrada "INPUT"
pinMode(led, OUTPUT);
}

void loop()
{
if(digitalRead(botao) == LOW) // Se o botão for pressionado
{
estadoLed = !estadoLed; // troca o estado do LED
digitalWrite(led, estadoLed);
while(digitalRead(botao) == LOW);
delay(100);
}
}

const int BotaoPin = 7;

void setup()
{
  pinMode(BotaoPin, INPUT_PULLUP); // define o pino do BotaoPin como entrada "INPUT"
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  while (digitalRead(BotaoPin) == HIGH);
  
  digitalWrite(LED_BUILTIN, HIGH);
  delay(random(200, 3001));
  digitalWrite(LED_BUILTIN, LOW);
}
1 Like

THX :slight_smile:

Good afternoon,
Sorry again.
I tried to make the system difficult but it keeps giving an error. could you help me again?

const int Botao1 = 7;
const int Botao2 = 4;
const int Botao3 = 2;
const int led1 = 13;
const int led2 = 12;
const int led3 = 8;

void setup()
{
pinMode(Botao1, INPUT_PULLUP); // define o pino do BotaoPin como entrada "INPUT"
pinMode(Botao2, INPUT_PULLUP);
pinMode(Botao3, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}

void loop()
{
while(digitalRead(Botao1) == HIGH);
delay(random(200, 3000));
digitalWrite(led1, HIGH);
delay(400);
digitalWrite(led1, LOW);
delay(4000);
//if(digitalRead(Botao2) == HIGH);
//delay(random(200, 3000));
//digitalWrite(led2, HIGH);
//delay(400);
// digitalWrite(led2, LOW);
// delay(4000);
//else(digitalRead(Botao3) == HIGH);
//delay(random(200, 3000));
//digitalWrite(led3, HIGH);
// delay(400);
// digitalWrite(led3, LOW);
// delay(4000);
}

It compiles without error for me.

If you uncomment the new code it will wait until button1 is pressed, blink on led1, then wait until button2 is pressed. The buttons will ONLY work in order because this line means "Wait until button1 is pressed":
while (digitalRead(Botao1) == HIGH) ;

If you want to be able to press buttons in any order, use

  if (pressed)
  {
    // do stuff
  }

instead of

while (notPressed) ;  // Wait for press
// do stuff once the wait is over
void loop()
{
  if (digitalRead(Botao1) == LOW)
  {
    delay(random(200, 3000));
    digitalWrite(led1, HIGH);
    delay(400);
    digitalWrite(led1, LOW);
    // delay(4000);
  }

  if (digitalRead(Botao2) == LOW)
  {
    delay(random(200, 3000));
    digitalWrite(led2, HIGH);
    delay(400);
    digitalWrite(led2, LOW);
    // delay(4000);
  }

  if (digitalRead(Botao3) == LOW)
  {
    delay(random(200, 3000));
    digitalWrite(led3, HIGH);
    delay(400);
    digitalWrite(led3, LOW);
    // delay(4000);
  }
}
1 Like

@victor88srocha

CODE TAGS please

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