strange button problem...

hey guys, so i have 2 buttons, one does program changes up and the other is program changes down, one interval at a time.

the problem is this:
lets say i am on program 10
i press program down, it goes to 9, press again it goes to 8.
now i want to go back up to program 9
i press program up and it will go down to 7, press again 8, press again 9.
then if i press program down it will go up one before going down and so on.
hopefully that makes sense.

i am using AlphaBeta's button library for the unique press function.

this is the section of code in question.

 Button progUp = Button(11,PULLUP); //program up pin 11
Button progDn = Button(12,PULLUP); //program up pin 12
int prog = 114;



    if(progUp.uniquePress()){
    MIDI.send(PC,prog++,0,1);
}
   if(progDn.uniquePress()){
    MIDI.send(PC,prog--,0,1);
}

*note that is not the entire sketch, just the button assignment and loop section.

Try ++prog and --prog; I think you are running into a pre vs. post decrement/increment issue...

:slight_smile:

that was exactly the problem!

thanks so much, i could not figure out what was going wrong, and it was so simple!

The real question is - do you understand the difference as to where and how things work based on where the increment/decrement operators are placed?

actually...i guess i don't really understand why it makes a difference.
i just know that i will be using the -- or ++ int he front now.

would you mind explaining whats going on? it would be cool to truly know what was going on.

thanks cr0sh!

pre Xcrement pun slightly intended makes the value change before it's passed, and post changes the value after it's passed.
:slight_smile:

assume prog = 5

preincrement:
x = ++prog;
y = prog;
results in x = 6 and y = 6 (increment the value of prog then use the incremented value)

but
post increment:
x = prog++;
y = prog;

results in x = 5 and y = 6 (use the value of prog then increment it)

wow, thanks guys not that makes sense!
great to know why i should do something rather than just knowing what to do.

Note that this same logic applies everywhere; for instance:

if (i++ < 10) {
  // do stuff
}

...will function differently than:

if (++i < 10) {
  // do stuff
}

As you have seen, if you aren't taking it into account (perhaps from a night of drinkin' - ;D ), it can trip you up!

:slight_smile: