Post by RC Tractor Guy on May 17, 2015 22:13:43 GMT
Here is the code I used in my 8 servo tester.
/*
Arduino Mega based 8 Servo Tester using encoders
www.rctractors.net
*/
#include <Servo.h>
// Servo Initial Angle
int servo_1_pos = 90;
int servo_2_pos = 90;
int servo_3_pos = 90;
int servo_4_pos = 90;
int servo_5_pos = 90;
int servo_6_pos = 90;
int servo_7_pos = 90;
int servo_8_pos = 90;
Servo servo_1; // create servo object to control servo 1
Servo servo_2; // create servo object to control servo 2
Servo servo_3; // create servo object to control servo 3
Servo servo_4; // create servo object to control servo 4
Servo servo_5; // create servo object to control servo 5
Servo servo_6; // create servo object to control servo 6
Servo servo_7; // create servo object to control servo 7
Servo servo_8; // create servo object to control servo 8
void setup()
{
servo_1.attach(12); // attaches servo 1 on pin 12 to the servo object
servo_2.attach(11); // attaches servo 2 on pin 11 to the servo object
servo_3.attach(10); // attaches servo 3 on pin 10 to the servo object
servo_4.attach(9); // attaches servo 4 on pin 9 to the servo object
servo_5.attach(8); // attaches servo 5 on pin 8 to the servo object
servo_6.attach(7); // attaches servo 6 on pin 7 to the servo object
servo_7.attach(6); // attaches servo 7 on pin 6 to the servo object
servo_8.attach(5); // attaches servo 8 on pin 5 to the servo object
// Encoder Pins
pinMode(22, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(23, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(24, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(25, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(26, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(27, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(28, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(29, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(30, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(31, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(32, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(33, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(34, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(35, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(36, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(37, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
// Push Button Pins
pinMode(46, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(47, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(48, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(49, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(50, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(51, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(52, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
pinMode(53, INPUT_PULLUP); // Set the pin to be an input and enable pull up resistor
}
void loop()
{
// Set Servo Positions
servo_1.write(servo_1_pos);
servo_2.write(servo_2_pos);
servo_3.write(servo_3_pos);
servo_4.write(servo_4_pos);
servo_5.write(servo_5_pos);
servo_6.write(servo_6_pos);
servo_7.write(servo_7_pos);
servo_8.write(servo_8_pos);
// Test pushbuttons
if (digitalRead(46) == LOW) { // check if the pushbutton is pressed
servo_5_pos=90; // if yes set servo to 90 degrees
}
if (digitalRead(47) == LOW) { // check if the pushbutton is pressed
servo_6_pos=90; // if yes set servo to 90 degrees
}
if (digitalRead(48) == LOW) { // check if the pushbutton is pressed
servo_7_pos=90; // if yes set servo to 90 degrees
}
if (digitalRead(49) == LOW) { // check if the pushbutton is pressed
servo_8_pos=90; // if yes set servo to 90 degrees
}
if (digitalRead(50) == LOW) { // check if the pushbutton is pressed
servo_1_pos=90; // if yes set servo to 90 degrees
}
if (digitalRead(51) == LOW) { // check if the pushbutton is pressed
servo_3_pos=90; // if yes set servo to 90 degrees
}
if (digitalRead(52) == LOW) { // check if the pushbutton is pressed
servo_2_pos=90; // if yes set servo to 90 degrees
}
if (digitalRead(53) == LOW) { // check if the pushbutton is pressed
servo_4_pos=90; // if yes set servo to 90 degrees
}
// Test encoders
// Pin 22 and 23 are outputs from one encoder
while(digitalRead(22) == LOW){ // If 22 goes low
if (digitalRead(23) == LOW){ // before 23
while(digitalRead(23) == LOW){ // Empty loop to prevent multiple steps
}
if(servo_5_pos!=180){ // Increase servo angle
servo_5_pos++;
}
}
}
// If 23 goes low before 22, decrease the servo angle
while(digitalRead(23) == LOW){
if (digitalRead(22) == LOW){
while(digitalRead(22) == LOW){
}
if(servo_5_pos!=0){
servo_5_pos--;
}
}
}
while(digitalRead(24) == LOW){
if (digitalRead(25) == LOW){
while(digitalRead(25) == LOW){
}
if(servo_6_pos!=180){
servo_6_pos++;
}
}
}
while(digitalRead(25) == LOW){
if (digitalRead(24) == LOW){
while(digitalRead(24) == LOW){
}
if(servo_6_pos!=0){
servo_6_pos--;
}
}
}
while(digitalRead(26) == LOW){
if (digitalRead(27) == LOW){
while(digitalRead(27) == LOW){
}
if(servo_7_pos!=180){
servo_7_pos++;
}
}
}
while(digitalRead(27) == LOW){
if (digitalRead(26) == LOW){
while(digitalRead(26) == LOW){
}
if(servo_7_pos!=0){
servo_7_pos--;
}
}
}
while(digitalRead(28) == LOW){
if (digitalRead(29) == LOW){
while(digitalRead(29) == LOW){
}
if(servo_8_pos!=180){
servo_8_pos++;
}
}
}
while(digitalRead(29) == LOW){
if (digitalRead(28) == LOW){
while(digitalRead(28) == LOW){
}
if(servo_8_pos!=0){
servo_8_pos--;
}
}
}
while(digitalRead(30) == LOW){
if (digitalRead(31) == LOW){
while(digitalRead(31) == LOW){
}
if(servo_1_pos!=180){
servo_1_pos++;
}
}
}
while(digitalRead(31) == LOW){
if (digitalRead(30) == LOW){
while(digitalRead(30) == LOW){
}
if(servo_1_pos!=0){
servo_1_pos--;
}
}
}
while(digitalRead(32) == LOW){
if (digitalRead(33) == LOW){
while(digitalRead(33) == LOW){
}
if(servo_2_pos!=180){
servo_2_pos++;
}
}
}
while(digitalRead(33) == LOW){
if (digitalRead(32) == LOW){
while(digitalRead(32) == LOW){
}
if(servo_2_pos!=0){
servo_2_pos--;
}
}
}
while(digitalRead(34) == LOW){
if (digitalRead(35) == LOW){
while(digitalRead(35) == LOW){
}
if(servo_3_pos!=180){
servo_3_pos++;
}
}
}
while(digitalRead(35) == LOW){
if (digitalRead(34) == LOW){
while(digitalRead(34) == LOW){
}
if(servo_3_pos!=0){
servo_3_pos--;
}
}
}
while(digitalRead(36) == LOW){
if (digitalRead(37) == LOW){
while(digitalRead(37) == LOW){
}
if(servo_4_pos!=180){
servo_4_pos++;
}
}
}
while(digitalRead(37) == LOW){
if (digitalRead(36) == LOW){
while(digitalRead(36) == LOW){
}
if(servo_4_pos!=0){
servo_4_pos--;
}
}
}
}