Newbie confused with if statement


hi all, i've started electronics , programming please bear me. trying work through example , cannot figure out how first if statement can ever run after first time runs. first time runs, ok, lower down in program, value val assigned old_val means value 1 or high. if case, first if condition can never repeated again if old_val == 1 :

void loop(){
 val = digitalread(button);

 if ((val == high) && (old_val == low)) //first if statement
 {
   state = 1 - state;
   delay(10);
 }

 old_val = val;

 if (state == 1)
 {      
   digitalwrite(led, high);  
 } else
   {
     digitalwrite(led, low);
   }
}


code: [select]
old_val = val;

is outside of if statement.  so on future time through digitalread returns low, if statement not execute, old_val set low.  then next time val high if statement run again.

now the
code: [select]
state = 1 - state;
confuses me bit though.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Newbie confused with if statement


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums