Hi everyone!
I want to control 5 rc servos with 5 corresponding buttons. For example, if button 1 is pressed the head of servo 1 rotates 180 degrees. If the same button is pressed again then the head of the same servo rotates back 180 degrees. I am not sure though whether I need to include a sweep function in order to get this to work. I have written 2 lots of code (one has a sweep function, the other doesn't) was wondering if anyone could have a look at both of them and provide some feedback on which is closest to what I need in order to get it to work, and what code I still need to change or add in.
I also don't know whether my circuit I have created for it is correct and I realise that this circuit would require a lot of power - but I am unsure how much power it would need. Would you mind giving feedback on not only whether my circuit is correct? but also how much power I would need to power it? and how to attach it to the circuit? - I have attached an image of the circuit to this post.
Thanks in advance to anyone who replies!
Code with Sweep function-
#include <Servo.h> // make sure Servo library is included
Servo myServo; // create servo object to control a servo
int pos = 0;
int pushPin1 = 3;
int pushPin2 = 4;
int pushPin3 = 5;
int pushPin4 = 6;
int pushPin5 = 7;
void setup() {
pinMode(pushPin1, INPUT_PULLUP);
myServo.attach(8);
digitalWrite(3, HIGH);
pinMode(pushPin2, INPUT_PULLUP);
myServo.attach(9);
digitalWrite(4, HIGH);
pinMode(pushPin3, INPUT_PULLUP);
myServo.attach(10);
digitalWrite(5, HIGH);
pinMode(pushPin4, INPUT_PULLUP);
myServo.attach(11);
digitalWrite(6, HIGH);
pinMode(pushPin5, INPUT_PULLUP);
myServo.attach(12);
digitalWrite(7, HIGH);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(pushPin1) == LOW);
{
//the first button has been pushed and the first servo turns 180 degrees
pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
else {
//the first button has been pushed again and the first servo turns back 180 degrees
pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
if (digitalRead(pushPin2) == LOW);
{
//the second button has been pushed and the second servo turns 180 degrees
pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
else {
//the second button has been pushed again and the second servo turns back 180 degrees
}
pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
if (digitalRead(pushPin3) == LOW);
{
//the third button has been pushed and the third servo turns 180 degrees
pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
else {
//the third button has been pushed again and the third servo turns back 180 degrees
}
pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
if (digitalRead(pushPin4) == LOW);
{
//the fourth button has been pushed and the fourth servo turns 180 degrees
pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
else {
//the fourth button has been pushed again and the fourth servo turns back 180 degrees
}
pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
if (digitalRead(pushPin5) == LOW);
{
//the fifth button has been pushed and the fifth servo turns 180 degrees
pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
else {
//the fifth button has been pushed again and the fifth servo turns back 180 degrees
}
pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
}
Code without sweep function-
#include <Servo.h> // make sure Servo library is included
Servo myServo; // create servo object to control a servo
int pushPin1 = 3;
int button1Reading = 0;
int pushPin2 = 4;
int button2Reading = 0;
int pushPin3 = 5;
int button3Reading = 0;
int pushPin4 = 6;
int button4Reading = 0;
int pushPin5 = 7;
int button5Reading = 0;
void setup() {
pinMode(pushPin1, INPUT_PULLUP);
myServo.attach(8);
digitalWrite(3, HIGH);
pinMode(pushPin2, INPUT_PULLUP);
myServo.attach(9);
digitalWrite(4, HIGH);
pinMode(pushPin3, INPUT_PULLUP);
myServo.attach(10);
digitalWrite(5, HIGH);
pinMode(pushPin4, INPUT_PULLUP);
myServo.attach(11);
digitalWrite(6, HIGH);
pinMode(pushPin5, INPUT_PULLUP);
myServo.attach(12);
digitalWrite(7, HIGH);
Serial.begin(9600);
}
void loop()
{
button1Reading = digitalRead(pushPin1);
if (button1Reading == LOW)
//the first button has been pushed and the first servo turns 180 degrees
{
myServo.write(0);
} else {
//the first button has been pushed again and the first servo turns back 180 degrees
myServo.write(180);
}
button2Reading = digitalRead(pushPin2);
if (button2Reading == LOW)
//the second button has been pushed and the second servo turns 180 degrees
{
myServo.write(0);
} else {
//the second button has been pushed again and the second servo turns back 180 degrees
myServo.write(180);
}
button3Reading = digitalRead(pushPin3);
if (button3Reading == LOW)
//the third button has been pushed and the third servo turns 180 degrees
{
myServo.write(0);
} else {
//the third button has been pushed again and the third servo turns back 180 degrees
myServo.write(180);
}
button4Reading = digitalRead(pushPin4);
if (button4Reading == LOW)
//the fourth button has been pushed and the fourth servo turns 180 degrees
{
myServo.write(0);
} else {
//the fourth button has been pushed again and the fourth servo turns back 180 degrees
myServo.write(180);
}
button5Reading = digitalRead(pushPin5);
if (button5Reading == LOW)
//the fifth button has been pushed and the fifth servo turns 180 degrees
{
myServo.write(0);
} else {
//the fifth button has been pushed again and the fifth servo turns back 180 degrees
myServo.write(180);
}
}
