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 spotlightthat 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 modification help by Jim Weisenbach and Dave Brown, members of the Facebook Group,Arduino For Model Railroaders.
- Wiring diagrams, video and discussion located at Arduino DIY Switch Machines https://steamtraininfo.com/arduino-projects/arduino-diy-switch-machines
- 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 3 a pwm pin
- #define cpyellow 4 // yellow light on control panel indication turnout in divergent position.
- #define cpgreen 5 // greem light on control panel indication turnout in through position.
- #define tfgreen 6 // gren light on turnout entrance indicating straight through passage.
- #define tfyellow 7 // yellow light on turnout entrance indicating turnout is in the divergent position.
- #define tbgreen 8 // green light on turnout exit indicating straight through passage.
- #define tbred 9 // red light on turnout exit indicating turnout in divergent position.
- #define divgreen 10 // green light on divergent leg
- #define divred 11 // red light on divergent leg
- #define warning 12 // a flashing spotlight warning that the upcoming turnout is in the divergent position.
- #define toggle A5 // connect on off toggle switch to this pin. May also use latching push button
- //use
- int pos;
- Servo myservo;
- // millis
- int x = 1;
- unsigned long t1 = 0;
- unsigned long t2;
- void setup()
- {
- myservo.attach(3);
- 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); //add dropping resistor
- 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 sets blink rate. Change as you wish.
- if (pos > 0)
- {
- t2 = millis();
- if (t2 - t1 >= 1000)
- { 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