#include <Servo.h>
Servo myservo1; // create servo object to control a servo
#define servoPin 3
Servo myservo2;
#define servoPin 4
Servo myservo3;
#define servoPin 5
Servo myservo4;
#define servoPin 6
Servo myservo5;
#define servoPin 7
Servo myservo6;
#define servoPin 8
Servo myservo7;
#define servoPin 9
Servo myservo8;
#define servoPin 10
#define pushButtonPin 2
int angle1 =90; // initial angle for servo
int angleStep1 =10;
const int minAngle1 = 90;
const int maxAngle1 = 120;
int angle2 =90; // initial angle for servo
int angleStep2 =10;
const int minAngle2 = 60;
const int maxAngle2 = 90;
int buttonPushed =0;
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo1.attach(3);
myservo2.attach(4);
myservo3.attach(5);
myservo4.attach(6);
myservo5.attach(7);
myservo6.attach(8);
myservo7.attach(9);
myservo8.attach(10); // attaches the servo on pin 3 to the servo object
pinMode(2,INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
}
void loop() {
if(digitalRead(2) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// change the angle for next time through the loop:
angle1 = angle1 + angleStep1;
// reverse the direction of the moving at the ends of the angle:
if (angle1 <= minAngle1 || angle1 >= maxAngle1) {
angleStep1 = -angleStep1;
buttonPushed = 0;
// change the angle for next time through the loop:
angle2 = angle2 - angleStep2;
// reverse the direction of the moving at the ends of the angle:
if (angle2 <= minAngle2 || angle2 >= maxAngle2) {
angleStep2 = -angleStep2;
buttonPushed = 0;
myservo1.write(angle1); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle1); // print the angle
Serial.println(" degree");
delay(5); // waits for the servo to get there
myservo2.write(angle2); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle2); // print the angle
Serial.println(" degree");
delay(5); // waits for the servo to get there
myservo3.write(angle1); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle1); // print the angle
Serial.println(" degree");
delay(5); // waits for the servo to get there
myservo4.write(angle2); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle2); // print the angle
Serial.println(" degree");
delay(5); // waits for the servo to get there
myservo5.write(angle1); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle1); // print the angle
Serial.println(" degree");
delay(5); // waits for the servo to get there
myservo6.write(angle2); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle2); // print the angle
Serial.println(" degree");
delay(5); // waits for the servo to get there
myservo7.write(angle1); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle1); // print the angle
Serial.println(" degree");
delay(5); // waits for the servo to get there
myservo8.write(angle2); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle2); // print the angle
Serial.println(" degree");
delay(400); // waits for the servo to get there
}
}
}
}