Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Turnout control with LED position indicators on each leg of the turnout plus a remote flashing warning spotlight that indicates the switch is in a divergent status.
- This sketch may be loaded and used on an Arduino Nano. By using the Analog pins as Digital pins, you may
- add more lights to be controlled as you wish.
- The basic was sketch created by Gary Granai August 16, 2021 an later modified with much appreciated millis
- code modifcation help by Jim Weisenbach and Dave Brown, members of the Facebook Group, Arduino For Model Railroaders.
- Wiring diagrams, video and discussion located at https://steamtraininfo.com/arduino-projects/arduino-diy-turnout-motor
- Many other arduino projects can be found in the Arduino Projects Chapter at
- https://steamtraininfo.com/arduino-projects
- You may use this sketch and modify it as long as these credits remain in place and remain unmodified.
- */
- #include <Servo.h>
- //attach servo control wire to pin 2
- #define cpyellow 3 // yellow light on control panel indication turnout in divergent position.
- #define cpgreen 4 // greem light on control panel indication turnout in through position.
- #define tfgreen 5 // gren light on turnout entrance indicating straight through passage.
- #define tfyellow 6 // yellow light on turnout entrance indicating turnout is in the divergent position.
- #define tbgreen 7 // green light on turnout exit indicating straight through passage.
- #define tbred 8 // red light on turnout exit indicating turnout in divergent position.
- #define divgreen 9 // green light on divergent leg
- #define divred 10 // red light on divergent leg
- #define warning 11 // a flashing spotlight warning that the upcoming turnout is in the divergent position.
- #define toggle 12 // connect on off toggle swithc to this pin. May also use latching puch button
- int pos;
- Servo myservo;
- // millis
- int x = 1;
- unsigned long t1 = 0;
- unsigned long t2;
- void setup()
- {
- myservo.attach(2);
- pinMode(cpyellow, OUTPUT);
- pinMode(cpgreen, OUTPUT);
- pinMode(tfyellow, OUTPUT);
- pinMode(tfgreen, OUTPUT);
- pinMode(tbred, OUTPUT);
- pinMode(tbgreen, OUTPUT);
- pinMode(divred, OUTPUT);
- pinMode(divgreen, OUTPUT);
- pinMode(warning, OUTPUT);
- pinMode(toggle, INPUT_PULLUP); /*attaching the toggle to pin 12 eliminates the need to add a
- dropping resistor to the toggle.*/
- digitalWrite(warning, 1);
- pos = 0;
- delay(50);
- }
- void loop()
- {
- if (digitalRead(toggle) == LOW) {
- while (pos < 45) { // servo divergent angle. change as you wish.
- pos += 1;
- myservo.write(pos);
- delay(50); // sets speed of servo. change as you wish.
- }
- }
- else {
- while (pos > 0) { //servo though position. change as you wish.
- pos -= 1;
- myservo.write(pos);
- delay(50);
- }
- }
- // LED Control
- //Blinks warning LED
- if (pos > 0)
- {
- t2 = millis();
- if (t2 - t1 >= 1000) { // sets blink rate. Change as you wish.
- x = 1 - x;
- t1 = millis();
- digitalWrite(warning, x);
- }
- }
- // Turns off blinking warning LED
- else {
- digitalWrite(warning, LOW);
- }
- //Turns on LEDs when turnout is in divergent position
- if (pos > 0) {
- digitalWrite(tfyellow, HIGH);
- digitalWrite(tfgreen, LOW);
- digitalWrite(tbred, HIGH);
- digitalWrite(tbgreen, LOW);
- digitalWrite(divred, LOW);
- digitalWrite(divgreen, HIGH);
- digitalWrite(cpyellow, HIGH);
- digitalWrite(cpgreen, LOW);
- }
- //Turns on LEDS when turnout is in divergent position
- else {
- digitalWrite(tfgreen, HIGH);
- digitalWrite(tfyellow, LOW);
- digitalWrite(tbgreen, HIGH);
- digitalWrite(tbred, LOW);
- digitalWrite(divred, HIGH);
- digitalWrite(divgreen, LOW);
- digitalWrite(cpgreen, HIGH);
- digitalWrite(cpyellow, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement