gear shifter driven by servo

Hi everyone, as a newcomer I have few questions in my little project that I have been working on.

I have created a fairly simple code that controls my servo by 2 different buttons. When pressed one turn the servo left and one right. Problem is that my code works a little wrong. The servo turns only when the button is pressed down and I want it to make the movement once the button is pressed. Like if I press the button real quick, servo goes to certain position and comes back.

I hope I don't sound too confusing :slight_smile:

Here is a video of my setup, and the code

#include <Servo.h>

Servo myservo;
int button7=0;
int button6=0;
int pos=90;

void setup()
{
pinMode(7, INPUT);
pinMode(6, INPUT);
myservo.attach(9);
}

void loop()
{
button7=digitalRead(7);
button6=digitalRead(6);

myservo.write(pos);

delay(5);

pos=constrain(pos,0,180);

if(button7==1 && button6==0)
{
pos = 0;

}
 if(button7==0 && button6==0)
{
pos = 90;

}
if(button7==0 && button6==1)
{
pos = 180;
}
}
  1. please edit your posting to use code tags, its all explained in the sticky thread.

  2. Are your buttons wired to ground or Vcc?

  3. Your code handles 3 button states, perhaps one of them is neither-button-pressed?
    Do you really want the servo to go somewhere everytime the buttons aren't pressed?

Hello, the previous post should be more clear now.

Iirc the ground was wired to power ground in the arduino board, same as servo power ground.

  1. It is mandatory that the servo returns to the middle position, once the gear is shifted. But with this code it feels little dodgy from time to time (interference occurs for unknown reason). I was hoping to find better alternatives to have the middle point as "home" state and by one button click force the servo to turn for example from 90 to 180 and back.

Now when button is pressed, servo turns to the correct position, when released it comes back.
I feel like I need a direction to go with this programming, MY first code to be honest.

heromanni:
3) It is mandatory that the servo returns to the middle position, once the gear is shifted.

Then you'll need some way of determining when the gear has shifted.... A sensor?

One thing that I thought is that would there be a certain angle which after servo gets input to return to home.

I have not tested the movement yet, but for example if after turning 20 degrees, the gear has been shifted, would the arduino generate a feedback to allow returning to home state?

For what I understand is that arduino knows the state at which servo is at all times

For what I understand is that arduino knows the state at which servo is at all times

Bad assumption. The arduino only knows where the servo has been commanded to go. There is no actual position feedback from typical hobby servos.

And you don't even know if it gets there - overload it and it'll grind away stuck still...

Perhaps you can use a delay or timeout to decide when to return to idle state
if there's no other meaningful indication.

Very basic servo button code.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
    delay(2000);
    servo1.write(20);
  }
}

Zoomkart thank you for that, code is basic but seems like it does the job quite well. I'll just add some code and second button for downshifting.

The calibration with delay time and angles will only be set correctly in field test, those should not be a problem.

Another thing is to code a flatshift function when upshifting, I quess it is just a digital output signal that runs a relay with given time delay (that also is to be set correct, maybe 10-40 milliseconds).

Can I just add line to void loop with command that turn one digital pin high once button is pressed?
Should not be a difficult job and sorry for all this questions, I maybe should just test it out

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
    delay(2000);
    servo1.write(20);
  }
}