Advertisement
kartonman

DIY Turnout Motor LEDS

Sep 22nd, 2021 (edited)
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. /*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.
  2.  
  3. This sketch may be loaded and used on an Arduino Nano. By using the Analog pins as Digital pins, you may
  4. add more lights to be controlled as you wish.
  5.  
  6. The basic was sketch created by Gary Granai August 16, 2021 an later modified with much appreciated millis
  7. code modifcation help by Jim Weisenbach and Dave Brown, members of the Facebook Group, Arduino For Model Railroaders.
  8.  
  9. Wiring diagrams, video and discussion located at https://steamtraininfo.com/arduino-projects/arduino-diy-turnout-motor
  10.  
  11. Many other arduino projects can be found in the Arduino Projects Chapter at
  12. https://steamtraininfo.com/arduino-projects
  13.  
  14. You may use this sketch and modify it as long as these credits remain in place and remain unmodified.
  15. */
  16.  
  17. #include <Servo.h>
  18.  
  19. //attach servo control wire to pin 2
  20.  
  21. #define cpyellow 3 // yellow light on control panel indication turnout in divergent position.
  22. #define cpgreen 4 // greem light on control panel indication turnout in through position.
  23. #define tfgreen 5 // gren light on turnout entrance indicating straight through passage.
  24. #define tfyellow 6 // yellow light on turnout entrance indicating turnout is in the divergent position.
  25. #define tbgreen 7 // green light on turnout exit indicating straight through passage.
  26. #define tbred 8 // red light on turnout exit indicating turnout in divergent position.
  27. #define divgreen 9 // green light on divergent leg
  28. #define divred 10 // red light on divergent leg
  29. #define warning 11 // a flashing spotlight warning that the upcoming turnout is in the divergent position.
  30. #define toggle 12 // connect on off toggle swithc to this pin. May also use latching puch button
  31.  
  32. int pos;
  33. Servo myservo;
  34.  
  35. // millis
  36. int x = 1;
  37. unsigned long t1 = 0;
  38. unsigned long t2;
  39.  
  40.  
  41.  
  42.  
  43. void setup()
  44. {
  45. myservo.attach(2);
  46. pinMode(cpyellow, OUTPUT);
  47. pinMode(cpgreen, OUTPUT);
  48. pinMode(tfyellow, OUTPUT);
  49. pinMode(tfgreen, OUTPUT);
  50. pinMode(tbred, OUTPUT);
  51. pinMode(tbgreen, OUTPUT);
  52. pinMode(divred, OUTPUT);
  53. pinMode(divgreen, OUTPUT);
  54. pinMode(warning, OUTPUT);
  55. pinMode(toggle, INPUT_PULLUP); /*attaching the toggle to pin 12 eliminates the need to add a
  56. dropping resistor to the toggle.*/
  57.  
  58. digitalWrite(warning, 1);
  59. pos = 0;
  60. delay(50);
  61.  
  62. }
  63.  
  64. void loop()
  65. {
  66.  
  67. if (digitalRead(toggle) == LOW) {
  68. while (pos < 45) { // servo divergent angle. change as you wish.
  69. pos += 1;
  70. myservo.write(pos);
  71. delay(50); // sets speed of servo. change as you wish.
  72. }
  73. }
  74.  
  75.  
  76. else {
  77. while (pos > 0) { //servo though position. change as you wish.
  78. pos -= 1;
  79. myservo.write(pos);
  80. delay(50);
  81.  
  82. }
  83. }
  84.  
  85. // LED Control
  86. //Blinks warning LED
  87. if (pos > 0)
  88. {
  89. t2 = millis();
  90. if (t2 - t1 >= 1000) { // sets blink rate. Change as you wish.
  91. x = 1 - x;
  92. t1 = millis();
  93. digitalWrite(warning, x);
  94. }
  95. }
  96. // Turns off blinking warning LED
  97. else {
  98. digitalWrite(warning, LOW);
  99.  
  100. }
  101. //Turns on LEDs when turnout is in divergent position
  102. if (pos > 0) {
  103. digitalWrite(tfyellow, HIGH);
  104. digitalWrite(tfgreen, LOW);
  105. digitalWrite(tbred, HIGH);
  106. digitalWrite(tbgreen, LOW);
  107. digitalWrite(divred, LOW);
  108. digitalWrite(divgreen, HIGH);
  109. digitalWrite(cpyellow, HIGH);
  110. digitalWrite(cpgreen, LOW);
  111. }
  112. //Turns on LEDS when turnout is in divergent position
  113. else {
  114. digitalWrite(tfgreen, HIGH);
  115. digitalWrite(tfyellow, LOW);
  116. digitalWrite(tbgreen, HIGH);
  117. digitalWrite(tbred, LOW);
  118. digitalWrite(divred, HIGH);
  119. digitalWrite(divgreen, LOW);
  120. digitalWrite(cpgreen, HIGH);
  121. digitalWrite(cpyellow, LOW);
  122. }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement