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?
intbotao = 7;
intled = 13;
boolestadoLed = 0;
voidsetup()
{
pinMode(botao, INPUT_PULLUP); // define o pino do botao como entrada "INPUT"
pinMode(led, OUTPUT);
}
voidloop()
{
if(digitalRead(botao) == LOW) // Se o botão for pressionado
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);
}
}
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