I have 2 servos connected to a Arduino Mega and 3 buttons. 2 of the buttons control an individual servo and the other controls both. A button press writes the servos from 0-150 and another would write it back from 150-0.
Everything worked with 2 buttons and 2 servos but once I added the 3rd button a press of the button causes the servos to rotate continuously 0-150 150-0 until another button press.
Any help on reasoning out why this is happening? Is it the code?
Thanks
#include <Servo.h>;
Servo servo, servo2; //naming the two servos
const int buttonPin = 8; // pushbutton 1 pin
const int servoPin = 3; // servo 1 pin
int counter = 0; //create a variable to store a counter and set it to 0
const int buttonPin2 = 12; // pushbutton 2 pin
const int servoPin2 = 5; // servo 2 pin
int counter2 = 0; //create a variable to store a second counter and set it to 0
const int buttonPin3 = 10; // pushbutton 3 pin
int counter3 = 0; //create a variable to store a third counter and set it to 0
void setup()
{
servo.attach (servoPin);
pinMode(buttonPin, INPUT);
servo2.attach (servoPin2);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT); // Set up the pushbutton pins to be an input and attach the servos
}
void loop()
{
int buttonState; // local variable to hold the pushbutton 1 states
//read the digital state of buttonPin with digitalRead() function and store the value in buttonState variable
buttonState = digitalRead(buttonPin);
//if the button is pressed increment counter and wait a tiny bit to give us some time to release the button
if (buttonState == LOW) // light the LED
{
counter++;
delay(300);
}
if(counter == 0)
servo.write (20); // zero degrees
else if(counter == 1)
servo.write(150);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter = 0;
int buttonState2; // local variable to hold the pushbutton 2 states
//read the digital state of buttonPin with digitalRead() function and store the value in buttonState variable
buttonState2 = digitalRead(buttonPin2);
//if the button is pressed increment counter and wait a tiny bit to give us some time to release the button
if (buttonState2 == LOW) // light the LED
{
counter2++;
delay(300);
}
if(counter2 == 0)
servo2.write (20); // zero degrees
else if(counter2 == 1)
servo2.write(150);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter2 = 0;
int buttonState3; // local variable to hold the pushbutton 3 states
//read the digital state of buttonPin with digitalRead() function and store the value in buttonState variable
buttonState3 = digitalRead(buttonPin3);
//if the button is pressed increment counter and wait a tiny bit to give us some time to release the button
if (buttonState3 == LOW) // light the LED
{
counter3++;
delay(300);
}
if(counter3 == 0)
servo.write (20), servo2.write (20); // zero degrees
else if(counter3 == 1)
servo.write (150), servo2.write(150);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter3 = 0;
}
// Orignal Framework of the code by UnicornClockworks https://www.instructables.com/id/Servo-Motor-Arduino/
Please use "code tags" at the top left for Your formatted code.
Use Serial monitor and"Serial.print" to se what the sketch is doing. Your info doesn't give any help at all. "Adding a third button and problems occur………". That could be anything from nothing to a roasted controller.
#include <Servo.h>;
Servo servo, servo2; //naming the two servos
const int buttonPin = 8; // pushbutton 1 pin
const int servoPin = 3; // servo 1 pin
int counter = 0; //create a variable to store a counter and set it to 0
const int buttonPin2 = 12; // pushbutton 2 pin
const int servoPin2 = 5; // servo 2 pin
int counter2 = 0; //create a variable to store a second counter and set it to 0
const int buttonPin3 = 10; // pushbutton 3 pin
int counter3 = 0; //create a variable to store a third counter and set it to 0
void setup()
{
servo.attach (servoPin);
pinMode(buttonPin, INPUT);
servo2.attach (servoPin2);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT); // Set up the pushbutton pins to be an input and attach the servos
}
void loop()
{
int buttonState; // local variable to hold the pushbutton 1 states
//read the digital state of buttonPin with digitalRead() function and store the value in buttonState variable
buttonState = digitalRead(buttonPin);
//if the button is pressed increment counter and wait a tiny bit to give us some time to release the button
if (buttonState == LOW) // light the LED
{
counter++;
delay(300);
}
if (counter == 0)
servo.write (20); // zero degrees
else if (counter == 1)
servo.write(150);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter = 0;
int buttonState2; // local variable to hold the pushbutton 2 states
//read the digital state of buttonPin with digitalRead() function and store the value in buttonState variable
buttonState2 = digitalRead(buttonPin2);
//if the button is pressed increment counter and wait a tiny bit to give us some time to release the button
if (buttonState2 == LOW) // light the LED
{
counter2++;
delay(300);
}
if (counter2 == 0)
servo2.write (20); // zero degrees
else if (counter2 == 1)
servo2.write(150);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter2 = 0;
int buttonState3; // local variable to hold the pushbutton 3 states
//read the digital state of buttonPin with digitalRead() function and store the value in buttonState variable
buttonState3 = digitalRead(buttonPin3);
//if the button is pressed increment counter and wait a tiny bit to give us some time to release the button
if (buttonState3 == LOW) // light the LED
{
counter3++;
delay(300);
}
if (counter3 == 0)
servo.write (20), servo2.write (20); // zero degrees
else if (counter3 == 1)
servo.write (150), servo2.write(150);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter3 = 0;
}
// Orignal Framework of the code by UnicornClockworks https://www.instructables.com/id/Servo-Motor-Arduino/
Everything worked with 2 buttons and 2 servos but once I added the 3rd button a press of the button causes the servos to rotate continuously 0-150 150-0 until another button press.
What is the 3rd button supposed to do?
How do you have your buttons wired, with 10k pullup or pull down resistors?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
meltDown:
Still not clear...might be best to spell out what the 3rd one does. "When button 3 is pressed servo1 must do and servo2 must do ".
edit: Is button 3 pressed supposed to make the servo act as if buttons 1 and 2 were both pressed at once?
Yes to your edit. When the 3rd button is pressed, no matter the position of the servos, both servos would rotate to 0 degrees and when pressed again both would rotate to 150 degrees.
Also this will be powered by a 9V volt adapter. I have an updated circuit but only in fritzing.
I think I know why the servos sway back and forth but I just can't figure a way around it.
The way I have it set up causes the if-else statements to contradict each other as the the ones for the individual controls hold the servo in a position while the counter value remains the same. Then the 3rd button tries to change the position and then the other if-else's send it back to the other.
I think this is what's happening but I'm just getting in to this and don't have much code experience otherwise.
Just to get it clear in my mind... The devil is, as always, in the details....
Let's say servo2 has been moved to 150 by an odd-numbered press of button2. The next (even) press of button2 should move it to 0.
But then let's say before that, we get a press of button3, which moves servo2 to 0.
Servo2 and button2 are now out of synch...
What does the next (even) press of button2 do? It "should" move servo2 to 0, but it's already there by dint of button3. So is the next (even) press of button2 ignored (leaving servo2 at 0) or does it change its mind so that servo2 moves to 150 changing the purpose of button2's even presses.
Summary: Do buttons 1 and 2
always move their respective servos to 0 (even press) and 150 (odd press), or
always move their servos to the opposite position, like when button C has messed the sequence up?
ZCCode:
I think this is what's happening but I'm just getting in to this and don't have much code experience otherwise.
Probably right: that's what got me thinking to ask my questions in the previous post.
What you could do, when counter3 causes that out-of-synch conflict, is change counter1 and counter2 to reflect the new positions (ie the ones counter3 has caused, even though neither button1 nor button2 has just been pressed) so that when counter1 and counter2 are next examined, the servos are in agreement with the counters and don't move.
But then you need to live with (or work round) the fact that the next press of button1 or button2 will move their servos, which may or may not be what you want... see my "summary" in previous post.
edit: what would make things a little simpler, is not to have those counters as int but as bool, seeing as you only need 2 values. Then it's a simple matter to change any of them to their opposites with a simple ! meaning "not" as below:
bool counter1= false;
//then later on...
counter1=!counter1; //makes it true
//then laterer on...
counter1=!counter1; //makes it false
I see code that should move the servo back and forth when the button (INPUT with pullup) is not pressed.
if (buttonState == LOW) // light the LED
{
counter++;
delay(300);
}
if(counter == 0)
servo.write (20); // zero degrees
else if(counter == 1)
servo.write(150);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter = 0;
If you read button3 first you could do this to servo 1 and 2,
if (buttonState == LOW) // light the LED
{
counter++;
delay(300);
}
if((counter == 0) || (counter3 == 0))
servo.write (20); // zero degrees
else if ((counter == 1) || (counter3 == 1))
servo.write(150);
//reset the counter to 0 if 1 -- do this to counter3 AFTER processing servo 2
if (counter > 0)
counter = 0;