kartonman

levelcrossing3trackpotentiometer

Jan 12th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. levelcrossing3trackpotentiometer
  2.  
  3. #include <Servo.h>
  4.  
  5. #define GATE_SERVO_SPEED_POT_VALUE 30 // [ms] lower number is higher servo speed
  6. #define GATE_CLOSE_DELAY_POT_VALUE 1500 // [ms] time between start blinking and gate closing
  7. #define BLINK_SPEED_POT_VALUE 400 // [ms] smaller number is faster blinking
  8. #define END_OF_TRAIN_DELAY 2000 // [ms] time to wait before deciding this was the end of the train
  9. #define NUM_SENSORS 6 // two sensors per track, one left and one right of the gate
  10. #define FLASH_DELAY 2000 // [ms] change as you wish
  11.  
  12. byte sensor_pin[NUM_SENSORS] = {2, 3, 4, 5, 6, 7}; // sensor "OUT" wires connect to these pins.
  13. /*sensor pin colors blue, white, purple, brown, yellow, gray */
  14.  
  15. #define LED1_PIN 8 // one of the flashing lights.
  16. #define LED2_PIN 9 // the second flashing light.
  17.  
  18. #define SERVO1_PIN 10
  19. #define SERVO2_PIN 11
  20.  
  21. #define POTENTIOMETER_1 A0
  22. #define POTENTIOMETER_2 A1
  23. #define POTENTIOMETER_3 A2
  24. #define POTENTIOMETER_4 A3
  25. #define POTENTIOMETER_5 A4
  26. #define POTENTIOMETER_6 A5
  27. #define POTENTIOMETER_7 A6
  28.  
  29. // Change this by adjusting potentiometers
  30. byte GATE_OPEN_ANGLE_1; // POT 1
  31. byte GATE_CLOSED_ANGLE_1; // POT 2
  32. byte GATE_OPEN_ANGLE_2; // POT 3
  33. byte GATE_CLOSED_ANGLE_2; // POT 4
  34.  
  35. byte state = 1;
  36. byte train_counter, n;
  37. byte led1, led2, blink_enabled;
  38. byte angle1 = GATE_OPEN_ANGLE_1;
  39. byte setpoint1 = GATE_OPEN_ANGLE_1;
  40. byte angle2 = GATE_OPEN_ANGLE_2;
  41. byte setpoint2 = GATE_OPEN_ANGLE_2;
  42. byte sensor_state[NUM_SENSORS];
  43. byte end_of_train[NUM_SENSORS];
  44. unsigned long time_to_blink;
  45. unsigned long time_to_close_gate;
  46. unsigned long time_for_servo_step;
  47. unsigned long time_end_of_train[NUM_SENSORS];
  48.  
  49. Servo gate_servo1;
  50. Servo gate_servo2;
  51.  
  52. #define Bell 12 // Replace 12 with the appropriate pin number for the bell
  53.  
  54. void setup() {
  55. pinMode(LED1_PIN, OUTPUT);
  56. pinMode(LED2_PIN, OUTPUT);
  57. pinMode(Bell, OUTPUT);
  58. for (byte i = 0; i < NUM_SENSORS; i++) pinMode(sensor_pin[i], INPUT_PULLUP);
  59. gate_servo1.attach(SERVO1_PIN);
  60. gate_servo2.attach(SERVO2_PIN);
  61. delay(1000);
  62. gate_servo1.detach();
  63. gate_servo2.detach();
  64. Serial.begin(9600);
  65. Serial.println("Railway Crossing Control Ready");
  66. Serial.println();
  67. Serial.println("Waiting for train");
  68. for (byte i = 0; i < NUM_SENSORS; i++) sensor_state[i] = 1;
  69. }
  70.  
  71. void loop() { {
  72.  
  73. for (byte i = 0; i < NUM_SENSORS; i++) {
  74. if (sensor_state[i] == 1) { // detect arrival of new train
  75. if (!digitalRead(sensor_pin[i])) { // train detected
  76. train_counter++;
  77. sensor_state[i] = 0;
  78. if (i % 2) n = i - 1;
  79. else n = i + 1;
  80. sensor_state[n] = 2; // buddy sensor departure detection enabled
  81. Serial.print("Arrival: ");
  82. Serial.println(i);
  83. Serial.print("Trains: ");
  84. Serial.println(train_counter);
  85. }
  86. } else if (sensor_state[i] > 1) {
  87. if (!digitalRead(sensor_pin[i])) { // departure detected
  88. time_end_of_train[i] = millis() + (unsigned long)END_OF_TRAIN_DELAY;
  89. if (i % 2) n = i - 1;
  90. else n = i + 1;
  91. sensor_state[n] = 1; // buddy sensor enabled again
  92. if (sensor_state[i] == 2) {
  93. Serial.print("Departure: ");
  94. Serial.println(i);
  95. }
  96. sensor_state[i] = 3;
  97. }
  98. if (sensor_state[i] == 3) // decide if end of train has passed based on a timer
  99. if (millis() > time_end_of_train[i]) end_of_train[i] = 1;
  100. if (end_of_train[i]) { // this takes care train_counter-- is executed only once
  101. train_counter--;
  102. end_of_train[i] = 0;
  103. sensor_state[i] = 1;
  104. Serial.print("Trains: ");
  105. Serial.println(train_counter);
  106. }
  107. }
  108. }
  109.  
  110. switch (state) {
  111. case 1: // Gates open. Not blinking. Waiting for train.
  112. if (train_counter) { // A train is detected.
  113. Serial.println("Blinking started");
  114. blink_enabled = 1;
  115. time_to_close_gate = millis() + (unsigned long)GATE_CLOSE_DELAY_POT_VALUE;
  116. state = 2;
  117. }
  118. break;
  119.  
  120. case 2: // Blinking. Wait until it's time to close the gates
  121. if (millis() > time_to_close_gate) { // Gate delay time has passed
  122. Serial.println("Gate1 closing");
  123. gate_servo1.attach(SERVO1_PIN);
  124. setpoint1 = GATE_CLOSED_ANGLE_1;
  125. state = 3;
  126. }
  127. break;
  128.  
  129. case 3: // Gate1 closing. Blinking.
  130. if (angle1 == setpoint1) { // Gate1 closed
  131. gate_servo1.detach();
  132. Serial.println("Gate1 closed");
  133. Serial.println("Gate2 closing");
  134. gate_servo2.attach(SERVO2_PIN);
  135. setpoint2 = GATE_CLOSED_ANGLE_2;
  136. state = 4;
  137. }
  138. break;
  139.  
  140. case 4: // Gate2 closing. Blinking.
  141. if (angle2 == setpoint2) { // Gate2 is fully closed
  142. gate_servo2.detach();
  143. Serial.println("Gate2 closed");
  144. state = 5;
  145. }
  146. break;
  147.  
  148. case 5: // Gate2 closed. Blinking. Wait until all trains are gone.
  149. if (!train_counter) { // End of last train detected
  150. Serial.println("Gate2 opening");
  151. gate_servo2.attach(SERVO2_PIN);
  152. setpoint2 = GATE_OPEN_ANGLE_2;
  153. state = 6;
  154. }
  155. break;
  156.  
  157. case 6: // Gate2 opening. Blinking.
  158. if (train_counter) state = 3; // Another train is coming
  159. else if (angle2 == setpoint2) { // Gate2 open
  160. gate_servo2.detach();
  161. Serial.println("Gate2 open");
  162. Serial.println("Gate1 opening");
  163. gate_servo1.attach(SERVO1_PIN);
  164. setpoint1 = GATE_OPEN_ANGLE_1;
  165. state = 7;
  166. }
  167. break;
  168.  
  169. case 7: // Gate1 opening. Blinking.
  170. if (train_counter) state = 2; // Another train is coming
  171. else if (angle1 == setpoint1) { // Gate1 open
  172. gate_servo1.detach();
  173. Serial.println("Gate1 open");
  174. Serial.println("Stop blinking");
  175. blink_enabled = 0; // Stop blinking
  176. led1 = 0;
  177. led2 = 0;
  178. Serial.println();
  179. Serial.println("Waiting for train");
  180. state = 1;
  181. }
  182. break;
  183. }
  184.  
  185. if (millis() > time_for_servo_step) {
  186. time_for_servo_step = millis() + (unsigned long)GATE_SERVO_SPEED_POT_VALUE;
  187. if (angle1 < setpoint1) angle1++;
  188. if (angle1 > setpoint1) angle1--;
  189. if (angle2 < setpoint2) angle2++;
  190. if (angle2 > setpoint2) angle2--;
  191. gate_servo1.write(angle1);
  192. gate_servo2.write(angle2);
  193. }
  194.  
  195. if (blink_enabled == 1) {
  196. if (millis() > time_to_blink) {
  197. time_to_blink = millis() + (unsigned long)BLINK_SPEED_POT_VALUE;
  198. led1 = !led1;
  199. led2 = !led1;
  200. digitalWrite(Bell, HIGH);
  201. }
  202.  
  203. digitalWrite(LED1_PIN, led1);
  204. digitalWrite(LED2_PIN, led2);
  205. digitalWrite(Bell, HIGH);
  206. }
  207.  
  208. if (blink_enabled != 1) {
  209. digitalWrite(Bell, LOW);
  210. digitalWrite(LED1_PIN, LOW);
  211. digitalWrite(LED2_PIN, LOW);
  212. }
  213. }}
  214.  
Advertisement
Add Comment
Please, Sign In to add comment