I am still new to this, so sorry if I have done things incorrectly.
I found this sketch somewhere, for just controlling some servos. I reduced the number of servos to four, and added the four relays.
It is used for operating points on an oo gauge layout, The relays are for switching the electrofrog sections on each point.
The problem is that when you apply power the servos seem to go to a random position until you cycle each switch, and that is a pain, as we have 12 points.
What I am looking for is a modification to make the servos go straight to the position as selected by each switch. I hope that is clear.
Thanks for your help.
// A not so simple Arduino Sketch to control 4 servos for point or signal control
// using 4 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.
// 4 relays can be added for changing the power polarity on Electro-Frog Points
// Unfortunately these relays will not work with Turnouts or switches, only points.
// 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 +5VDC, and have a pull down resistor connected to Arduino Ground
//
// If the switch moves the servo in the wrong direction reverse the limits 50 to 150 and 150 to 50
// 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[4];
byte servoPin[] = { 2, 3, 4, 5}; // pin numbers for servo signals
byte switchPin[] = { 19, 18, 17, 16}; // pin numbers for switch connections
// Uno analog pins A5 A4 A3 A2
byte servoLowPos[] = { 55, 55, 55, 55}; // degrees for low servo position S-4, S-3, S-2, S-1
byte servoHighPos[] = {145, 145, 145, 145}; // degrees for high servo position S-4, S-3, S-2, S-1
byte servoPos[4];
const int rly1Pin = 8;
const int rly2Pin = 9;
const int rly3Pin = 10;
const int rly4Pin = 11;
const int button1Pin = A2;
const int button2Pin = A3;
const int button3Pin = A4;
const int button4Pin = A5;
int button1State = 0; //variables for reading the pushbutton status
int button2State = 0; //variables for reading the pushbutton status
int button3State = 0; //variables for reading the pushbutton status
int button4State = 0; //variables for reading the pushbutton status
void setup() {
Serial.begin(9600);
setupServos();
setupSwitches();
pinMode(rly1Pin, OUTPUT); //initialize the relay pin as an output
pinMode(rly2Pin, OUTPUT); //initialize the relay pin as an output
pinMode(rly3Pin, OUTPUT); //initialize the relay pin as an output
pinMode(rly4Pin, OUTPUT); //initialize the relay pin as an output
pinMode(button1Pin, INPUT); //initialize the pushbutton pin as an input
pinMode(button2Pin, INPUT); //initialize the pushbutton pin as an input
pinMode(button3Pin, INPUT); //initialize the pushbutton pin as an input
pinMode(button4Pin, INPUT); //initialize the pushbutton pin as an input
}
void loop() {
button1State = digitalRead(button1Pin); //read the state of the pushbutton value
button2State = digitalRead(button2Pin); //read the state of the pushbutton value
button3State = digitalRead(button3Pin); //read the state of the pushbutton value
button4State = digitalRead(button4Pin); //read the state of the pushbutton value
if (button1State == HIGH) { //check if the switc is ON
//if it is, then turn the relay on
digitalWrite(rly1Pin, HIGH); }
else { digitalWrite(rly1Pin, LOW); } // turn relay off
if (button2State == HIGH) { //check if the switc is ON
//if it is, then turn the relay on
digitalWrite(rly2Pin, HIGH); }
else { digitalWrite(rly2Pin, LOW); } // turn relay off
if (button3State == HIGH) { //check if the switc is ON
//if it is, then turn the relay on
digitalWrite(rly3Pin, HIGH); }
else { digitalWrite(rly3Pin, LOW); } // turn relay off
if (button4State == HIGH) { //check if the switc is ON
//if it is, then turn the relay on
digitalWrite(rly4Pin, HIGH); }
else { digitalWrite(rly4Pin, LOW); } // turn relay off
for (byte n = 0; n < 4; 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 < 4; 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 < 4; n++) {
pinMode(switchPin[n], INPUT);
}
}