Thread: second "if" loop doesn't stop looping.

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20

    second "if" loop doesn't stop looping.

    Hi Friends,

    Working on my project here, decided to add a button to get into setup, and use the two others to go up and down. It's easier. But now I run these two if loops, the first one works fine, one click gives me one increment, but as soon as I add the down counter, it just keeps on ticking down. No matter if the button is HIGH or LOW, and if I push the + button, the counter goes up one and back down and down.

    I checked the wires, buttons work fine, 0v and 5v for both, the button2 is LOW.

    Code:
     void loop()
     {
      button5 = digitalRead(buttonPin5);
          if(button5 == HIGH);               //button5 is held high for the time being.
    
           {
           button4 = digitalRead(buttonPin4);  // start loop
           button2 = digitalRead(buttonPin2);
           
            if(button2 == LOW && button4 == LOW)  //both low, nothing happening
              {
               delay(500); 
              }
    
            if(button4 == HIGH)                  //button4 HIGH count up
              {
               digitalWrite(13, HIGH); //LED
               yh++;
               delay(200);
               Serial.println(yh, DEC);
               digitalWrite(13, LOW);
               }
    
            if(button2 == HIGH);       //button2 HIGH count down, here's the problem.
               {
                yh--;
                delay(200);
                digitalWrite(13, HIGH); //LED 
                Serial.println(yh, DEC);
                digitalWrite(13, LOW);
                }
                
          }
     }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    None of that is an actual loop. Also, if those aren't meant to be exclusive checks, if both 4 and 5 are high, it will end up making yh the same thing as it was before (button4 will increment it, button2 will decrement it).


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20
    Never mind, I found the problem the second if loop has a ";" at the end, and that's what the problem was, now it's working fine.

    Sorry for the wasted bandwidth.

    Thanks quzah, how would I make them exclusive checks?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could do:
    Code:
    if( button one is true && button two is not )
    {
    }
    else
    if( button two is true && button one is not )
    {
    }
    else
    {
        ...some other combination is in effect...
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20
    Thank you quzah, I'm slugging through this with great enthusiasm.
    My only wish would be to have started ten years ago.
    Lucky for me I don't have to use pointers yet, or call on functions outside the loop.
    But I feel it won't be long before I do because I already have a timer.h file I can use to delay while I'm doing something else.

    But now back to my program, I need to get this thing done before the weekend.
    Believe it or not, I spent the whole day trying to get this stupid button to work.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by RobertD View Post
    Never mind, I found the problem the second if loop has a ";" at the end, and that's what the problem was, now it's working fine.
    That wasn't the only one with a semicolon at the end of the test:
    Code:
    if(button5 == HIGH);
    ... and it's not a loop (to reiterate what quzah stated).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    And that is precisely why an "if" (or "for" or "while") should be written like this:
    Code:
    if (condition) {
        statement1;
    } else {
        statement2;
    }
    Instead of like this:
    Code:
    if (condition)
    {
        statement1;
    }
    else
    {
        statement2;
    }
    MUCH less likely to make that kind of mistake.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I hate that style of indenting. An opening brace should be on the same level of indentation as a closing brace in my opinion. But that is really a different thread entirely.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I hate that style of indenting. An opening brace should be on the same level of indentation as a closing brace in my opinion. But that is really a different thread entirely.


    Quzah.
    There's always my way ... that nobody likes but me...

    Code:
    if (x = 11)
      { dothisthing(); }
    else
      { dothatthing();
         andalittleofthis(); }
    It looks a lot nicer when syntax highlighted and it produces very compact code... but I'm thinking it's as non-standard as it gets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need "if","for loop",&"else" source codes
    By dn_angel_07 in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2009, 10:01 PM
  2. Replies: 4
    Last Post: 08-12-2009, 03:03 PM
  3. Replies: 17
    Last Post: 12-15-2006, 11:02 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM