// a simple Arduino Sketch to control 6 servos for point or signal control
// using 6 toggle switches.
// each servo moves between one of two position depending on the switch setting
// each servo can have different limits to it's movement
// the servos must not be powered from the Arduino, it can't supply enough current
// typical wiring for the servos is like this
//
// Arduino servo pin -------------------Servo Signal wire (Orange or white perhaps)
// Power supply Positive -------------- Servo Positive (Centre wire - red)
// Power supply Ground -------------- Servo Ground (Black or Brown wire)
// |
// Arduino Ground ----------- (NB Arduino Ground must be connected to power supply ground)
// The toggle switches should be connected so that when switched they connect the Arduino switch pin to ground
//
// Arduino Switch pin ---------- ---------- Arduino Ground
//
// If the switch moves the servo in the wrong direction turn the switch around in it's mounting
// The high and low positions can be set separately for each servo by editing the data in the
// arrays servoLowPos and servoHighPos. In the program as written they are all set to 50 and 150 degrees.
#include <Servo.h>
Servo pointServo[9];
byte servoPin[] = { 2, 3, 4, 5, 6, 7}; // pin numbers for servo signals
byte switchPin[] = { 19, 18, 17, 16, 15, 14}; // pin numbers for switch connections
// Uno analog pins A5 A4 A3 A2 A1 A0
byte servoLowPos[] = { 50, 50, 50, 50, 50, 50}; // degrees for low servo position
byte servoHighPos[] = {150, 150, 150, 150, 150, 150}; // degrees for high servo position
byte servoPos[6];
int Relay1 = 8;
int Relay2 = 9;
int Relay3 = 10;
int Relay4 = 11;
int Relay5 = 12;
int Relay6 = 13;
int Relay1state = 0;
int sw24state = 0;
int sw24 = 0;
void setup() {
Serial.begin(9600); //start Serial in case we need to print debugging info
setupServos();
setupSwitches();
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, INPUT);
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);
}
void loop() {
byte swA = digitalRead(switchPin[14]);
if (swA == 1)
{
digitalWrite (Relay1, 1);
}
if (swA != 1)
{
digitalWrite (Relay1, 0);
}
for (byte n = 0; n < 6; n++) {
boolean servoMove = false;
byte sw = digitalRead(switchPin[n]);
if (sw == HIGH) {
if (servoPos[n] != servoHighPos[n]) { // check if the position has changed
servoMove = true;
servoPos[n] = servoHighPos[n];
}
}
else { // if sw == LOW
if (servoPos[n] != servoLowPos[n]) { // check if the position has changed
servoMove = true;
servoPos[n] = servoLowPos[n];
}
}
if (servoMove) { // only move the servo if the switch has changed
pointServo[n].write(servoPos[n]);
}
}
}
void setupServos() {
for (byte n = 0; n < 6; n++) {
pointServo[n].attach(servoPin[n]);
servoPos[n] = servoLowPos[n]; // this is just a starting value and may be over-ridden immediately by a switch
}
}
void setupSwitches() {
for (byte n = 0; n < 6; n++) {
pinMode(switchPin[n], INPUT);
}
}