Sequential shifter - 2xservo at 5 positions w. 2 buttons->pos. 1-2-3-4-5-4-3-2-1

Hi

I’m building a sequential shifter and need some coding help. I got the setup running with this code, where I got the gear positions coded on 5 buttons. That part works fine.

But I would really like to make a sequential shifter, so I could shift up and down using 2 buttons - 1up 2dwn - like in a racecar. But I don’t know how to code this.

The setup contains 2 servo’s which controlls the position of the shifter arm on my gearbox. We can call it X and Y axis - one controlls side to side movement and the other in/out movement. The shifter arm can move in 4 pos. in/out - and 3 pos. sideways.

so pos. are like this:

XX-Y2 = neutral

X1Y1 = Reverse
X2Y1 = 1. gear
X3Y1 = 3. gear
X4Y1 = 5. gear
X2Y3 = 2. gear
X3Y3 = 4. gear

Please will be much welcome:-)

#include <Servo.h>

Servo myservo; // servo object 1
Servo myservo1; // servo object 2

int pos = 0; // variable to store the servo position
const int buttonPin = 1; //sets pin 1 as button
const int buttonPin1 = 2; //sets pin 2 as button
const int buttonPin2 = 3; //sets pin 3 as button
const int buttonPin3 = 4; //sets pin 4 as button
const int buttonPin4 = 5; //sets pin 5 as button
const int ledPin = 13; //sets pin 13 as LED
int buttonState = 0; //sets button 1 as off
int buttonState1 = 0; //sets button 2 as off
int buttonState2 = 0; //sets button 3 as off
int buttonState3 = 0; //sets button 4 as off
int buttonState4 = 0; //sets button 5 as off

void setup()
{
myservo.attach(9); // attaches servo 1 on pin 09
myservo1.attach(10); // attaches servo 2 on pin 10

pinMode (buttonPin, INPUT); // sets button as input
pinMode (ledPin, OUTPUT); // sets led as output
}

void loop()
{
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);

//button 1

if (buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);

{
myservo.write(5); // tell servo 1 to go to position
delay(1500); // waits 15ms for the servo to reach the position
myservo1.write(10); // tell servo 2 to go to position
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(ledPin, LOW);
}

//button 2

if (buttonState1 == HIGH)
{
digitalWrite(ledPin, HIGH);
{
myservo.write(40); // tell servo 1 to go to position
delay(1500); // waits 15ms for the servo to reach the position
myservo1.write(60); // tell servo 2 to go to position
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(ledPin, LOW);
}

//button 3

if (buttonState2 == HIGH)
{
digitalWrite(ledPin, HIGH);
{
myservo.write(80); // tell servo 1 to go to position
delay(1500); // waits 15ms for the servo to reach the position
myservo1.write(30); // tell servo 2 to go to position
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(ledPin, LOW);
}

//button 4

if (buttonState3 == HIGH)
{
digitalWrite(ledPin, HIGH);
{
myservo.write(130); // tell servo 1 to go to position
delay(1500); // waits 15ms for the servo to reach the position
myservo1.write(120); // tell servo 2 to go to position
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(ledPin, LOW);
}

//button 5

if (buttonState4 == HIGH)
{
digitalWrite(ledPin, HIGH);
{
myservo.write(180); // tell servo 1 to go to position
delay(1500); // waits 15ms for the servo to reach the position
myservo1.write(40); // tell servo 2 to go to position
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(ledPin, LOW);
}
}

Henrik

If you have 2 servos, one that moves the shifter in the X direction and one that moves the shifter in the y direction, it would make more sense for the objects to be named servoX and servoY, instead of myservo and myservo1.

If you have 5 switches, an array of 5 pin numbers and states makes more sense than the mess you have now. By the way, they are switches, not buttons. Buttons belong on the front of shirts.

delay(1500); // waits 15ms for the servo to reach the position
delay(15); // waits 15ms for the servo to reach the position

Is 1500 equal to 15 milliseconds, or is 15 equal to 15 milliseconds? Is the comment even necessary or useful? That is, does it convey any non-obvious information?

What you have with the gearbox is a state machine. The gearbox can only be in one gear at a time, right? If that constraint is not imposed, awful noises result, right? Those noises are expensive to make go away, right?

So, create an integer variable, gearPos. Initialize it to 0. Each time the up button is pressed, increment the gearPos variable, unless it is at max. Each time the down button is pressed, decrement the gearPos variable, unless it is at min.

Each time there is a change to gearPos, set the servo positions to correspond to the new value of gearPos. Saving the X positions in an array, and the Y positions in an array would make this very easy to do.

A delay of 1.5 seconds after moving the first servo before moving the second servo seems like far too long to me.

Hi

First of all, I'm not from a english speaking country and I ask for help in this forum because i'm new to this - I didn't write here to be mocked for my ignorance or lack of linguistic capabilities.

And dispite your comment "By the way, they are switches, not buttons. Buttons belong on the front of shirts." - you later write "Each time the up BUTTON is pressed, increment the gearPos variable....."

And again, the statement "If you have 5 switches, an array of 5 pin numbers and states makes more sense than the mess you have now." - more mocking and you beeing smart - not helping.

More mocking and non helping comment. "Is 1500 equal to 15 milliseconds, or is 15 equal to 15 milliseconds? Is the comment even necessary or useful? That is, does it convey any non-obvious information?" - Okay - I messed up a comment - I'm so sorry.

I will ofcource adjust the timing of the servo movements - but for safety reason during development I'll go with long shift-times.

The rest of your reply was very useful - I thank you for this. I will see if I can find someone who can help me make something usefull out of it.

Henrik