Hi folks. This a presently pretty successful project that controls 2 servos simultaneously to open or close model railroad track switches on a button push, and changes the colors of led signals as it does.
It works!
I've been told that instead of using an analog input for the single button with a 1K pull down resistor, I should have used a digital input. I was also informed that this script is "needlessly complex". Since I based it on others peoples work and I just modified it, they are probably correct.
I'll probably like to expand it in the future with a few more servos and some sensors. It also might be fun to automate some things once the sensors are in. leds have 1k resistors on positive for 5 volts.
Since I am really brand new at this, any suggestions from which I might learn some things would be deeply appreciated.
Thanks! Script follows:
//Controls Two Turnouts And LED Signals
//Activates two servos at once and turns signal leds from green, yellow, to red
//Modified by Artie Langston 7-16-2023
//kd0gy@arrl.net
/*****************************************************************/
const int button = A0;
const int AredledPin = 8; //the A red led connect to pin8
const int AyelledPin = 9; //the A yellow led connect to pin9
const int AgrnledPin = 10; //the A green led connect to pin10
const int BredledPin = 11; //the B red led connect to pin11
const int ByelledPin = 12; //the B yellow led connect to pin12
const int BgrnledPin = 13; //the B green led connect to pin13
/*************************************************************/
int start1 = 90;
int stop1 = 165;
#include <Servo.h>
Servo servo_one; // create servo object to control servo one
Servo servo_two; // create servo object to control servo two
int pos = 90; // variable to store the servo position
/*****************************************************************/
void setup() {
Serial.begin(9600);
servo_one.attach(6);
servo_two.attach(7);
pinMode(AredledPin, OUTPUT); //initialize the A led pin as output
pinMode(AyelledPin, OUTPUT); //initialize the A led pin as output
pinMode(AgrnledPin, OUTPUT); //initialize the A led pin as output
pinMode(BredledPin, OUTPUT); //initialize the B led pin as output
pinMode(ByelledPin, OUTPUT); //initialize the B led pin as output
pinMode(BgrnledPin, OUTPUT); //initialize the B led pin as output
}
enum SWITCHSTATES {
ST_OFF1,
ST_OFF2,
ST_STRAIGHT,
ST_TURN,
};
/**************************************/
SWITCHSTATES switchState = ST_OFF1;
/**************************************/
void loop() {
int buttonRead = analogRead(button);
delay(200);
switch (switchState) {
case ST_OFF1:
switchoff1(buttonRead); //sets up changes to ST_OFF1
digitalWrite(AgrnledPin, LOW); //turn the A the green led on
digitalWrite(AyelledPin, HIGH); //turn the A the yellow led off
digitalWrite(AredledPin, HIGH); //turn the A red led off
digitalWrite(BgrnledPin, HIGH); //turn the B green led on
digitalWrite(ByelledPin, HIGH); //turn the B yellow led off
digitalWrite(BredledPin, LOW); //turn the B red led off
break;
case ST_OFF2:
switchoff2(buttonRead); //sets up changes to ST_OFF2
digitalWrite(AyelledPin, HIGH); //turn the A yellow led on
digitalWrite(ByelledPin, HIGH); //turn the B yellow led on
break;
case ST_STRAIGHT:
switchstraight(buttonRead); //sets up changes to ST_STRAIGHT
digitalWrite(AgrnledPin, LOW); //turn the A green led on
digitalWrite(AyelledPin, LOW); //turn the A yellow led on
digitalWrite(AredledPin, HIGH); //turn the A red led off
digitalWrite(BgrnledPin, LOW); //turn the B green led on
digitalWrite(ByelledPin, LOW); //turn the B yellow led on
digitalWrite(BredledPin, HIGH); //turn the B red led off
break;
case ST_TURN:
switchturn(buttonRead); // sets up changes to ST_TURN
digitalWrite(AgrnledPin, HIGH); //turn the A green led on
digitalWrite(AyelledPin, LOW); //turn the A yellow led on
digitalWrite(AredledPin, LOW); //turn the A red led off
digitalWrite(BgrnledPin, LOW); //turn the B green led on
digitalWrite(ByelledPin, LOW); //turn the B green led on
digitalWrite(BredledPin, HIGH); //turn the B red led off
break;
}
}
/**************************************************************/
void switchoff1(int buttonRead) {
servo_one.write(start1);
servo_two.write(start1);
if (buttonRead > 500) {
switchState = ST_TURN; //switches to different state
digitalWrite(AgrnledPin, HIGH); //turn the A green led on
digitalWrite(AyelledPin, LOW); //turn the A yellow led off
digitalWrite(AredledPin, LOW); //turn the A red led off
digitalWrite(BgrnledPin, LOW); //turn the B green led on
digitalWrite(ByelledPin, LOW); //turn the B yellow led on
digitalWrite(BredledPin, HIGH); //turn the B red led off
}
}
/****************************************************************/
void switchturn(int buttonRead) {
for (pos = start1; pos <= stop1; pos += 1) { // goes from 90 degrees to 165 degrees in steps of 1 degree
servo_one.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 3ms for the servo to reach the position
servo_two.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 3ms for the servo to reach the position
}
switchState = ST_OFF2; //chages to ST_OFF2
}
/*********************************************************************************/
void switchoff2(int buttonRead) {
servo_one.write(stop1);
servo_two.write(stop1);
if (buttonRead > 500) {
switchState = ST_STRAIGHT; //switches to Straight
}
}
/**********************************************************************/
void switchstraight(int buttonRead) {
for (pos = stop1; pos >= start1; pos -= 1) { // goes from 165 degrees to 90 degrees
servo_one.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 3ms for the servo to reach the position
servo_two.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 3ms for the servo to reach the position
switchState = ST_OFF1; //chages to ST_OFF1
}
}