I need this code so when I press the left button, the servo starts a sweep motion and when I press the right button, it stops the sweep motion (for 4 servos).
I am new to Arduino, so if you can leave and explanation, that would be nice. Thanks! I got this code from an instructable if you are curious (http://www.instructables.com/id/How-to-Control-3-Servo-Motors-using-Push-Button-Sw/step8/Programming-the-Arduino-The-Main-Loop-explained/)
Here is the code below (I also attached it to the post if you would prefer)
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
int pos1 = 85; // variable to store the servo's starting position
int pos2 = 85;
int pos3 = 85;
int pos4 = 85;
const int maxDeg = 175; // limits the maximum range of the servo's movement to 175
const int minDeg = 5; // limits the minimum range of the servo's movement to 5
const int movement = 2; // distance to move servo
// This basically means the servo will sweep from 5 to 175 (not 0 to 180 as expected), you can adjust this to suit your own servo motors specs
const int leftPin = 2; // tells the Arduino the location of the signal cable from the switch
const int rightPin = 3;
const int leftPin2 = 4;
const int rightPin2 = 5;
const int leftPin3 = 6;
const int rightPin3 = 7;
const int leftPin4 = 12;
const int rightPin4 = 13;
const int outputPin = 8; // tells the Arduino the location of the signal cable to the servo
const int outputPin2 = 9;
const int outputPin3 = 10;
const int outputPin4 = 11;
int leftPressed = 0; // variables we will use to keep information about the switch states
int rightPressed = 0;
int leftPressed2 = 0;
int rightPressed2 = 0;
int leftPressed3 = 0;
int rightPressed3 = 0;
int leftPressed4 = 0;
int rightPressed4 = 0;
void setup()
{
myservo.attach(outputPin); // attaches the servo motor's signal cable location, stored in the variable outputPin, to the servo object
myservo2.attach(outputPin2);
myservo3.attach(outputPin3);
myservo4.attach(outputPin4);
pinMode(leftPin, INPUT); // sets the state of the pins to input mode
pinMode(rightPin, INPUT);
pinMode(leftPin2, INPUT);
pinMode(rightPin2, INPUT);
pinMode(leftPin3, INPUT);
pinMode(rightPin3, INPUT);
pinMode(leftPin4, INPUT);
pinMode(rightPin4, INPUT);
}
void loop()
{
leftPressed = digitalRead(leftPin); //gives a value to the variables as the state of the switch
rightPressed = digitalRead(rightPin);
leftPressed2 = digitalRead(leftPin2);
rightPressed2 = digitalRead(rightPin2);
leftPressed3 = digitalRead(leftPin3);
rightPressed3 = digitalRead(rightPin3);
leftPressed4 = digitalRead(leftPin4);
rightPressed4 = digitalRead(rightPin4);
// The following routine handles what happens if the first set of push buttons are pressed
if(leftPressed){
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
if(rightPressed){
if(pos > minDeg)
pos -= movement;
myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’
}
// The following routine handles what happens if the second pair of push buttons are pressed
if(leftPressed2){
if(pos2 < maxDeg)
pos2 += movement;
myservo2.write(pos2);
}
if(rightPressed2){
if(pos2 > minDeg)
pos2 -= movement;
myservo2.write(pos2);
}
// The following routine handles what happens if the third pair of push buttons are pressed
if(leftPressed3){
if(pos3 < maxDeg)
pos3 += movement;
myservo3.write(pos3);
}
if(rightPressed3){
if(pos3 > minDeg)
pos3 -= movement;
myservo3.write(pos3);
}
// The following routine handles what happens if the fourth pair of push buttons are pressed
if(leftPressed4){
if(pos4 < maxDeg)
pos4 += movement;
myservo4.write(pos4);
}
if(rightPressed4){
if(pos4 > minDeg)
pos4 -= movement;
myservo4.write(pos4);
}
}
//if(rightPressed3){
// ctrl_servo(myservo3, pos3, -1);
//void ctrl_servo(servo, poss, dir){
// if(poss > minDeg)
// poss += movement*dir;
// servo.write(poss);
// }
// else;
// delay(15);
// }
_4_Servo_Button_Switch_Odyssey.ino (4.45 KB)