/* ABS Signal circuit using the Arduino microcontroller. This code implements a single section of bi-directional ABS signals using Arduino NANO board. It should work on the UNO, MEGA, Leonardo or Pro MINI boards as well. This code is in the public domain and may be shared under the provisions of the Creative Commons license type BY. For more information visit this site: https://creativecommons.org/licenses/by/4.0/ Version 1.5a by Dennis Drury Email cowrr1984@gmail.com Version history V1.0 Initial Release V1.1 Added approach lighting V1.2 Changed pin assignments to better match up with the Pro MINI board. V1.3 Changed pin assignments to better match up with the NANO board. V1.4 Fixed syntax error in the Advance Approach logic and comment error in the Approach Lighting logic. V1.5 Changed millis variables to unsigned long to better match up with the Arduino recommendations. V1.5a Changed input pins due to analog input only on A6 and 7 of the NANO. Sig_W 0-| ------------|------------|-------------|----------------|------------|--------- Blk_WD2 Blk_WD Blk_WH |-0 Blk_EH Blk_ED Blk_ED2 Sig_E The Flashing Yellow section includes modified code from the Arduino.cc website that is in the public domain http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay */ // There are six detected blocks int Blk_WD2 = A0; int Blk_WD = A1; int Blk_WH = A2; int Blk_EH = A3; int Blk_ED = A4; int Blk_ED2 = A5; // There are six signal outputs // Sig_Wr, Sig_Wy, Sig_Wg, Sig_Er, Sig_Ey and Sig_Eg const int Sig_Wr = 2; const int Sig_Wy = 3; const int Sig_Wg = 4; const int Sig_Er = 6; const int Sig_Ey = 7; const int Sig_Eg = 8; // There are four internal variables that will change. These are only used for the flashing yellow // aspects. int Sig_Wy_f = LOW; int Sig_Ey_f = LOW; unsigned long previousMillis1 = 0; unsigned long previousMillis2 = 0; // The following variables are the flash rates for the signals // Note these values may need to change depending on the size of your final code and the clock // speed of your processor. // The numbers are slightly different because in my experience no two prototype flashing relays // ever flashed at the exact same rate. unsigned long interval1 = 800; unsigned long interval2 = 810; // The setup routine runs once on bootup or pressing the reset button void setup() { // Set up the blocks as inputs pinMode(Blk_WD2, INPUT_PULLUP); pinMode(Blk_WD, INPUT_PULLUP); pinMode(Blk_WH, INPUT_PULLUP); pinMode(Blk_EH, INPUT_PULLUP); pinMode(Blk_ED, INPUT_PULLUP); pinMode(Blk_ED2, INPUT_PULLUP); // Set up the signals as outputs pinMode(Sig_Wr, OUTPUT); pinMode(Sig_Wy, OUTPUT); pinMode(Sig_Wg, OUTPUT); pinMode(Sig_Er, OUTPUT); pinMode(Sig_Ey, OUTPUT); pinMode(Sig_Eg, OUTPUT); } // This loop runs continously void loop() { // Read block occupancy Blk_WD2 = digitalRead(A0); Blk_WD = digitalRead(A1); Blk_WH = digitalRead(A2); Blk_EH = digitalRead(A3); Blk_ED = digitalRead(A4); Blk_ED2 = digitalRead(A5); // Set West Signal Green if (Blk_WH == HIGH && Blk_WD == HIGH && Blk_WD2 == HIGH) //If approach lighting add “&& Blk_EH == LOW” to if statement { digitalWrite(Sig_Wr, LOW); digitalWrite(Sig_Wy, LOW); digitalWrite(Sig_Wg, HIGH); } // Set West Signal Flashing Yellow if (Blk_WH == HIGH && Blk_WD == HIGH && Blk_WD2 == LOW) //If approach lighting add “&& Blk_EH == LOW” to if statement { digitalWrite(Sig_Wr, LOW); digitalWrite(Sig_Wg, LOW); unsigned long currentMillis1 = millis(); if(currentMillis1 - previousMillis1 > interval1) { // save the last time you blinked the LED previousMillis1 = currentMillis1; // if the LED is off turn it on and vice-versa: if (Sig_Wy_f == LOW) Sig_Wy_f = HIGH; else Sig_Wy_f = LOW; // set the LED with the state of the variable: digitalWrite(Sig_Wy, Sig_Wy_f); } } // Set West Signal Yellow if (Blk_WH == HIGH && Blk_WD == LOW) //If approach lighting add “&& Blk_EH == LOW” to if statement { digitalWrite(Sig_Wr, LOW); digitalWrite(Sig_Wy, HIGH); digitalWrite(Sig_Wg, LOW); } // Set West Signal Red if (Blk_WH == LOW) //If approach lighting add “&& Blk_EH == LOW” to if statement { digitalWrite(Sig_Wr, HIGH); digitalWrite(Sig_Wy, LOW); digitalWrite(Sig_Wg, LOW); } /* Uncomment this section to enable approach lighting // Set West Signal to Off if (Blk_EH == HIGH) { digitalWrite(Sig_Wr, LOW); digitalWrite(Sig_Wy, LOW); digitalWrite(Sig_Wg, LOW); } */ // Set East Signal Green if (Blk_EH == HIGH && Blk_ED == HIGH && Blk_ED2 == HIGH) //If approach lighting add “&& Blk_WH == LOW” to if statement { digitalWrite(Sig_Er, LOW); digitalWrite(Sig_Ey, LOW); digitalWrite(Sig_Eg, HIGH); } // Set East Signal Flashing Yellow if (Blk_EH == HIGH && Blk_ED == HIGH && Blk_ED2 == LOW) //If approach lighting add “&& Blk_WH == LOW” to if statement { digitalWrite(Sig_Er, LOW); digitalWrite(Sig_Eg, LOW); unsigned long currentMillis2 = millis(); if(currentMillis2 - previousMillis2 > interval2) { // save the last time you blinked the LED previousMillis2 = currentMillis2; // if the LED is off turn it on and vice-versa: if (Sig_Ey_f == LOW) Sig_Ey_f = HIGH; else Sig_Ey_f = LOW; // set the LED with the ledState of the variable: digitalWrite(Sig_Ey, Sig_Ey_f); } } // Set West Signal Yellow if (Blk_EH == HIGH && Blk_ED == LOW) //If approach lighting add “&& Blk_WH == LOW” to if statement { digitalWrite(Sig_Er, LOW); digitalWrite(Sig_Ey, HIGH); digitalWrite(Sig_Eg, LOW); } // Set West Siganl Red if (Blk_EH == LOW) //If approach lighting add “&& Blk_WH == LOW” to if statement { digitalWrite(Sig_Er, HIGH); digitalWrite(Sig_Ey, LOW); digitalWrite(Sig_Eg, LOW); } /* Uncomment this section to enable approach lighting // Set signal 2 to Off if (Blk_WH == HIGH) { digitalWrite(Sig_Er, LOW); digitalWrite(Sig_Ey, LOW); digitalWrite(Sig_Eg, LOW); } */ }