Hello guys.
I am trying to move servo motor step by step with two buttons (one to move it one step left per press of the button and the other one to move it right by a step). So moving servo motor form 0 to x value works as it should but when I try to move it back one step with the other button servo first returns to 0 position and then moves to the targeted position. This is a problem because I am trying to make electronic shifting for my bike. So if servo first moves to 0 and then targeted position it will jump all the gears
Here is the code
#include <Servo.h>;
const int buttonPin1 = 4;
const int buttonPin2 = 2;
const int servoPin = 3;
Servo servo;
int counter = 0;
void setup()
{
Serial.begin(9600);
servo.attach (servoPin);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop()
{
int buttonState1;
int buttonState2;
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == LOW) // light the LED
{
digitalWrite(13,HIGH);
counter++;
delay(200);
} else if(buttonState2 == LOW){
counter--;
delay(200);
}
switch(counter){
case 0:
servo.write (10);
delay(20);
break;
case 1:
servo.write (30);
delay(20);
break;
case 2:
servo.write (50);
delay(20);
break;
case 3:
servo.write (70);
delay(20);
break;
case 4:
servo.write (90);
delay(20);
break;
}
Serial.println(counter);
}
You have nothing to limit the range of your counter, i.e., it could count up or down continuously. If you don't want it to go greater than 4 or less than 0 you need to put a check in for that.
You are checking "if" the buttons are pressed not "when" they are pressed. For example, if you hold button1 down you will continuously count up rather than one count per press. Check out the following example to detect when a button is pressed: StateChangeDetection
thank you for your advice. I planned to add a limit for the counter after making the core working but I didn't realize that that would make a problem if it was not there (still new to arduino and coding in general). As of StateChangeDetection I'll look it up and update you with what I got.
corbax:
So moving servo motor form 0 to x value works as it should but when I try to move it back one step with the other button servo first returns to 0 position and then moves to the targeted position.
I don't see any problem in your sketch that would cause that to happen. Are you sure the sketch you posted is the one that is currently uploaded to the board? What model Arduino are you using? How is everything powered?
So I tried doing what ToddL1962 said. The code worked on my laptop but I needed to switch to PC and then it stopped working (On the laptop I have a year old version of IDE and on the PC is the news version. I don't know if that matters but I'll add that anyway). Now on the PC first 7 moves are working fine but at the 8th one it starts moving from one side to the other uncontrollably and when I get back down to the 5th move it stops moving from side to side but goes to pos 10 and then wanted pos like in the first post.
johnwasser I am sure that the posted code is uploaded to the board. I am using arduino Nano and it is powered by mini-USB cable which is also used to transfer data.
Is it maybe a problem in servo motor because when it is in pos 0 to 10ish it produces some weird buzzing sound.
Here is a new version of the code
#include <Servo.h>;
const int buttonPin1 = 4;
const int buttonPin2 = 2;
const int servoPin = 3;
Servo servo;
const int minGear = 1;
const int maxGear = 9;
int counter = 1;
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
void setup()
{
Serial.begin(9600);
servo.attach (servoPin);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
if(buttonState1 != lastButtonState1)
{
if (buttonState1 == LOW && counter != maxGear)
{
digitalWrite(13,HIGH);
counter++;
delay(150);
}
delay(20);
}
lastButtonState1 = buttonState1;
if(buttonState2 != lastButtonState2)
{
if (buttonState2 == LOW && counter != minGear)
{
digitalWrite(13,HIGH);
counter--;
delay(150);
}
delay(20);
}
lastButtonState2 = buttonState2;
switch(counter){
case 1:
servo.write (10);
delay(20);
break;
case 2:
servo.write (30);
delay(20);
break;
case 3:
servo.write (50);
delay(20);
break;
case 4:
servo.write (70);
delay(20);
break;
case 5:
servo.write (90);
delay(20);
break;
case 6:
servo.write (110);
delay(20);
break;
case 7:
servo.write (130);
delay(20);
break;
case 8:
servo.write (150);
delay(20);
break;
case 9:
servo.write (170);
delay(20);
break;
}
Serial.println(counter);
}
Schematics are often useful. You can draw them using a pencil and paper, or use an online tool and then post a picture of it. To post a picture upload it to an image hosting site and then put the link between image tags