Basic "if loop question


I’m having moderate but not complete success with an app I’ve written.

The app should flash LED 6, unless I press the corresponding button 3, in which case it stops the flashing cycle, emits a different color during the duration of the button press, and publishes the state change.

I think this is a pretty basic question but my code doesn’t seem to work:

also, should say b.led(6,255,0,25);

Right now when I run this software it doesn’t seem to run the first part of the If loop

I’d rather say the else if() can never be true since else is only ever the case when the if() is not :wink:
If you replace the else for an inverse if() your else if() would read like this

  // reworded else if
  if ((b.buttonOn(3) != true) && (b.buttonOn(3) == true))
    // this will never happen ;-) 
    // unless you are exceedingly quick to switch the button between the two function calls ;-)
1 Like

I see what you’re saying. So my pseudo code of:

####If button state is unpressed, Light up LED color 1

####If button state is pressed, Light up LED color 2

is impossible to execute?

I’ve only really done Matlab and a bit of Python, never any hardware interfacing like this, so I’m not sure how the logic should work

It’s not impossible but the way you’ve written it is not working.

You’d rather want something like this

    button3 = false; 
    b.ledOn(6, 255, 0, 25);
  else if(!button3) // else already implies b.buttonOn(3) == true
    button3 = true;

Your if and else if have the exact same content in them.

And screenshots are a horrible way to share and discuss code.

@tedder be nice! :wink:

@charlietokowitz there are a couple of alternate ways that you can share code in the forums:

  1. If it is a short snippet, paste your code into the compose window and click on the button in the formatting bar (</>)

  2. If it is a long code block, you can create and link to a “gist”, or a code snippet hosted on GitHub. They’re free to make, and format well within Discourse.