Understand an if statement

Hi there,

Can someone please explain what this piece of code does?

if (onOffSwitchState != PreviousOnOffSwitchState){
  if (onOffSwitchState == HIGH){
     motorEnabled = !motorEnabled;
  }
}

These are all variables that are what their names imply.
And the OnOffSwitchState variable is set by a push button.

Any help would be greatly appreciated,
shmily

It checks if the state of the switch/button chanģed. If so, it checks if the new state is high. And if so, if will toggle motorEnabled; if motorEnabled was true ( or high) it will become false (or low), and vice versa.

!= means "not equals to"

So, evidently the old state of the switch was saved in PreviousOnOffSwitchState, and now it's looking to see if the current state, onOffSwitchState, differs from that.

If it does differ, then we look to see in what direction. (Since a change could be high to low or low to high, and knowing merely that it's changed is often not enough.)

So the next if, which you will see is inside the first if, so we know it's changed, looks to see if the current state is high, where presumably a high means pressed. If it IS high, then change the value of the motorEnabled value to its opposite. (! means "not", or opposite)

Boils down to if the button has become pressed, toggle the motor.

Note what I said there: has become pressed, not is pressed. It looks to see first if the switch changed, then to see if it's high. It can only get to the point for looking for the high if it changed, which means it must have been low,ie it's a NEW press. (Else, holding it down wold toggle it forever.)

edit: I know I'm saying what sterretje said, but I had typed it all out while he posted, so thought wth, may as well post too :wink:

Man that was really confusing. Will try to read another 37 times. If i still don't understand it, you guys will have to explain it again :smiley:

Ok let me paint the values a few times through loop.

Let's assume first that our switch is wired so a press makes the pin high, since that's what your example implies. Doesn't change the principle though. So it's low most of the time: will need a pull down for that, that's another story. We'll assume that PreviousOnOffSwitchState, when it was declared, was initialised low to match the default position of the button.

So let's say we start with button unpressed. First time through loop, compare old state to new. State was initialised low, it is low, if test fails, do nothing.

Now, and here's a part which you didn't post, there will be a line in the code that puts the current value into the previois value for next time. I'm assuming there is such a line. So, save current as old.

Through loop again, still button not pressed, current and previous are the same (remember that's an updated previous), do nothing.

Now press the button.

Go through loop. Current is high, previous is low, there's a change, if test passes.

Now inside that if, check if it's high. It is, means it went from low to high, ie, a press, so toggle motor. Save current as old, remember thats a high now.

Now and here's the fancy part, the processor is so fast, that you haven't let go the button by the time it loops again.

So let's see if it's changed. Current is high since your finger is slow. Old is high because we just saved it. Changed?- no. Do nothing. (That's where it would accidentally toggle the motor if we didn't do the check, if we just looked for a high, not a change to high). Save current to old, still a high.

Now let go the button.

Loop whizzes round. Check for a change. Current is low (we just let go), old is high. Changed? Yes. Is it high? No. Inner if fails, do nothing. (This was a high to low change, which has no significance. But perhaps in some cases a release also may do some action, program that into an else of the inner if.)

So now we're back where we started, excpet we toggled the motor along the way.

Rinse and repeat.

(Btw, toggling the motor state won't actually toggle the motor: there must be a line you didn't post that does a digitalWrite(pin, motorEnabled); )

Wow that was simple.

yes all your assumptions were correct and its all written in the code.

Thanks for taking the time to explain it so clearly,
shmily

shmilylauber:
Can someone please explain what this piece of code does?

if (onOffSwitchState != PreviousOnOffSwitchState){

if (onOffSwitchState == HIGH){
    motorEnabled = !motorEnabled;
  }
}

Flick the motor of/on once when the on/off switch (probably a pushbutton) becomes HIGH.