if...else section does not respond

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tomz
 
Posts: 6
Joined: Wed Nov 02, 2011 11:43 am

if...else section does not respond

Post by tomz »

hi...this is my first post, so please bear with me. following is a section of code (if...else) that just does not work.the code falls through the first test...i rewrote that section using a switch case construct, and all is well, but i really don't know why the if...else doesn't work. could anyone enlighten me?

Code: Select all

void setdigit()                                   // function to set digit up or down
  {
     while ( digitalRead( (update ) )                // while the update pin is true (high)
       {
         digit();                                    		 // display the number
         
         if (digitalRead( countdown ) )              // check to see if we count up or down
           {
              num = num - 1;                         	// if so, decrement number
              
                if (num < 0 )                        	// reset to 9 if decrement < 0
                 {				// hence a signed integer was needed
                   num = 9;
                 }
           }
               else
                 {
                   num = num + 1;                   		// increment number
                 
                     if (num = 10)                  		// reset to 0 if increment is greater than 9)
                       {
                         num = 0;
                       }
                 }
                 
         delay(500);                               		// wait half second as a "thumb debounce"
       }
        
    EEPROM.write( 0, num );                        	// write number to internal EEPROM 
  }

User avatar
abqlewis
 
Posts: 71
Joined: Fri Dec 11, 2009 2:19 am

Re: if...else section does not respond

Post by abqlewis »

I don't quite know what "falls through" means, so I don't know if it fixes your problem, but I see a very common error. On line 20, you have "if (num = 10)" which will set "num" to 10, and always pass because 10 is not 0. Then it will always set num to 0. I think you mean "if (num == 10)". Even with that, there are some things I would do differently. Instead of:

Code: Select all

num = num + 1;              
if (num == 10)  
{
   num = 0;
}
I would do :

Code: Select all

if (num < 9)  
   num++;
else
   num = 0;
Why? First (this is just a general rule), if you compare to EQUAL 10, since num is modified in other areas, there is a chance you COULD miss 10, and then you got a runaway condition. Second, in the countdown case, you avoid the need to check negative numbers, so it takes a boundary case out of the condition. Third, 9 is really the important number, so it should be in the code (why just comment it, when your code can SAY it). Fourth, I just like the grouped code for the decision and action.

Hope this helps,
Michael

User avatar
tomz
 
Posts: 6
Joined: Wed Nov 02, 2011 11:43 am

Re: if...else section does not respond

Post by tomz »

hi michael
what is that saying??? "the difficult we do immediately, the obvious take a bit longer..." You are absolutely right, I was always setting num to 10, so the code "falls through"...(my definition of dropped loops, skipped tests, et al). Your solution works fine, and your help is much appreciated. I really don't know why I didn't use the " > 9 " test here instead of the " = "...that would have avoided the range trap you pointed out...btw, If you wonder why I don't use the " num++ " or " num-- ", it is because I plan to introduce, at a later date, a variable inc/dec, instead of a single inc/dec, and it is easier to auto-insert that into the code...

Thanks again...
tom

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”