Components and supplies
Generic Arduino sensor modules (Hall or IR)
5 mm LED: Green
Free URB unit
Resistor 221 ohm
Arduino Nano R3
Generic Motor-driver L298
Resistor 10k ohm
5 mm LED: Red
Apps and platforms
Arduino IDE
Project description
Code
LOCAL unit sketch
arduino
1// -------------------------------------------------- // 2// WWW.ARDUINORAILWAYCONTROL.COM // 3// local_urb.ino // 4// V.2.0 11/24/2019 // 5// // 6// SMALL INTERLOCKING // 7// For URB unit V.2.FINAL // 8// https://arduinorailwaycontrol.com/urb_unit.html // 9// // 10// Author: Steve Massikker // 11// -------------------------------------------------- // 12void(* resetFunc) (void) = 0; 13 14#include <Wire.h> 15#include <Servo.h> 16 17// SERVO 18#define JUNCTION_EN 4 19Servo J1; // TURNOUT A 20Servo J2; // TURNOUT B 21 22// SIGNALS 23#define S_LA_RED 3 // SIGNAL LINE A 24#define S_LB_RED 2 // SIGNAL LINE B 25#define S_B3_RED 6 // SIGNAL BLOCK 3 26#define S_B3_GRN 5 27#define S_OUT_RED 10 // SIGNAL OUT 28#define S_OUT_GRN 9 29#define S_B1_RED 17 // SIGNAL BLOCK 1 30#define S_B1_GRN 16 31#define S_B2_RED 15 // SIGNAL BLOCK 2 32#define S_B2_GRN 14 33 34 35// RELAY 36#define RELAY_LA 13 // LINE A 37#define RELAY_LB 12 // LINE B 38 39// Variables 40byte dataFromI2C; 41unsigned long millisJunctions, timerStation; 42bool switch_A = true, 43 switch_B = false, 44 flag_blink_AB = false, 45 flag_change_junc = false; 46 47bool flag_interlocking = false, 48 flag_station_open = true; 49 50bool open_block1 = true, 51 open_block2 = true, 52 open_block3 = true; 53 54void setup() { 55 56 // Initializie Hardware Serial & I2C 57 Serial.begin(9600); 58 Wire.begin(2); // Set address #2 59 Wire.onReceive(receiveI2C); 60 61 // Initialize pins 62 pinMode (JUNCTION_EN, OUTPUT); 63 pinMode (RELAY_LA, OUTPUT); 64 pinMode (RELAY_LB, OUTPUT); 65 pinMode (S_LA_RED, OUTPUT); 66 pinMode (S_LB_RED, OUTPUT); 67 pinMode (S_B3_GRN, OUTPUT); 68 pinMode (S_B3_RED, OUTPUT); 69 pinMode (S_B1_RED, OUTPUT); 70 pinMode (S_B1_GRN, OUTPUT); 71 pinMode (S_B2_RED, OUTPUT); 72 pinMode (S_B2_GRN, OUTPUT); 73 pinMode (S_OUT_RED, OUTPUT); 74 pinMode (S_OUT_GRN, OUTPUT); 75 76 // Initialize Servos 77 J1.attach(7); 78 J2.attach(8); 79 80 // Set default 81 J1.write(180); J2.write(0); 82 delay(900); 83 digitalWrite(JUNCTION_EN, LOW); 84 digitalWrite(S_LA_RED, LOW); 85 digitalWrite(S_LB_RED, HIGH); 86 digitalWrite(S_B1_GRN, LOW); 87 digitalWrite(S_B1_RED, HIGH); 88 digitalWrite(S_B2_GRN, LOW); 89 digitalWrite(S_B2_RED, HIGH); 90 digitalWrite(S_B3_GRN, LOW); 91 digitalWrite(S_B3_RED, HIGH); 92 digitalWrite(S_OUT_GRN, HIGH); 93 digitalWrite(S_OUT_RED, LOW); 94 digitalWrite(RELAY_LA, LOW); // Line A is ON 95 digitalWrite(RELAY_LB, HIGH); // Line B is OFF 96} 97 98void loop() { 99 100// ---- COMMAND PARSING 101 if (dataFromI2C != 0) { 102 103 switch (dataFromI2C) { 104 105 // RESET 106 case 99: resetFunc(); break; 107 108 // SWITCH A 109 case 30: J1.write(0); 110 delay(50); 111 switch_A = false; 112 flag_change_junc = true; 113 digitalWrite(JUNCTION_EN, HIGH); 114 Serial.println("SWITCH A Branch"); 115 millisJunctions = millis(); 116 break; 117 118 case 31: J1.write(180); 119 delay(50); 120 switch_A = true; 121 flag_change_junc = true; 122 digitalWrite(JUNCTION_EN, HIGH); 123 Serial.println("SWITCH A Straight"); 124 millisJunctions = millis(); 125 break; 126 127 // SWITCH B 128 case 32: J2.write(0); 129 delay(50); 130 switch_B = false; 131 flag_change_junc = true; 132 digitalWrite(JUNCTION_EN, HIGH); 133 Serial.println("SWITCH B Branch"); 134 millisJunctions = millis(); 135 break; 136 137 case 33: J2.write(180); 138 delay(50); 139 switch_B = true; 140 flag_change_junc = true; 141 digitalWrite(JUNCTION_EN, HIGH); 142 Serial.println("SWITCH B Straight"); 143 millisJunctions= millis(); 144 break; 145 146 // MODE 147 case 50: flag_interlocking = false; 148 break; 149 case 51: flag_interlocking = true; 150 break; 151 152 // BLOCKS STATE 153 case 101: open_block1 = false; 154 Serial.println("BLOCK 1"); 155 break; 156 case 102: open_block2 = false; 157 open_block1 = true; 158 Serial.println("BLOCK 2"); 159 break; 160 case 103: open_block3 = false; 161 open_block2 = true; 162 Serial.println("BLOCK 3"); 163 break; 164 case 104: open_block3 = true; 165 flag_station_open = false; 166 timerStation = millis(); 167 Serial.println("OUT"); 168 break; 169 } 170 171 dataFromI2C = 0; 172 } 173 174// ---- SIGNALS 175 // BLOCK 1 176 if (flag_interlocking) { 177 if (open_block1) { 178 digitalWrite(S_B1_GRN, HIGH); 179 digitalWrite(S_B1_RED, LOW); 180 } 181 else { 182 digitalWrite(S_B1_GRN, LOW); 183 digitalWrite(S_B1_RED, HIGH); 184 } 185 // BLOCK 2 186 if (open_block2) { 187 digitalWrite(S_B2_GRN, HIGH); 188 digitalWrite(S_B2_RED, LOW); 189 } 190 else { 191 digitalWrite(S_B2_GRN, LOW); 192 digitalWrite(S_B2_RED, HIGH); 193 } 194 // BLOCK 3 195 if (open_block3) { 196 digitalWrite(S_B3_GRN, HIGH); 197 digitalWrite(S_B3_RED, LOW); 198 } 199 else { 200 digitalWrite(S_B3_GRN, LOW); 201 digitalWrite(S_B3_RED, HIGH); 202 } 203 } 204 else { 205 digitalWrite(S_B1_GRN, LOW); 206 digitalWrite(S_B1_RED, HIGH); 207 digitalWrite(S_B2_GRN, LOW); 208 digitalWrite(S_B2_RED, HIGH); 209 digitalWrite(S_B3_GRN, LOW); 210 digitalWrite(S_B3_RED, HIGH); 211 } 212 213 // OUT 214 if (flag_station_open) { 215 digitalWrite(S_OUT_GRN, HIGH); 216 digitalWrite(S_OUT_RED, LOW); 217 } 218 else { 219 digitalWrite(S_OUT_GRN, LOW); 220 digitalWrite(S_OUT_RED, HIGH); 221 } 222 223// ---- RELAY 224 if (flag_change_junc) { 225 226 if (switch_A && !switch_B) { // Line A 227 digitalWrite(S_LA_RED, LOW); 228 digitalWrite(S_LB_RED, HIGH); // RED 229 digitalWrite(RELAY_LA, LOW); 230 digitalWrite(RELAY_LB, HIGH); 231 flag_blink_AB = false; 232 } 233 else if (!switch_A && switch_B) { // Line B 234 digitalWrite(S_LA_RED, HIGH); // RED 235 digitalWrite(S_LB_RED, LOW); 236 digitalWrite(RELAY_LA, HIGH); 237 digitalWrite(RELAY_LB, LOW); 238 flag_blink_AB = false; 239 } 240 else { // Undefined 241 digitalWrite(RELAY_LA, HIGH); 242 digitalWrite(RELAY_LB, HIGH); 243 flag_blink_AB = true; 244 } 245 246 flag_change_junc = false; 247 } 248 249 if (millis() > (millisJunctions + 900)) digitalWrite(JUNCTION_EN, LOW); 250 if (millis() > (timerStation + 15000)) flag_station_open = true; // Delay 15 sec. 251 252 if (flag_blink_AB) { 253 static bool tick_blink; 254 static unsigned long blinkMillis; 255 if (millis() > (blinkMillis + 500)) { // 500 - blinking optimal period 256 tick_blink = !tick_blink; 257 blinkMillis = millis(); 258 } 259 digitalWrite(S_LA_RED, tick_blink); 260 digitalWrite(S_LB_RED, !tick_blink); 261 } 262 263} 264 265// ----------- FUNCTIONS ----------- // 266 267void receiveI2C(int howMany) { 268 while (Wire.available() > 0) { 269 dataFromI2C = Wire.read(); 270 } 271} 272
COMM unit sketch
arduino
1// -------------------------------------------------- // 2// WWW.ARDUINORAILWAYCONTROL.COM // 3// comm_urb.ino // 4// V.2.0 11/24/2019 // 5// // 6// SMALL INTERLOCKING // 7// For URB unit V.2.FINAL // 8// 122Hz Thrust contol // 9// https://arduinorailwaycontrol.com/urb_unit.html // 10// // 11// Author: Steve Massikker // 12// -------------------------------------------------- // 13void(* resetFunc) (void) = 0; 14 15#include <Wire.h> 16#include <SoftwareSerial.h> 17 18// Bluetooth module 19SoftwareSerial Bluetooth(12, 13); // D12 - RX | D13 - TX 20 21// PWM (SPEED) 22#define MD_1_ENA 10 // LINES A, B 23#define MD_1_ENB 9 // BLOCK 1 24#define MD_2_ENA 11 // BLOCK 3 25#define MD_2_ENB 3 // BLOCK 2 26// DIRECTION DRIVER A 27#define MD_1_IN1 4 28#define MD_1_IN2 2 29// DIRECTION DRIVER B 30#define MD_12_IN3_IN3_IN1 5 31#define MD_12_IN4_IN4_IN2 6 32// SENSORS 33#define SNS_1 17 34#define SNS_2 14 35#define SNS_3 15 36#define SNS_4 7 37#define SNS_5 8 38#define SNS_6 16 39#define SNS_7 A6 40 41// Variables 42int addressI2C; 43byte dataToI2C; 44bool stringComplete = false; 45String inputString = ""; 46 47 // 24 speed 48 byte speedArrayA [] = {30,40,50,60,80,110,140,160,180,200,220,255}; 49 byte speedTrainA = 0; 50 51 // Interlocking 52 unsigned long timerBlock1, timerBlock2, timerBlock3, timerStation; 53 byte speedBlock1, speedBlock2, speedBlock3; 54 55 // Dispather AWS 56 bool flag_interlocking = false; 57 bool flag_station_open = true; 58 59 // Sensors 60 unsigned long timer_protected_sensor1, timer_protected_sensor3, 61 timer_protected_sensor5, timer_protected_sensor7; 62 bool protected_sensor1, protected_sensor3, 63 protected_sensor5, protected_sensor7; 64 bool sensor_block1_IN, sensor_block2_IN, sensor_block3_IN, 65 sensor_block1_OUT, sensor_block2_OUT, sensor_block3_OUT, 66 sensor_OUT; 67 bool latch_stop_block1, latch_stop_block2, latch_stop_block3; 68 69 // Latches 70 bool open_block1 = true, open_block2 = true, open_block3 = true; 71 72 // TURNOUTS & SET DEFAULT POSITIONS 73 bool switch_A = true, switch_B = false; 74 75 76void setup() { 77 78 // Initializie Serials & I2C 79 Serial.begin(9600); 80 Bluetooth.begin(9600); 81 inputString.reserve(4); 82 Wire.begin(); // Set as Master 83 84 // Initialize pins 85 pinMode (MD_1_ENA, OUTPUT); 86 pinMode (MD_1_ENB, OUTPUT); 87 pinMode (MD_2_ENA, OUTPUT); 88 pinMode (MD_2_ENB, OUTPUT); 89 pinMode (MD_1_IN1, OUTPUT); 90 pinMode (MD_1_IN2, OUTPUT); 91 pinMode (MD_12_IN3_IN3_IN1, OUTPUT); 92 pinMode (MD_12_IN4_IN4_IN2, OUTPUT); 93 pinMode (SNS_1, INPUT); 94 pinMode (SNS_2, INPUT); 95 pinMode (SNS_3, INPUT); 96 pinMode (SNS_4, INPUT); 97 pinMode (SNS_5, INPUT); 98 pinMode (SNS_6, INPUT); 99 pinMode (SNS_7, INPUT); 100 101 // Set PWM frequency for D3, D9, D10, D11 102 // Timer 1 divisor to 256 for PWM frequency of 122.55 Hz (9, 10) 103 TCCR1B = TCCR1B & B11111000 | B00000100; 104 // Timer 2 divisor to 256 for PWM frequency of 122.55 Hz (3, 11) 105 TCCR2B = TCCR2B & B11111000 | B00000100; 106 107 // Set default direction to FORWARD (POLARITY) NON Interlocking 108 digitalWrite(MD_1_IN1, LOW); 109 digitalWrite(MD_1_IN2, HIGH); 110 digitalWrite(MD_12_IN3_IN3_IN1, LOW); 111 digitalWrite(MD_12_IN4_IN4_IN2, HIGH); 112 113} 114 115void loop() { 116 117 // ---- START PARSING INCOMING APP COMMANDS 118 if (stringComplete) { 119 // RESET 120 if (inputString =="999z") { 121 dataToI2C = 99; 122 addressI2C = 2; 123 sendDataViaI2C(); 124 resetFunc(); 125 } 126 127 // FUNCTIONS 128 if (inputString.charAt(0) =='a') { 129 // Speed 130 if (inputString.charAt(1) =='0') { 131 if (inputString.charAt(2) =='0') speedTrainA = 0; 132 if (inputString.charAt(2) =='2') speedTrainA = speedArrayA[0]; 133 if (inputString.charAt(2) =='4') speedTrainA = speedArrayA[1]; 134 if (inputString.charAt(2) =='6') speedTrainA = speedArrayA[2]; 135 if (inputString.charAt(2) =='8') speedTrainA = speedArrayA[3]; 136 } 137 if (inputString.charAt(1) =='1') { 138 if (inputString.charAt(2) =='0') speedTrainA = speedArrayA[4]; 139 if (inputString.charAt(2) =='2') speedTrainA = speedArrayA[5]; 140 if (inputString.charAt(2) =='4') speedTrainA = speedArrayA[6]; 141 if (inputString.charAt(2) =='6') speedTrainA = speedArrayA[7]; 142 if (inputString.charAt(2) =='8') speedTrainA = speedArrayA[8]; 143 } 144 if (inputString.charAt(1) =='2') { 145 if (inputString.charAt(2) =='0') speedTrainA = speedArrayA[9]; 146 if (inputString.charAt(2) =='2') speedTrainA = speedArrayA[10]; 147 if (inputString.charAt(2) =='4') speedTrainA = speedArrayA[11]; 148 } 149 150 // Direction and Stop 151 if (inputString.charAt(1) =='d') { 152 if (inputString.charAt(2) =='f') { // (f) Forward 153 digitalWrite(MD_1_IN1, LOW); 154 digitalWrite(MD_1_IN2, HIGH); 155 digitalWrite(MD_12_IN3_IN3_IN1, LOW); 156 digitalWrite(MD_12_IN4_IN4_IN2, HIGH); 157 } 158 if (inputString.charAt(2) =='b') { // (b) Backward - Interlocking 159 digitalWrite(MD_1_IN1, HIGH); 160 digitalWrite(MD_1_IN2, LOW); 161 digitalWrite(MD_12_IN3_IN3_IN1, HIGH); 162 digitalWrite(MD_12_IN4_IN4_IN2, LOW); 163 } 164 if (inputString.charAt(2) =='s') { // (s) Stop button 165 speedTrainA = 0; 166 } 167 168 analogWrite(MD_1_ENA, speedTrainA); // Throttle Driver A 169 170 if (!flag_interlocking) { 171 analogWrite(MD_1_ENB, speedTrainA); 172 analogWrite(MD_2_ENB, speedTrainA); 173 if (flag_station_open) analogWrite(MD_2_ENA, speedTrainA); 174 else analogWrite(MD_2_ENA,0); 175 } 176 } 177 } 178 179 if (inputString.charAt(0) =='j') { 180 // Switch A 181 if (inputString.charAt(1) =='a') { 182 if (inputString.charAt(2) =='0') { // Branch direction 183 switch_A = false; 184 Bluetooth.print("a0z"); // Feedback to App 185 Serial.print("a0z"); 186 addressI2C = 2; dataToI2C = 30; sendDataViaI2C(); 187 } 188 if (inputString.charAt(2) =='1') { // Throw direction 189 switch_A = true; 190 Bluetooth.print("a1z"); // Feedback to App 191 Serial.print("a1z"); 192 addressI2C = 2; dataToI2C = 31; sendDataViaI2C(); 193 } 194 } 195 196 // Switch B 197 if (inputString.charAt(1) =='b') { 198 if (inputString.charAt(2) =='0') { 199 switch_B = false; 200 Bluetooth.print("b0z"); 201 Serial.print("b0z"); 202 addressI2C = 2; dataToI2C = 32; sendDataViaI2C(); 203 } 204 if (inputString.charAt(2) =='1') { 205 switch_B = true; 206 Bluetooth.print("b1z"); 207 Serial.print("b1z"); 208 addressI2C = 2; dataToI2C = 33; sendDataViaI2C(); 209 } 210 } 211 } 212 213 // AUTOMATE ON | OFF 214 if (inputString.charAt(0) =='f') { 215 if (inputString.charAt(1) =='a') { 216 217 if (inputString.charAt(2) =='0') { 218 flag_interlocking = false; 219 speedBlock1 = 0; speedBlock2 = 0; speedBlock3 = 0; 220 addressI2C = 2; dataToI2C = 50; sendDataViaI2C(); // AUTOMATE OFF 221 } 222 if (inputString.charAt(2) =='1') { 223 flag_interlocking = true; 224 speedBlock1 = 180; speedBlock2 = 140; speedBlock3 = 160; 225 digitalWrite(MD_1_IN1, HIGH); 226 digitalWrite(MD_1_IN2, LOW); 227 digitalWrite(MD_12_IN3_IN3_IN1, HIGH); 228 digitalWrite(MD_12_IN4_IN4_IN2, LOW); 229 protected_sensor1 = false; protected_sensor3 = false; 230 protected_sensor5 = false; protected_sensor7 = false; 231 latch_stop_block1 = false; latch_stop_block2 = false; latch_stop_block3 = false; 232 open_block1 = true; open_block2 = true; open_block3 = true; 233 addressI2C = 2; dataToI2C = 51; sendDataViaI2C(); // AUTOMATE ON 234 } 235 236 analogWrite(MD_1_ENB, speedBlock1); 237 analogWrite(MD_2_ENB, speedBlock2); 238 analogWrite(MD_2_ENA, speedBlock3); 239 } 240 } 241 242 dataToI2C = 0; 243 inputString = ""; 244 stringComplete = false; 245 } 246 247 // ---- MAIN BLOCK 248 if (flag_interlocking) { 249 250 // CHECK SENSORS STATES 251 252 if (digitalRead(SNS_1) == LOW) { // BLOCK 1 | HALL 253 sensor_block1_IN = true; 254 open_block1 = false; 255 //Serial.println("SENSOR 1"); 256 } 257 else sensor_block1_IN = false; 258 if (digitalRead(SNS_2) == LOW) { // HALL 259 sensor_block1_OUT = true; 260 latch_stop_block1 = true; 261 //Serial.println("SENSOR 2"); 262 } 263 else sensor_block1_OUT = false; 264 265 if (digitalRead(SNS_3) == HIGH) { // BLOCK 2 | IR 266 sensor_block2_IN = true; 267 open_block2 = false; 268 //Serial.println("SENSOR 3"); 269 } 270 else sensor_block2_IN = false; 271 if (digitalRead(SNS_4) == LOW) { // HALL 272 sensor_block2_OUT = true; 273 latch_stop_block2 = true; 274 //Serial.println("SENSOR 4"); 275 } 276 else sensor_block2_OUT = false; 277 278 if (digitalRead(SNS_5) == HIGH) { // BLOCK 3 | IR 279 sensor_block3_IN = true; 280 open_block3 = false; 281 //Serial.println("SENSOR 5"); 282 } 283 else sensor_block3_IN = false; 284 if (digitalRead(SNS_6) == LOW) { // HALL 285 sensor_block3_OUT = true; 286 latch_stop_block3 = true; 287 //Serial.println("SENSOR 6"); 288 } 289 else sensor_block3_OUT = false; 290 291 // OUT 292 if (analogRead(SNS_7) > 550) { // ANALOGUE HALL 293 sensor_OUT = true; 294 //Serial.println("SENSOR 7"); 295 } 296 else sensor_OUT = false; 297 298 // RESET BLOCK LATCHES 299 if (sensor_block2_IN) open_block1 = true; 300 if (sensor_block3_IN) open_block2 = true; 301 if (sensor_OUT) open_block3 = true; 302 303 // RESET AUTOSTOP LATCHES 304 if (sensor_block2_IN) latch_stop_block1 = false; 305 if (sensor_block3_IN) latch_stop_block2 = false; 306 if (sensor_OUT) latch_stop_block3 = false; 307 308 // ---- SIGNALS 309 // SIGNAL 1 310 if (sensor_block1_IN && !protected_sensor1) { 311 timer_protected_sensor1 = millis(); 312 protected_sensor1 = true; 313 addressI2C = 2; dataToI2C = 101; sendDataViaI2C(); 314 } 315 // SIGNAL 2 316 if (sensor_block2_IN && !protected_sensor3) { 317 timer_protected_sensor3 = millis(); 318 protected_sensor3 = true; 319 addressI2C = 2; dataToI2C = 102; sendDataViaI2C(); 320 } 321 // SIGNAL 3 322 if (sensor_block3_IN && !protected_sensor5) { 323 timer_protected_sensor5 = millis(); 324 protected_sensor5 = true; 325 addressI2C = 2; dataToI2C = 103; sendDataViaI2C(); 326 } 327 // SIGNAL 4 328 if (sensor_OUT && !protected_sensor7) { 329 timer_protected_sensor7 = millis(); 330 timerStation = millis(); 331 protected_sensor7 = true; 332 flag_station_open = false; 333 addressI2C = 2; dataToI2C = 104; sendDataViaI2C(); 334 } 335 336 // AUTOSTOP 337 // BLOCK 1 338 if (!open_block2) { 339 if (latch_stop_block1) { 340 if (millis() > (timerBlock1 + 100)) { 341 timerBlock1 = millis(); 342 if (speedBlock1 > 40) speedBlock1 = speedBlock1 - 25; 343 else speedBlock1 = 0; 344 } 345 } 346 } 347 else speedBlock1 = 180; 348 349 // BLOCK 2 350 if (!open_block3) { 351 if (latch_stop_block2) { 352 if (millis() > (timerBlock2 + 100)) { 353 timerBlock2 = millis(); 354 if (speedBlock2 > 60) speedBlock2 = speedBlock2 - 30; 355 else speedBlock2 = 0; 356 } 357 } 358 } 359 else speedBlock2 = 140; 360 361 // BLOCK 3 362 if (!flag_station_open) { 363 if (latch_stop_block3) { 364 if (millis() > (timerBlock3 + 100)) { 365 timerBlock3 = millis(); 366 if (speedBlock3 > 40) speedBlock3 = speedBlock3 - 18; 367 else speedBlock3 = 0; 368 } 369 } 370 } 371 else speedBlock3 = 160; 372 373 analogWrite(MD_1_ENB, speedBlock1); 374 analogWrite(MD_2_ENB, speedBlock2); 375 analogWrite(MD_2_ENA, speedBlock3); 376 } 377 378 // ---- TIMERS 379 if (millis() > (timer_protected_sensor1 + 1000)) protected_sensor1 = false; 380 if (millis() > (timer_protected_sensor3 + 1500)) protected_sensor3 = false; 381 if (millis() > (timer_protected_sensor5 + 1500)) protected_sensor5 = false; 382 if (millis() > (timer_protected_sensor7 + 1000)) protected_sensor7 = false; 383 if (millis() > (timerStation + 15000)) flag_station_open = true; // Delay 15 sec. 384 385 bluetoothEvent(); 386 387} 388 389// ----------- FUNCTIONS ----------- // 390void serialEvent() { 391 if (Serial.available()) { 392 char inChar = (char)Serial.read(); 393 inputString += inChar; 394 if (inChar == 'z') { 395 stringComplete = true; 396 } 397 } 398} 399 400void bluetoothEvent() { 401 if (Bluetooth.available()) { 402 char inChar = (char)Bluetooth.read(); 403 inputString += inChar; 404 if (inChar == 'z') { 405 //Serial.println(inputString); // Command from App 406 stringComplete = true; 407 } 408 } 409} 410 411void sendDataViaI2C() { 412 Wire.beginTransmission(addressI2C); 413 Wire.write(dataToI2C); 414 Wire.endTransmission(); 415}
COMM unit sketch
arduino
1// -------------------------------------------------- // 2// WWW.ARDUINORAILWAYCONTROL.COM 3 // 4// comm_urb.ino // 5// 6 V.2.0 11/24/2019 // 7// // 8// 9 SMALL INTERLOCKING // 10// For URB unit V.2.FINAL 11 // 12// 122Hz Thrust contol // 13// 14 https://arduinorailwaycontrol.com/urb_unit.html // 15// // 16// 17 Author: Steve Massikker // 18// -------------------------------------------------- 19 // 20void(* resetFunc) (void) = 0; 21 22#include <Wire.h> 23#include <SoftwareSerial.h> 24 25// 26 Bluetooth module 27SoftwareSerial Bluetooth(12, 13); // D12 - RX | D13 - TX 28 29// 30 PWM (SPEED) 31#define MD_1_ENA 10 // LINES A, B 32#define MD_1_ENB 9 // BLOCK 33 1 34#define MD_2_ENA 11 // BLOCK 3 35#define MD_2_ENB 3 // BLOCK 2 36// DIRECTION 37 DRIVER A 38#define MD_1_IN1 4 39#define MD_1_IN2 2 40// DIRECTION DRIVER B 41#define 42 MD_12_IN3_IN3_IN1 5 43#define MD_12_IN4_IN4_IN2 6 44// SENSORS 45#define SNS_1 46 17 47#define SNS_2 14 48#define SNS_3 15 49#define SNS_4 7 50#define SNS_5 8 51#define 52 SNS_6 16 53#define SNS_7 A6 54 55// Variables 56int addressI2C; 57byte dataToI2C; 58bool 59 stringComplete = false; 60String inputString = ""; 61 62 // 24 speed 63 byte 64 speedArrayA [] = {30,40,50,60,80,110,140,160,180,200,220,255}; 65 byte speedTrainA 66 = 0; 67 68 // Interlocking 69 unsigned long timerBlock1, timerBlock2, timerBlock3, 70 timerStation; 71 byte speedBlock1, speedBlock2, speedBlock3; 72 73 // Dispather 74 AWS 75 bool flag_interlocking = false; 76 bool flag_station_open = true; 77 78 79 // Sensors 80 unsigned long timer_protected_sensor1, timer_protected_sensor3, 81 82 timer_protected_sensor5, timer_protected_sensor7; 83 bool protected_sensor1, 84 protected_sensor3, 85 protected_sensor5, protected_sensor7; 86 bool sensor_block1_IN, 87 sensor_block2_IN, sensor_block3_IN, 88 sensor_block1_OUT, sensor_block2_OUT, 89 sensor_block3_OUT, 90 sensor_OUT; 91 bool latch_stop_block1, latch_stop_block2, 92 latch_stop_block3; 93 94 // Latches 95 bool open_block1 = true, open_block2 96 = true, open_block3 = true; 97 98 // TURNOUTS & SET DEFAULT POSITIONS 99 bool 100 switch_A = true, switch_B = false; 101 102 103void setup() { 104 105 // Initializie 106 Serials & I2C 107 Serial.begin(9600); 108 Bluetooth.begin(9600); 109 inputString.reserve(4); 110 111 Wire.begin(); // Set as Master 112 113 // Initialize pins 114 pinMode (MD_1_ENA, 115 OUTPUT); 116 pinMode (MD_1_ENB, OUTPUT); 117 pinMode (MD_2_ENA, OUTPUT); 118 pinMode 119 (MD_2_ENB, OUTPUT); 120 pinMode (MD_1_IN1, OUTPUT); 121 pinMode (MD_1_IN2, OUTPUT); 122 123 pinMode (MD_12_IN3_IN3_IN1, OUTPUT); 124 pinMode (MD_12_IN4_IN4_IN2, OUTPUT); 125 126 pinMode (SNS_1, INPUT); 127 pinMode (SNS_2, INPUT); 128 pinMode (SNS_3, 129 INPUT); 130 pinMode (SNS_4, INPUT); 131 pinMode (SNS_5, INPUT); 132 pinMode (SNS_6, 133 INPUT); 134 pinMode (SNS_7, INPUT); 135 136 // Set PWM frequency for D3, D9, 137 D10, D11 138 // Timer 1 divisor to 256 for PWM frequency of 122.55 Hz (9, 10) 139 140 TCCR1B = TCCR1B & B11111000 | B00000100; 141 // Timer 2 divisor to 256 for 142 PWM frequency of 122.55 Hz (3, 11) 143 TCCR2B = TCCR2B & B11111000 | B00000100; 144 145 146 // Set default direction to FORWARD (POLARITY) NON Interlocking 147 digitalWrite(MD_1_IN1, 148 LOW); 149 digitalWrite(MD_1_IN2, HIGH); 150 digitalWrite(MD_12_IN3_IN3_IN1, LOW); 151 152 digitalWrite(MD_12_IN4_IN4_IN2, HIGH); 153 154} 155 156void loop() { 157 158 159 // ---- START PARSING INCOMING APP COMMANDS 160 if (stringComplete) { 161 // 162 RESET 163 if (inputString =="999z") { 164 dataToI2C = 99; 165 addressI2C 166 = 2; 167 sendDataViaI2C(); 168 resetFunc(); 169 } 170 171 // 172 FUNCTIONS 173 if (inputString.charAt(0) =='a') { 174 // Speed 175 176 if (inputString.charAt(1) =='0') { 177 if (inputString.charAt(2) =='0') 178 speedTrainA = 0; 179 if (inputString.charAt(2) =='2') speedTrainA = speedArrayA[0]; 180 181 if (inputString.charAt(2) =='4') speedTrainA = speedArrayA[1]; 182 if 183 (inputString.charAt(2) =='6') speedTrainA = speedArrayA[2]; 184 if (inputString.charAt(2) 185 =='8') speedTrainA = speedArrayA[3]; 186 } 187 if (inputString.charAt(1) 188 =='1') { 189 if (inputString.charAt(2) =='0') speedTrainA = speedArrayA[4]; 190 191 if (inputString.charAt(2) =='2') speedTrainA = speedArrayA[5]; 192 if 193 (inputString.charAt(2) =='4') speedTrainA = speedArrayA[6]; 194 if (inputString.charAt(2) 195 =='6') speedTrainA = speedArrayA[7]; 196 if (inputString.charAt(2) =='8') 197 speedTrainA = speedArrayA[8]; 198 } 199 if (inputString.charAt(1) =='2') 200 { 201 if (inputString.charAt(2) =='0') speedTrainA = speedArrayA[9]; 202 203 if (inputString.charAt(2) =='2') speedTrainA = speedArrayA[10]; 204 if 205 (inputString.charAt(2) =='4') speedTrainA = speedArrayA[11]; 206 } 207 208 209 // Direction and Stop 210 if (inputString.charAt(1) =='d') { 211 if 212 (inputString.charAt(2) =='f') { // (f) Forward 213 digitalWrite(MD_1_IN1, 214 LOW); 215 digitalWrite(MD_1_IN2, HIGH); 216 digitalWrite(MD_12_IN3_IN3_IN1, 217 LOW); 218 digitalWrite(MD_12_IN4_IN4_IN2, HIGH); 219 } 220 if 221 (inputString.charAt(2) =='b') { // (b) Backward - Interlocking 222 digitalWrite(MD_1_IN1, 223 HIGH); 224 digitalWrite(MD_1_IN2, LOW); 225 digitalWrite(MD_12_IN3_IN3_IN1, 226 HIGH); 227 digitalWrite(MD_12_IN4_IN4_IN2, LOW); 228 } 229 if 230 (inputString.charAt(2) =='s') { // (s) Stop button 231 speedTrainA = 0; 232 233 } 234 235 analogWrite(MD_1_ENA, speedTrainA); // Throttle Driver 236 A 237 238 if (!flag_interlocking) { 239 analogWrite(MD_1_ENB, 240 speedTrainA); 241 analogWrite(MD_2_ENB, speedTrainA); 242 if (flag_station_open) 243 analogWrite(MD_2_ENA, speedTrainA); 244 else analogWrite(MD_2_ENA,0); 245 246 } 247 } 248 } 249 250 if (inputString.charAt(0) =='j') 251 { 252 // Switch A 253 if (inputString.charAt(1) =='a') { 254 if 255 (inputString.charAt(2) =='0') { // Branch direction 256 switch_A = false; 257 258 Bluetooth.print("a0z"); // Feedback to App 259 Serial.print("a0z"); 260 261 addressI2C = 2; dataToI2C = 30; sendDataViaI2C(); 262 } 263 if 264 (inputString.charAt(2) =='1') { // Throw direction 265 switch_A = true; 266 267 Bluetooth.print("a1z"); // Feedback to App 268 Serial.print("a1z"); 269 270 addressI2C = 2; dataToI2C = 31; sendDataViaI2C(); 271 } 272 273 } 274 275 // Switch B 276 if (inputString.charAt(1) =='b') { 277 278 if (inputString.charAt(2) =='0') { 279 switch_B = false; 280 281 Bluetooth.print("b0z"); 282 Serial.print("b0z"); 283 addressI2C 284 = 2; dataToI2C = 32; sendDataViaI2C(); 285 } 286 if (inputString.charAt(2) 287 =='1') { 288 switch_B = true; 289 Bluetooth.print("b1z"); 290 291 Serial.print("b1z"); 292 addressI2C = 2; dataToI2C = 293 33; sendDataViaI2C(); 294 } 295 } 296 } 297 298 // AUTOMATE 299 ON | OFF 300 if (inputString.charAt(0) =='f') { 301 if (inputString.charAt(1) 302 =='a') { 303 304 if (inputString.charAt(2) =='0') { 305 flag_interlocking 306 = false; 307 speedBlock1 = 0; speedBlock2 = 0; speedBlock3 = 0; 308 addressI2C 309 = 2; dataToI2C = 50; sendDataViaI2C(); // AUTOMATE OFF 310 } 311 if 312 (inputString.charAt(2) =='1') { 313 flag_interlocking = true; 314 speedBlock1 315 = 180; speedBlock2 = 140; speedBlock3 = 160; 316 digitalWrite(MD_1_IN1, 317 HIGH); 318 digitalWrite(MD_1_IN2, LOW); 319 digitalWrite(MD_12_IN3_IN3_IN1, 320 HIGH); 321 digitalWrite(MD_12_IN4_IN4_IN2, LOW); 322 protected_sensor1 323 = false; protected_sensor3 = false; 324 protected_sensor5 = false; protected_sensor7 325 = false; 326 latch_stop_block1 = false; latch_stop_block2 = false; latch_stop_block3 327 = false; 328 open_block1 = true; open_block2 = true; open_block3 = true; 329 330 addressI2C = 2; dataToI2C = 51; sendDataViaI2C(); // AUTOMATE 331 ON 332 } 333 334 analogWrite(MD_1_ENB, speedBlock1); 335 analogWrite(MD_2_ENB, 336 speedBlock2); 337 analogWrite(MD_2_ENA, speedBlock3); 338 } 339 } 340 341 342 dataToI2C = 0; 343 inputString = ""; 344 stringComplete = false; 345 346 } 347 348 // ---- MAIN BLOCK 349 if (flag_interlocking) { 350 351 // CHECK 352 SENSORS STATES 353 354 if (digitalRead(SNS_1) == LOW) { // BLOCK 1 | HALL 355 356 sensor_block1_IN = true; 357 open_block1 = false; 358 //Serial.println("SENSOR 359 1"); 360 } 361 else sensor_block1_IN = false; 362 if (digitalRead(SNS_2) 363 == LOW) { // HALL 364 sensor_block1_OUT = true; 365 latch_stop_block1 366 = true; 367 //Serial.println("SENSOR 2"); 368 } 369 else sensor_block1_OUT 370 = false; 371 372 if (digitalRead(SNS_3) == HIGH) { // BLOCK 2 | IR 373 sensor_block2_IN 374 = true; 375 open_block2 = false; 376 //Serial.println("SENSOR 3"); 377 378 } 379 else sensor_block2_IN = false; 380 if (digitalRead(SNS_4) == LOW) 381 { // HALL 382 sensor_block2_OUT = true; 383 latch_stop_block2 = true; 384 385 //Serial.println("SENSOR 4"); 386 } 387 else sensor_block2_OUT 388 = false; 389 390 if (digitalRead(SNS_5) == HIGH) { // BLOCK 3 | IR 391 392 sensor_block3_IN = true; 393 open_block3 = false; 394 //Serial.println("SENSOR 395 5"); 396 } 397 else sensor_block3_IN = false; 398 if (digitalRead(SNS_6) 399 == LOW) { // HALL 400 sensor_block3_OUT = true; 401 latch_stop_block3 402 = true; 403 //Serial.println("SENSOR 6"); 404 } 405 else sensor_block3_OUT 406 = false; 407 408 // OUT 409 if (analogRead(SNS_7) > 550) { // ANALOGUE 410 HALL 411 sensor_OUT = true; 412 //Serial.println("SENSOR 7"); 413 } 414 415 else sensor_OUT = false; 416 417 // RESET BLOCK LATCHES 418 419 if (sensor_block2_IN) open_block1 = true; 420 if (sensor_block3_IN) open_block2 421 = true; 422 if (sensor_OUT) open_block3 = true; 423 424 // RESET AUTOSTOP 425 LATCHES 426 if (sensor_block2_IN) latch_stop_block1 = false; 427 if (sensor_block3_IN) 428 latch_stop_block2 = false; 429 if (sensor_OUT) latch_stop_block3 = false; 430 431 432 // ---- SIGNALS 433 // SIGNAL 1 434 if (sensor_block1_IN && !protected_sensor1) 435 { 436 timer_protected_sensor1 = millis(); 437 protected_sensor1 = true; 438 439 addressI2C = 2; dataToI2C = 101; sendDataViaI2C(); 440 } 441 // 442 SIGNAL 2 443 if (sensor_block2_IN && !protected_sensor3) { 444 timer_protected_sensor3 445 = millis(); 446 protected_sensor3 = true; 447 addressI2C = 2; dataToI2C 448 = 102; sendDataViaI2C(); 449 } 450 // SIGNAL 3 451 if (sensor_block3_IN 452 && !protected_sensor5) { 453 timer_protected_sensor5 = millis(); 454 protected_sensor5 455 = true; 456 addressI2C = 2; dataToI2C = 103; sendDataViaI2C(); 457 } 458 459 // SIGNAL 4 460 if (sensor_OUT && !protected_sensor7) { 461 timer_protected_sensor7 462 = millis(); 463 timerStation = millis(); 464 protected_sensor7 = true; 465 466 flag_station_open = false; 467 addressI2C = 2; dataToI2C = 468 104; sendDataViaI2C(); 469 } 470 471 // AUTOSTOP 472 // BLOCK 1 473 if 474 (!open_block2) { 475 if (latch_stop_block1) { 476 if (millis() > (timerBlock1 477 + 100)) { 478 timerBlock1 = millis(); 479 if (speedBlock1 > 40) 480 speedBlock1 = speedBlock1 - 25; 481 else speedBlock1 = 0; 482 } 483 484 } 485 } 486 else speedBlock1 = 180; 487 488 // BLOCK 2 489 if 490 (!open_block3) { 491 if (latch_stop_block2) { 492 if (millis() > (timerBlock2 493 + 100)) { 494 timerBlock2 = millis(); 495 if (speedBlock2 > 60) 496 speedBlock2 = speedBlock2 - 30; 497 else speedBlock2 = 0; 498 } 499 500 } 501 } 502 else speedBlock2 = 140; 503 504 // BLOCK 3 505 if 506 (!flag_station_open) { 507 if (latch_stop_block3) { 508 if (millis() 509 > (timerBlock3 + 100)) { 510 timerBlock3 = millis(); 511 if (speedBlock3 512 > 40) speedBlock3 = speedBlock3 - 18; 513 else speedBlock3 = 0; 514 } 515 516 } 517 } 518 else speedBlock3 = 160; 519 520 analogWrite(MD_1_ENB, 521 speedBlock1); 522 analogWrite(MD_2_ENB, speedBlock2); 523 analogWrite(MD_2_ENA, 524 speedBlock3); 525 } 526 527 // ---- TIMERS 528 if (millis() > (timer_protected_sensor1 529 + 1000)) protected_sensor1 = false; 530 if (millis() > (timer_protected_sensor3 531 + 1500)) protected_sensor3 = false; 532 if (millis() > (timer_protected_sensor5 533 + 1500)) protected_sensor5 = false; 534 if (millis() > (timer_protected_sensor7 535 + 1000)) protected_sensor7 = false; 536 if (millis() > (timerStation + 15000)) 537 flag_station_open = true; // Delay 15 sec. 538 539 bluetoothEvent(); 540 541} 542 543// 544 ----------- FUNCTIONS ----------- // 545void serialEvent() { 546 if (Serial.available()) 547 { 548 char inChar = (char)Serial.read(); 549 inputString += inChar; 550 if 551 (inChar == 'z') { 552 stringComplete = true; 553 } 554 } 555} 556 557void 558 bluetoothEvent() { 559 if (Bluetooth.available()) { 560 char inChar = (char)Bluetooth.read(); 561 562 inputString += inChar; 563 if (inChar == 'z') { 564 //Serial.println(inputString); 565 // Command from App 566 stringComplete = true; 567 } 568 } 569} 570 571void 572 sendDataViaI2C() { 573 Wire.beginTransmission(addressI2C); 574 Wire.write(dataToI2C); 575 576 Wire.endTransmission(); 577}
LOCAL unit sketch
arduino
1// -------------------------------------------------- // 2// WWW.ARDUINORAILWAYCONTROL.COM 3 // 4// local_urb.ino // 5// 6 V.2.0 11/24/2019 // 7// // 8// 9 SMALL INTERLOCKING // 10// For URB unit V.2.FINAL 11 // 12// https://arduinorailwaycontrol.com/urb_unit.html 13 // 14// // 15// Author: 16 Steve Massikker // 17// -------------------------------------------------- 18 // 19void(* resetFunc) (void) = 0; 20 21#include <Wire.h> 22#include <Servo.h> 23 24// 25 SERVO 26#define JUNCTION_EN 4 27Servo J1; // TURNOUT A 28Servo J2; // TURNOUT 29 B 30 31// SIGNALS 32#define S_LA_RED 3 // SIGNAL LINE A 33#define S_LB_RED 2 34 // SIGNAL LINE B 35#define S_B3_RED 6 // SIGNAL BLOCK 3 36#define S_B3_GRN 5 37#define 38 S_OUT_RED 10 // SIGNAL OUT 39#define S_OUT_GRN 9 40#define S_B1_RED 17 // SIGNAL 41 BLOCK 1 42#define S_B1_GRN 16 43#define S_B2_RED 15 // SIGNAL BLOCK 2 44#define 45 S_B2_GRN 14 46 47 48// RELAY 49#define RELAY_LA 13 // LINE A 50#define RELAY_LB 51 12 // LINE B 52 53// Variables 54byte dataFromI2C; 55unsigned long millisJunctions, 56 timerStation; 57bool switch_A = true, 58 switch_B = false, 59 flag_blink_AB 60 = false, 61 flag_change_junc = false; 62 63bool flag_interlocking = false, 64 65 flag_station_open = true; 66 67bool open_block1 = true, 68 open_block2 69 = true, 70 open_block3 = true; 71 72void setup() { 73 74 // Initializie 75 Hardware Serial & I2C 76 Serial.begin(9600); 77 Wire.begin(2); // Set address 78 #2 79 Wire.onReceive(receiveI2C); 80 81 // Initialize pins 82 pinMode (JUNCTION_EN, 83 OUTPUT); 84 pinMode (RELAY_LA, OUTPUT); 85 pinMode (RELAY_LB, OUTPUT); 86 87 pinMode (S_LA_RED, OUTPUT); 88 pinMode (S_LB_RED, OUTPUT); 89 pinMode (S_B3_GRN, 90 OUTPUT); 91 pinMode (S_B3_RED, OUTPUT); 92 pinMode (S_B1_RED, OUTPUT); 93 pinMode 94 (S_B1_GRN, OUTPUT); 95 pinMode (S_B2_RED, OUTPUT); 96 pinMode (S_B2_GRN, OUTPUT); 97 98 pinMode (S_OUT_RED, OUTPUT); 99 pinMode (S_OUT_GRN, OUTPUT); 100 101 // Initialize 102 Servos 103 J1.attach(7); 104 J2.attach(8); 105 106 // Set default 107 J1.write(180); 108 J2.write(0); 109 delay(900); 110 digitalWrite(JUNCTION_EN, LOW); 111 digitalWrite(S_LA_RED, 112 LOW); 113 digitalWrite(S_LB_RED, HIGH); 114 digitalWrite(S_B1_GRN, LOW); 115 116 digitalWrite(S_B1_RED, HIGH); 117 digitalWrite(S_B2_GRN, LOW); 118 digitalWrite(S_B2_RED, 119 HIGH); 120 digitalWrite(S_B3_GRN, LOW); 121 digitalWrite(S_B3_RED, HIGH); 122 123 digitalWrite(S_OUT_GRN, HIGH); 124 digitalWrite(S_OUT_RED, LOW); 125 digitalWrite(RELAY_LA, 126 LOW); // Line A is ON 127 digitalWrite(RELAY_LB, HIGH); // Line B is OFF 128} 129 130void 131 loop() { 132 133// ---- COMMAND PARSING 134 if (dataFromI2C != 0) { 135 136 137 switch (dataFromI2C) { 138 139 // RESET 140 case 99: resetFunc(); 141 break; 142 143 // SWITCH A 144 case 30: J1.write(0); 145 delay(50); 146 147 switch_A = false; 148 flag_change_junc = true; 149 digitalWrite(JUNCTION_EN, 150 HIGH); 151 Serial.println("SWITCH A Branch"); 152 millisJunctions 153 = millis(); 154 break; 155 156 case 31: J1.write(180); 157 delay(50); 158 159 switch_A = true; 160 flag_change_junc = true; 161 162 digitalWrite(JUNCTION_EN, HIGH); 163 Serial.println("SWITCH 164 A Straight"); 165 millisJunctions = millis(); 166 break; 167 168 169 // SWITCH B 170 case 32: J2.write(0); 171 delay(50); 172 switch_B 173 = false; 174 flag_change_junc = true; 175 digitalWrite(JUNCTION_EN, 176 HIGH); 177 Serial.println("SWITCH B Branch"); 178 millisJunctions 179 = millis(); 180 break; 181 182 case 33: J2.write(180); 183 delay(50); 184 185 switch_B = true; 186 flag_change_junc = true; 187 188 digitalWrite(JUNCTION_EN, HIGH); 189 Serial.println("SWITCH 190 B Straight"); 191 millisJunctions= millis(); 192 break; 193 194 195 // MODE 196 case 50: flag_interlocking = false; 197 break; 198 199 case 51: flag_interlocking = true; 200 break; 201 202 // 203 BLOCKS STATE 204 case 101: open_block1 = false; 205 Serial.println("BLOCK 206 1"); 207 break; 208 case 102: open_block2 = false; 209 open_block1 210 = true; 211 Serial.println("BLOCK 2"); 212 break; 213 214 case 103: open_block3 = false; 215 open_block2 = true; 216 Serial.println("BLOCK 217 3"); 218 break; 219 case 104: open_block3 = true; 220 flag_station_open 221 = false; 222 timerStation = millis(); 223 Serial.println("OUT"); 224 225 break; 226 } 227 228 dataFromI2C = 0; 229 } 230 231// ---- 232 SIGNALS 233 // BLOCK 1 234 if (flag_interlocking) { 235 if (open_block1) { 236 237 digitalWrite(S_B1_GRN, HIGH); 238 digitalWrite(S_B1_RED, LOW); 239 240 } 241 else { 242 digitalWrite(S_B1_GRN, LOW); 243 digitalWrite(S_B1_RED, 244 HIGH); 245 } 246 // BLOCK 2 247 if (open_block2) { 248 digitalWrite(S_B2_GRN, 249 HIGH); 250 digitalWrite(S_B2_RED, LOW); 251 } 252 else { 253 digitalWrite(S_B2_GRN, 254 LOW); 255 digitalWrite(S_B2_RED, HIGH); 256 } 257 // BLOCK 3 258 if 259 (open_block3) { 260 digitalWrite(S_B3_GRN, HIGH); 261 digitalWrite(S_B3_RED, 262 LOW); 263 } 264 else { 265 digitalWrite(S_B3_GRN, LOW); 266 digitalWrite(S_B3_RED, 267 HIGH); 268 } 269 } 270 else { 271 digitalWrite(S_B1_GRN, LOW); 272 digitalWrite(S_B1_RED, 273 HIGH); 274 digitalWrite(S_B2_GRN, LOW); 275 digitalWrite(S_B2_RED, HIGH); 276 277 digitalWrite(S_B3_GRN, LOW); 278 digitalWrite(S_B3_RED, HIGH); 279 } 280 281 282 // OUT 283 if (flag_station_open) { 284 digitalWrite(S_OUT_GRN, HIGH); 285 286 digitalWrite(S_OUT_RED, LOW); 287 } 288 else { 289 digitalWrite(S_OUT_GRN, 290 LOW); 291 digitalWrite(S_OUT_RED, HIGH); 292 } 293 294// ---- RELAY 295 296 if (flag_change_junc) { 297 298 if (switch_A && !switch_B) { // Line A 299 300 digitalWrite(S_LA_RED, LOW); 301 digitalWrite(S_LB_RED, HIGH); // RED 302 303 digitalWrite(RELAY_LA, LOW); 304 digitalWrite(RELAY_LB, HIGH); 305 306 flag_blink_AB = false; 307 } 308 else if (!switch_A && switch_B) 309 { // Line B 310 digitalWrite(S_LA_RED, HIGH); // RED 311 digitalWrite(S_LB_RED, 312 LOW); 313 digitalWrite(RELAY_LA, HIGH); 314 digitalWrite(RELAY_LB, 315 LOW); 316 flag_blink_AB = false; 317 } 318 else { // Undefined 319 320 digitalWrite(RELAY_LA, HIGH); 321 digitalWrite(RELAY_LB, HIGH); 322 323 flag_blink_AB = true; 324 } 325 326 flag_change_junc = false; 327 328 } 329 330 if (millis() > (millisJunctions + 900)) digitalWrite(JUNCTION_EN, 331 LOW); 332 if (millis() > (timerStation + 15000)) flag_station_open = true; // Delay 333 15 sec. 334 335 if (flag_blink_AB) { 336 static bool tick_blink; 337 static 338 unsigned long blinkMillis; 339 if (millis() > (blinkMillis + 500)) { // 500 - 340 blinking optimal period 341 tick_blink = !tick_blink; 342 blinkMillis 343 = millis(); 344 } 345 digitalWrite(S_LA_RED, tick_blink); 346 digitalWrite(S_LB_RED, 347 !tick_blink); 348 } 349 350} 351 352// ----------- FUNCTIONS ----------- // 353 354void 355 receiveI2C(int howMany) { 356 while (Wire.available() > 0) { 357 dataFromI2C 358 = Wire.read(); 359 } 360} 361
Downloadable files
Track Plan
Track Plan
Track Plan
Track Plan
Circuit of Interlocking system
Circuit of Interlocking system
Comments
Only logged in users can leave comments
Steve_Massikker
0 Followers
•0 Projects
Table of contents
Intro
23
0