kartonman

Turnout with position lights

Jul 29th, 2023
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1.  
  2.  
  3.  
  4. /*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.
  5.  
  6. 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.
  7.  
  8. 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.
  9.  
  10. Wiring diagrams, video and discussion located at Arduino DIY Switch Machines https://steamtraininfo.com/arduino-projects/arduino-diy-switch-machines
  11.  
  12. You may use this sketch and modify it as long as these credits remain in place and remain unmodified. */
  13. #include <Servo.h>
  14.  
  15. //attach servo control wire to pin 3 a pwm pin
  16.  
  17. #define cpyellow 4 // yellow light on control panel indication turnout in divergent position.
  18. #define cpgreen 5 // greem light on control panel indication turnout in through position.
  19. #define tfgreen 6 // gren light on turnout entrance indicating straight through passage.
  20. #define tfyellow 7 // yellow light on turnout entrance indicating turnout is in the divergent position.
  21. #define tbgreen 8 // green light on turnout exit indicating straight through passage.
  22. #define tbred 9 // red light on turnout exit indicating turnout in divergent position.
  23. #define divgreen 10 // green light on divergent leg
  24. #define divred 11 // red light on divergent leg
  25. #define warning 12 // a flashing spotlight warning that the upcoming turnout is in the divergent position.
  26. #define toggle A5 // connect on off toggle switch to this pin. May also use latching push button
  27. //use
  28.  
  29. int pos;
  30. Servo myservo;
  31.  
  32. // millis
  33. int x = 1;
  34. unsigned long t1 = 0;
  35. unsigned long t2;
  36.  
  37.  
  38.  
  39. void setup()
  40. {
  41. myservo.attach(3);
  42. pinMode(cpyellow, OUTPUT);
  43. pinMode(cpgreen, OUTPUT);
  44. pinMode(tfyellow, OUTPUT);
  45. pinMode(tfgreen, OUTPUT);
  46. pinMode(tbred, OUTPUT);
  47. pinMode(tbgreen, OUTPUT);
  48. pinMode(divred, OUTPUT);
  49. pinMode(divgreen, OUTPUT);
  50. pinMode(warning, OUTPUT);
  51. pinMode(toggle, INPUT-PULLUP); //add dropping resistor
  52.  
  53. digitalWrite(warning, 1);
  54. pos = 0;
  55. delay(50);
  56.  
  57. }
  58.  
  59. void loop()
  60. {
  61.  
  62. if (digitalRead(toggle) == LOW) {
  63. while (pos < 45) { // servo divergent angle. change as you wish.
  64. pos += 1;
  65. myservo.write(pos);
  66. delay(50); // sets speed of servo. change as you wish.
  67. }
  68. }
  69.  
  70. else {
  71. while (pos > 0) { //servo though position. change as you wish.
  72. pos -= 1;
  73. myservo.write(pos);
  74. delay(50);
  75.  
  76. }
  77. }
  78.  
  79. // LED Control
  80. //Blinks warning LED sets blink rate. Change as you wish.
  81. if (pos > 0)
  82. {
  83. t2 = millis();
  84. if (t2 - t1 >= 1000)
  85. { x = 1 - x;
  86. t1 = millis();
  87. digitalWrite(warning, x);
  88. }
  89. }
  90. // Turns off blinking warning LED
  91. else {
  92. digitalWrite(warning, LOW);
  93.  
  94. }
  95. //Turns on LEDs when turnout is in divergent position
  96. if (pos > 0) {
  97. digitalWrite(tfyellow, HIGH);
  98. digitalWrite(tfgreen, LOW);
  99. digitalWrite(tbred, HIGH);
  100. digitalWrite(tbgreen, LOW);
  101. digitalWrite(divred, LOW);
  102. digitalWrite(divgreen, HIGH);
  103. digitalWrite(cpyellow, HIGH);
  104. digitalWrite(cpgreen, LOW);
  105. }
  106. //Turns on LEDS when turnout is in divergent position
  107. else {
  108. digitalWrite(tfgreen, HIGH);
  109. digitalWrite(tfyellow, LOW);
  110. digitalWrite(tbgreen, HIGH);
  111. digitalWrite(tbred, LOW);
  112. digitalWrite(divred, HIGH);
  113. digitalWrite(divgreen, LOW);
  114. digitalWrite(cpgreen, HIGH);
  115. digitalWrite(cpyellow, LOW);
  116. }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment