Conditions

Can anyone tell me what difference did it make when I made the following change?
Initial Code [/b]
** **if(ch1==0 && PINB & 00000001)   {     ch1=1;     time1=current_time;   }     if(ch1==1 && !(PINB & 00000001))     {       ch1=0;       rch1 = current_time - time1;     }** **
Final code:[/b]

**__ <strong>** if(PINB & B00000001){            //Changed made conditoned seperated   if(ch1==0)   {     ch1=1;     time1=current_time;   }   }   else if(ch1==1)     {       ch1=0;       rch1 = current_time - time1;     }**</strong> __**
After I made the above change i got the desired output

dhr999:
After I made the above change i got the desired output

you may have simply changed the order of the operations (I can't see it from simply looking!)

C++ Operator Precedence shows you that

try coercing your operators with parenthesis if you want to try that.

& happens before && so that would be fine

seems equivalent unless PINB can change fast as you read it only once in the second case

One difference is that both are now using the same value of PINB. In the first example you read PINB twice and it is possible for the value of bit 0 to have changed between them.

Auto format your code and look again. You have laid it out for an else that does not exist

Mark

holmes4:
Auto format your code and look again. You have laid it out for an else that does not exist

Mark

No no - it's all right.

[color=red]if (PINB & B00000001) {[/color]
[color=blue]  if (ch1 == 0) {
    ch1 = 1;
    time1 = current_time;
  }[/color]
[color=red]} else[/color][color=green] if (ch1 == 1) {
  ch1 = 0;
  rch1 = current_time - time1;
}[/color]

the green if is a full expression.