How to add a second and a third push button in a servo sketch??

Hi all, we have a project with a shifter kart and we want to build a prototype with a servo controlled by three push buttons:

1-Go from 45º to 90º
2-Go from 45º to 0º
3-Go from 45º to 25º

I have found here in the forum one functional code, and we’ve tweaked a bit:

#include <Servo.h>
Servo myservo;  

int pos = 45;
int boton = 4; 
   
void setup(){ 

myservo.attach(9);
pinMode(pos, OUTPUT);
pinMode(button, INPUT); 
digitalWrite (button, LOW);
} 
 void loop(){ 
  
    if (digitalRead(button) == LOW)

  for(pos = 45; pos < 90; pos += 90) 
  {                                  
    myservo.write(pos);              
                          
  } 
  if (digitalRead(button) == HIGH) 
  
  for(pos = 90; pos>=90; pos-=45)    
  {                                
     myservo.write(pos);             
  delay(50);                             
  }
}

We have this problems:

-We tried to set up a second button (#2) and servo goes crazy
-If we press the button on long time, goes to 90º instead of do it on a long and a short press (the servo should go to 90º/45º/25º yes or yes and return to its original position, 45º…)

Someone can help us to set up the correct configuration to the three buttons?

Any help is greatly appreciated :slight_smile:

Regards :slight_smile:

We tried to set up a second button (#2) and servo goes crazy

That seems very unlikely, unless your code or your wiring is completely bogus. Which is it?

    if (digitalRead(button) == LOW)


  if (digitalRead(button) == HIGH)

Are you planning to add code for the other possible return values for digitalRead(), like CARESSED and TICKLED and...

Every if statement should be followed by curly braces.

Why is there a delay() in one loop, and not in the other?

-If we press the button on long time, goes to 90º instead of do it on a long and a short press (the servo should go to 90º/45º/25º yes or yes and return to its original position, 45º...)

Are you using some kind of random word generator?

 for(pos = 45; pos < 90; pos += 90) 
  {                                  
    myservo.write(pos);                          
  }

How many times do you expect this for loop to run?

-We tried to set up a second button (#2) and servo goes crazy

The way the program is written at the moment the servo will go to one position when the button is pressed and another when the button is not pressed. You have not posted your attempt at adding a second button, but if you left in the code to do both actions as above then no wonder the servo does not do what you want as it will be trying to do 2 things when the second button is pressed and the first is not.

Please post you program where you added a second button so we can see what is going on and give advice.

Some questions :

How is the servo powered ? If direct from the Arduino then it may be taking more current than the Arduino voltage regulator is designed to provide and could, therefore, damage it.

How are the buttons wired ? Are you using any pull up/down resistors to keep the input button at a known voltage at all times ?

int boton = 4;

Did you really copy this line from the IDE ?

As others have said there are some detail "shortcomings" in your code.

However the overall picture is pretty close to what is needed

Instead of reading the same button and acting on whether it is high or low each section should test its own button. For example

if (digitalRead(buttonApin) = LOW) { // assumes it reads LOW when button is pressed
   // make servo do one thing
}
if (digitalRead(buttonBpin) = LOW) {
  // make servo do another thing
}

...R

For example

That snippet won't even compile. I can't believe you posted that. Must be tired.

PaulS: That snippet won't even compile. I can't believe you posted that. Must be tired.

Strange as it may seem I did not expect it to compile :)

...R

Strange as it may seem I did not expect it to compile

I hope that OP has the same expectations.

I hope that OP has the same expectations.

Well, as his original code did not compile anyway he is no worse off.

Hi, first of all, thank you for the answers !! :slight_smile:

We have a error posting the code…“Boton” isn´t, and the correct value is “button” (We put in spanish in our code but we translate for this post…)

And I add that none of the three buttons will be pressed simultaneously…

PaulS:

We tried to set up a second button (#2) and servo goes crazy

That seems very unlikely, unless your code or your wiring is completely bogus. Which is it?

    if (digitalRead(button) == LOW)

if (digitalRead(button) == HIGH)



Are you planning to add code for the other possible return values for digitalRead(), like CARESSED and TICKLED and...

Every if statement should be followed by curly braces.

Why is there a delay() in one loop, and not in the other?



> -If we press the button on long time, goes to 90º instead of do it on a long and a short press (the servo should go to 90º/45º/25º yes or yes and return to its original position, 45º...)


Are you using some kind of random word generator?

No way with “caressed and tickled” retorn values because the servo must act on a short and a long press (For that I say “yes or yes” unconditionally…)

The wiring is simple and no errors on it (Push button with pull down resistor), and goes crazy because we don´t know how to configure it…

The code works fine with if sentences…

??

The first delay not exist in the original code (Look at the end of this post…)

Some words we check on a translator before we posting…

Henry_Best:

 for(pos = 45; pos < 90; pos += 90) 

{                                 
    myservo.write(pos);                         
  }



How many times do you expect this for loop to run?

The times that we needed (We want to emulate a shifter of a kart, and the buttons have to work every time that we pressed one of them to up, or down the gear…)

UKHeliBob:

-We tried to set up a second button (#2) and servo goes crazy

The way the program is written at the moment the servo will go to one position when the button is pressed and another when the button is not pressed. You have not posted your attempt at adding a second button, but if you left in the code to do both actions as above then no wonder the servo does not do what you want as it will be trying to do 2 things when the second button is pressed and the first is not.

Please post you program where you added a second button so we can see what is going on and give advice.

Some questions :

How is the servo powered ? If direct from the Arduino then it may be taking more current than the Arduino voltage regulator is designed to provide and could, therefore, damage it.

How are the buttons wired ? Are you using any pull up/down resistors to keep the input button at a known voltage at all times ?

int boton = 4;

Did you really copy this line from the IDE ?

Exact, and there we have the problem (The code for the second button is similar to the first button, and only we change positions, and the servo works crazy because go to many positions, like 45º, 25º and 0º…)

The servo is powered throught 5v arduino but I repeat that this is a prototype (The servo that we want to put is a 15kg of torque and is capable of go from 0º to 90º on 0.5 seconds…Don´t worry because we powered the final servo trought the power supply of the kart…)

The buttons it´s with 1k pull down resistors…

The “boton” is an error…Its “button”, sorry…

Robin2:
As others have said there are some detail “shortcomings” in your code.

However the overall picture is pretty close to what is needed

Instead of reading the same button and acting on whether it is high or low each section should test its own button. For example

if (digitalRead(buttonApin) = LOW) { // assumes it reads LOW when button is pressed

// make servo do one thing
}
if (digitalRead(buttonBpin) = LOW) {
  // make servo do another thing
}




...R

Yes, sorry for the error (boton)…

Yes, but the servo must be on 45º always (The servo have to go up or down from that position…)

PaulS:

For example

That snippet won’t even compile. I can’t believe you posted that. Must be tired.

You’re right, it was late and I was tired and I apologize for that, but look this:

This is with the correct code and with the first button (From 45º to 90º)…The config works…

Paul, the code is in this post:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
 int pos = 0;    // variable to store the servo position 
 int button = 2;  // The button will be on Pin 7 
 
 
 void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode(pos, OUTPUT);
 pinMode(button, INPUT); 
 digitalWrite (button, LOW);
} 
 
 void loop() 
{ 
  
    if (digitalRead(button) == LOW)

  for(pos = 0; pos < 90; pos += 90)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of  degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                          // waits 1s for the servo to reach the position 
  } 
  if (digitalRead(button) == HIGH) 
  
  for(pos = 90; pos>=90; pos-=90)     // goes from 90 degrees to 0 degrees 
  {                                
     myservo.write(pos);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
  }
  
}

But we change:

 void loop(){ 
  
    if (digitalRead(button) == LOW)

  for(pos = 45; pos < 90; pos += 90) 
  {                                  
    myservo.write(pos);              
                          
  } 
  if (digitalRead(button) == HIGH) 
  
  for(pos = 90; pos>=90; pos-=45)    
  {                                
     myservo.write(pos);             
  delay(50);                             
  }
}

And the code compiles without problems…

Ver Imagen: http://s2.subirimagenes.com/privadas/previo/thump_2254287dibujo.jpg

Robin2:

PaulS:
That snippet won’t even compile. I can’t believe you posted that. Must be tired.

Strange as it may seem I did not expect it to compile :slight_smile:

…R

PaulS:

Strange as it may seem I did not expect it to compile

I hope that OP has the same expectations.

UKHeliBob:

I hope that OP has the same expectations.

Well, as his original code did not compile anyway he is no worse off.

xDDDDD

Soooooooorry guys 8) Yesterday I worked hard and I came home very tired…

Thank you for your support :slight_smile:

Regards :slight_smile:

(We are trying to make it through arduino because the shifter kit = 1270 USD…Servo it´s on 120 USD…)

Carlos_2K14: Soooooooorry guys 8) Yesterday I worked hard and I came home very tired....

Thank you for your support :)

That was a very long post and I have not read all of it. I get the impression you may have got a bit mixed up by the different bits of advice.

For example this comment

That snippet won't even compile. I can't believe you posted that. Must be tired.

was aimed at me, not at you.

And my suggestion in Reply #XX was not attempting to point out any error in your code.

Is everything working OK for you now, or have you any other questions?

...R

For some reason I can't modify Reply #10. I had intended to change the XX to the number 4.

...R

Robin2: For some reason I can't modify Reply #10. I had intended to change the XX to the number 4.

...R

Yep. I'm finding the 'modify' button on only some of my posts. Of course, by Sod's law, it doesn't appear on the ones I want to modify! >:(

Include (servo.h) // or whatever it is. Can’t remember, using phone.
Servo shiftServo; makes a servo and named it
Int buttonShiftUp =0;
Int buttonShiftDown=1; //these are your shift up and down buttons you push to shift

Int servoPos =45; // make a ing for the servo postion and sets it to 45 for now

Int gearPos =0; int for current gear selected
Int gearNumber=3; //made a int for the number for gears you have. Set the 3 to //however many you have
void setup(){
shiftServo.attach(10);//hook up your servo to pin 10.

}
void shiftUp(){
servo.write(90); //moves servo to shift up postion
delay(100); // gives it time to get there
shiftServo.write(45); //returns servo to middle postion
delay(100); agains it waits for servo travel
}
void shiftDown(){
servo.write(0); //moves servo to shift down postion
delay(100); // gives it time to get there
shiftServo.write(45); //returns servo to middle postion
delay(100); agains it waits for servo travel}

void loop(){
if (digitalRead(shiftUpButton, HIGH) && gearPos < gear){
shiftUp();
gearPos=gearPos++;}
If(digitalRead(shiftDownButton, LOW) && gearPos>1){
shiftDown();
gearPos = gearPos --;}
}
Tell me how this looks you. Fyi un still new yo this as well and am not using an compiler to write this. So may have slepping and or exact correct coding problems ie missing ; and // .
Don’t think this is cut and paste is what I mean.

Your logic looks reasonable but there are a lot of detail errors in your code.

And you haven't set the pinMode() for your I/O pins.

It is best to use pinMode(INPUT_PULLUP) for your switch pins and then arrange that a switch press pulls the line LOW.

Get it to compile and then try it and let us know how it behaves.

...R