2
\$\begingroup\$

I have been working on this for a while. It is a small part of a much bigger code.

I have a digital input pin connected to a on/off switch and another input pin connected to a momentary push button. For the momentary push button I am using the onebutton library. It works great.

I am trying to make it so the momentary push button does nothing unless the on off switch connected to a digital pin if switched on or HIGH. For some reason the momentary push button still gets read and my program runs regardless of whether the switch is swichted or not.

void loop()
{
    ignition_mode = digitalRead(ignition);
    if (ignition_mode == HIGH);
    {
        button.tick();
    }
}
\$\endgroup\$
  • \$\begingroup\$ if (ignition_mode == HIGH); does nothing, because ; is an empty statement performed if the condition is true. \$\endgroup\$ – MrSmith42 Nov 11 '13 at 7:39
  • \$\begingroup\$ at least one } is missing \$\endgroup\$ – MrSmith42 Nov 11 '13 at 7:45
3
\$\begingroup\$

If you format you code properly, you can see that it does not what you intended:

void loop()
{
  ignition_mode = digitalRead(ignition);
  if (ignition_mode == HIGH)
    ;  // empty statement which means that nothing is done if the condition is true
  { // new scope-block, which has quite no effect here
    button.tick();
  }
}

You most likely intended:

void loop()
{
  ignition_mode = digitalRead(ignition);
  if (ignition_mode == HIGH)
  { 
    button.tick();
  }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ sweet, does the IDE platform used to compile this code not include even basic warnings about suspicious code? \$\endgroup\$ – Andyz Smith Nov 11 '13 at 16:12
3
\$\begingroup\$

Two possibilities:

Remove the ";" after the if. The correct version would be:

if (ignition_mode) {
 .. Do something here
}

Have you checked that the level on that pin actually toggles or is it fixed to some value?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.