servo actuation by button counting

i am trying to use a push button to shift a race car. But i need it to shift up multiple times with one stroke of the clutch. I currently have the clutch-shift-controled release working but i want it to shift twice if the button is pushed twice but only pull the clutch servo once. This is my code thus far:

#include <Servo.h>
int button1 = 8; //launch
int button2 = 4; //shift down
int button3 = 2; //shift up
int button4 = 7; //Neutral
int press1 = 0;
int press2 = 0;
int press3 = 0;
int press4 = 0;
Servo clutch;
Servo shifter;
int pos2 = 0;
int pos = 0;
int launchslip = 1;
int shiftslip = 0;
int vallaunch;
int valshift;
int shiftdelay= 450;
int buttonPushCounter = 0;
int lastpress2 = 0;
int counter = 0;

void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
clutch.attach(9);
shifter.attach(10);
digitalWrite(8, HIGH);
digitalWrite(4, HIGH);
digitalWrite(2, HIGH);
digitalWrite(7, HIGH);
}

void loop()
{
clutch.write(0);
press2 = digitalRead(button2);

if (press2 == LOW) //Shift down
{
counter++;
valshift = analogRead(shiftslip);
valshift = map(valshift, 0, 1023, 1, 5);
clutch.write(180);
delay(500);
for(counter = 1; counter > 0; counter-=1){
shifter.write(180);
delay(shiftdelay);
shifter.write(90);
delay(shiftdelay);
}
for(pos = 180; pos>=0; pos -= valshift){
clutch.write(pos);
delay(15);
}
}
}

So does it need to remember the last button pressed or do you want to put it on a timer?

Say you shift up, this triggers the clutch servo once. Then you press it again, but being that the code knows you pressed up before and not down or neutral, it doesn't trigger the clutch. Only when the new press is different from the previous press, does it trigger the clutch.

Or you can use a timer and see how many times you can press the up button with in a certain time frame of say 1.5 seconds and when 1.5 seconds has past, it then triggers the clutch.

I like the sound of the timer and then it does what it needs to do. How does that work?

Read the first button press then record the time and set a lockout flag to true (lockout flag will prevent the code from going back and record the time again, until the flag is reset later). Now you can use a while loop to keep getting the button presses and save them in a counter, until a certain time has passed. Once the time has passed, you can return the button press counter, and also change the lockout flag to false.

I have a code at home that I made a long while back. I will try to find it when I get home tonight. If anything I will definitely post it tomorrow.

It sounds harder than it is, but it is quite easy. Check out the example sketch Blink Without Delay for an idea about timers.

that does sound really hard. The only experience i have is the last two weeks and i havnet seen any timer stuff yet.

Try this, you have it in your example folder too, under File -> Examples -> Digital Blink Without Delay

Admittedly nothing to do with your question but if you are going to give buttons names then why not use meaningful names such as

const byte launch = 8;
const byte shiftDown = 4;
const byte shiftUp = 2;
const byte neutral = 7;

Then later in the program ist will be more obvious what is going on, such as

press2 = digitalRead(shiftDown);

Using more meaningful names for the other variables would also make sense as would using data sizes that do not waste space and declaring variables as const when you know that their value will not/cannot change as in my example above.

I have it that way on my compiled. Thought this one had been converted to update.

I really dislike the new code layout, I liked it when it was more compact. :(

Attached is the sketch I made a while back, but I tested it and it works.

I really dislike the new code layout, I liked it when it was more compact. :frowning:

Attached is the sketch I made a while back, but I tested it and it works.

Edit: I now don’t like the file upload, it doesn’t allow .ino files to be attached. No worries, I made it a txt file, all you need to do is just copy and paste it into your IDE.

ButtonMultiPress.txt (2.03 KB)