UNO loop nested if else while

Hi there. i'm using arduino uno and i found that the code below does not work. Seems like "if" do not work inside "if" statements. it is working when i push the first button. but when i push the second button it seem does not read the command. how do i solve this problem dear community.

while(ButtonStateAUTO==HIGH)
  {
    {
      Serial.println("auto");
      delay(1);

      if(ButtonStateMAN==HIGH)
        while(0)
        {
          Serial.println("manauto");
          delay(1000);
        }
      else 
      { 
        Serial.println("mode?");
        delay(1);
      }

    }
  }

First, use CTRL-T to autoformat that and make it readable, then Modify your post, use code tags (the # button), and repost the code.

If's do work inside if's. That code is missing anything to read the buttons, so it's hard to comment further.

This code (assuming there is digitalRead somewhere) would detect the first button (ButtonStateMAN) only if it is pressed (and held) before pressing the second (ButtonStateAUTO).
Once the ButtonStateAUTO is detected to be pressed, no more checks are made to see if the ButtonStateMAN is held. The only thing being checked is the value of the previous check which may or may not be still relevant.

Shpaget:
This code (assuming there is digitalRead somewhere) would detect the first button (ButtonStateMAN) only if it is pressed (and held) before pressing the second (ButtonStateAUTO).
Once the ButtonStateAUTO is detected to be pressed, no more checks are made to see if the ButtonStateMAN is held. The only thing being checked is the value of the previous check which may or may not be still relevant.

ya.. probably. because if I push 2nd button followed by 1st button, it did read the command, but not as i want. it came out "auto","manauto" repeatedly. how i solve this problem.

        while(0)
        {
          Serial.println("manauto");
          delay(1000);
        }

This loop will never print anything because while(0)will never be true.

Also, the first and last curly braces in your code are not necessary.

CrossRoads:
First, use CTRL-T to autoformat that and make it readable, then Modify your post, use code tags (the # button), and repost the code.

If's do work inside if's. That code is missing anything to read the buttons, so it's hard to comment further.

i'm sorry my mistakes. i already modified the post. Is it okay? then can you help me to solve the problem? thanks

UKHeliBob:

        while(0)

{
          Serial.println("manauto");
          delay(1000);
        }


This loop will never print anything because `while(0)`will never be true.

Also, the first and last curly braces in your code are not necessary.

Trust me. I had tried everything. Even i'm using if...else also it cannot read the command after i push the second button. Any solution? Thanks :slight_smile:

It's time for you to post all of your program and explain how things are wired. Pull up/down resistors for instance.

Yeah, entire code and schematic would help, but in the meantime...
This is quick and dirty way to do it. I haven't tested it with buttons but it should work.

if (ButtonStateAUTO == HIGH){
  for (int i = 0; i  < 1000; i++){
    delay (2);          //This delay gives you a window of about 2 seconds to push the second button.
    ButtonStateMAN = digitalRead (button2);
    if (ButtonStateMAN == HIGH){
      Serial.println("manauto");
      delay (1000);    //This delay is here because you had it in your posted code
      break;          //Once the ButtonStateMAN is detected to be pressed the for loop is exited and ButtonStateMAN is no longer checked
    }
  }
}

Shpaget:
Yeah, entire code and schematic would help, but in the meantime...
This is quick and dirty way to do it. I haven't tested it with buttons but it should work.

if (ButtonStateAUTO == HIGH){

for (int i = 0; i  < 1000; i++){
   delay (2);          //This delay gives you a window of about 2 seconds to push the second button.
   ButtonStateMAN = digitalRead (button2);
   [code]if (ButtonStateMAN == HIGH){
     Serial.println("manauto");
     delay (1000);    //This delay is here because you had it in your posted code
     break;


//Once the ButtonStateMAN is detected to be pressed the for loop is exited and ButtonStateMAN is no longer checked
}
}
}[/code]

THANKS A LOT.... :slight_smile: the code is working. i just need to add while(1) here

if (ButtonStateMAN == HIGH){
while(1)
      Serial.println("manauto");
      delay (1000);    //This delay is here because you had it in your posted code
      break;

...to make sure it does not read the upper command line (it loops forever inside the if statements)

THANKS TO ALL COMMUNITIES AS WELL. =)