Limiting button presses

I am working on a project to shift gears for a motorcycle engine 6 times both up and down. [0 2 3 4 5 6] 1st gear will be omitted, 0 represent Neutral. I was thinking a possible while loop but i can't envision it currently. :sweat_smile: So the upshift and downshift buttons needs to be disabled somehow past the specified limits. So any button press greater than 6th gear or Less than 0 (neutral) results in no PinOutput. Is there a way of disabling pins temporarily or a logical way of doing it?

Thanks for any help. Very new to arduino so all help is much appreciated.


You need to think in terms of a state-machine, for each state you adapt your
outputs (behaviour) accordingly, and change states when appropriate.

You might be able to get away with just using the current gear number as the state I think.

do you know of any helpful examples that I could look at to under this better?



do you know of any helpful examples that I could look at to under this better?

Wrong question.

The correct question is "Here is my code. How would I modify it to...?".

i’m thinking you should be able to use the constrain() function.
otherwise you should use old-skool if...then statements like ;
IF var < min THEN var = min
IF var > max THEN var = max
var being your gears

you could write a Function or Class (library) to handle which gear your are in.

for example if you write a class called Gear for example with some methods called:

up - for going up the gear.
down - for going down the gear
getGear - returns current gear
lastGear - returns last gear
gearChanged - true / false if gear has changed or not

you could use it like so:


Gear myGear(6);  // Create an instance of gear with maximum gears of 6.

myGear.up();              // go up a gear.
myGear.Down();       // go down a gear.
myGear.getGear();   // returns int of current gear.
myGear.lastGear();  // returns last selected gear.
myGear.gearChanged(); // returns a boolean true or false (0 / 1)

int x;
x = myGear.getGear();
if (x == someValue && myGear.gearChanged() == true;){
   // Do something

etc and so-forth

I’ve included a sample library to get you started (if you wish) you can alter it to include the code you want to execute for example make the library go up and down the physical gears, this would let you make a method that can select a gear instead of going up and down or you can use it as is and compare the value of the gear to execute the code for actually changing the gear.

Gear.7z (1.18 KB)