Simple Arduino program

Asked

Viewed 109 times

-1

I’m new to programming.... The program I’m trying to write to run on the Arduino, will command a circuit that has two buttons. The 1 button is to turn on an Led and it runs the sequence of flashes. The other, must turn off the whole system. The first button worked, but I can’t get the second button to turn off the LED. Can anyone help?

int led1 = 13;
int botao1 = 7;
int botao2 = 2;
int estadobotao1 = 0;
int estadobotao2 = 0;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(botao1, INPUT);
  pinMode(botao2, INPUT);

}

void loop()
{
estadobotao1 = digitalRead(botao1);

    if (estadobotao1 == HIGH) {
  while(estadobotao2 == LOW){ 

       digitalWrite(led1, HIGH);
       delay(300);
       digitalWrite(led1,LOW);
       delay(300);
       digitalWrite(led1, HIGH);
       delay(300);
       digitalWrite(led1,LOW);
       delay(300);
       digitalWrite(led1, HIGH);
       delay(500);
       digitalWrite(led1,LOW);
       delay(500);
       digitalWrite(led1, HIGH);
       delay(500);
       digitalWrite(led1,LOW);
       delay(500);
    estadobotao2 = digitalRead(botao2);
    }
        }
  if(estadobotao2 == HIGH){
    digitalWrite(led1,LOW);
  }
}

3 answers

0

I believe it is the exchange of WHILE for WHILE, any problem let me know:

int led1 = 13;
int botao1 = 7;
int botao2 = 2;
int estadobotao1 = 0;
int estadobotao2 = 0;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(botao1, INPUT);
  pinMode(botao2, INPUT);
}

void loop()
{
    estadobotao1 = digitalRead(botao1);
    if (estadobotao1 == HIGH) {
        do
        {
            digitalWrite(led1, HIGH);
            delay(300);
            digitalWrite(led1,LOW);
            delay(300);
            digitalWrite(led1, HIGH);
            delay(300);
            digitalWrite(led1,LOW);
            delay(300);
            digitalWrite(led1, HIGH);
            delay(500);
            digitalWrite(led1,LOW);
            delay(500);
            digitalWrite(led1, HIGH);
            delay(500);
            digitalWrite(led1,LOW);
            delay(500);
            estadobotao2 = digitalRead(botao2);
        }
        while(estadobotao2 == LOW);
    }
    if(estadobotao2 == HIGH)
    {
        digitalWrite(led1,LOW);
    }
}

0

While the program is running the code inside the while nothing will happen if you press the off button, the program will only quit while if you keep the 2 button pressed full time because in this case "stadobotao2 = digitalRead(botao2);" will update to HIGH. You could just put this 2 button in series to turn it all off. I also recommend to activate the pull up and put a delay to avoid the Bounce.

  • Good evening! Thanks for the reply.... I could not understand friend... The button 2 in series with whom?

-1

As it is already in the loop function, you can exchange this while for if. You could use an auxiliary variable to save the status of the button. Or if you want something more complex, look for interruption.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.