Hi Everyone,
Here is the code to implement a fully functional ABS signal system using the Arduino Uno or ProMini. The code is open source to please feel free to copy and modify it as you choose. Please let me know if there are any questions.
Cheers, Dennis Drury
Reno, NV
/*
ABS Signal circuit using the Arduino microcontroller.
This code implements a single section of bi-directional ABS signals using either the Uno, Leonardo, or ProMini boards.
Version 1.1 by Dennis Drury
Version history
V1.0 Initial Release
V1.1 Added approach lighting
This code is in the public domain and may be freely copied or modified.
Sig_1
0-
|
------
|
Blk_1 Blk_2 Blk_3
|
Sig_2
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
// Blk_1 to Blk_6
int Blk_1 = A5;
int Blk_2 = A4;
int Blk_3 = A3;
int Blk_4 = A2;
int Blk_5 = A1;
int Blk_6 = A0;
// There are six signal outputs
// Sig_1r, Sig_1y, Sig_1g, Sig_2r, Sig_2y and Sig_2g
const int Sig_1r = 6;
const int Sig_1y = 7;
const int Sig_1g = 8;
const int Sig_2r = 9;
const int Sig_2y = 10;
const int Sig_2g = 11;
// There are four internal variables that will change. These are only used for the flashing yellow aspects.
int Sig_1y_f = LOW;
int Sig_2y_f = LOW;
long previousMillis1 = 0;
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.
long interval1 = 800;
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_1, INPUT);
pinMode(Blk_2, INPUT);
pinMode(Blk_3, INPUT);
pinMode(Blk_4, INPUT);
pinMode(Blk_5, INPUT);
pinMode(Blk_6, INPUT);
pinMode(Blk_6, INPUT);
// Set up the signals as outputs
pinMode(Sig_1r, OUTPUT);
pinMode(Sig_1y, OUTPUT);
pinMode(Sig_1g, OUTPUT);
pinMode(Sig_2r, OUTPUT);
pinMode(Sig_2y, OUTPUT);
pinMode(Sig_2g, OUTPUT);
}
// This loop runs continously
void loop()
{
// Read block occupancy
Blk_1 = digitalRead(A5);
Blk_2 = digitalRead(A4);
Blk_3 = digitalRead(A3);
Blk_4 = digitalRead(A2);
Blk_5 = digitalRead(A1);
Blk_6 = digitalRead(A0);
// Set signal 1 Green
if (Blk_3 == HIGH && Blk_2 == HIGH && Blk_1 == HIGH && Blk_4 == LOW)
{
digitalWrite(Sig_1r, HIGH); digitalWrite(Sig_1y, HIGH); digitalWrite(Sig_1g, LOW);
}
// Set signal 1 Flashing Yellow
if (Blk_3 == HIGH && Blk_2 == HIGH && Blk_1 == LOW && Blk_4 == LOW)
{
digitalWrite(Sig_1r, HIGH); digitalWrite(Sig_1g, HIGH);
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_1y_f == LOW)
Sig_1y_f = HIGH;
else
Sig_1y_f = LOW;
// set the LED with the state of the variable:
digitalWrite(Sig_1y, Sig_1y_f);
}
}
// Set signal 1 Yellow
if (Blk_3 == HIGH && Blk_2 == LOW && Blk_4 == LOW)
{
digitalWrite(Sig_1r, HIGH); digitalWrite(Sig_1y, LOW); digitalWrite(Sig_1g, HIGH);
}
// Set signal 1 Red
if (Blk_3 == LOW && Blk_4 == LOW)
{
digitalWrite(Sig_1r, LOW); digitalWrite(Sig_1y, HIGH); digitalWrite(Sig_1g, HIGH);
}
// Set signal 1 to Off
if (Blk_4 == HIGH)
{
digitalWrite(Sig_1r, HIGH); digitalWrite(Sig_1y, HIGH); digitalWrite(Sig_1g, HIGH);
}
// Set signal 2 Green
if (Blk_4 == HIGH && Blk_5 == HIGH && Blk_6 == HIGH && Blk_3 == LOW)
{
digitalWrite(Sig_2r, HIGH); digitalWrite(Sig_2y, HIGH); digitalWrite(Sig_2g, LOW);
}
// Set signal 2 Flashing Yellow
if (Blk_4 == HIGH && Blk_5 == HIGH && Blk_6 == LOW && Blk_3 == LOW)
{
digitalWrite(Sig_2r, HIGH); digitalWrite(Sig_2g, HIGH);
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_2y_f == LOW)
Sig_2y_f = HIGH;
else
Sig_2y_f = LOW;
// set the LED with the ledState of the variable:
digitalWrite(Sig_2y, Sig_2y_f);
}
}
// Set signal 2 Yellow
if (Blk_4 == HIGH && Blk_5 == LOW && Blk_3 == LOW)
{
digitalWrite(Sig_2r, HIGH); digitalWrite(Sig_2y, LOW); digitalWrite(Sig_2g, HIGH);
}
// Set signal 2 Red
if (Blk_4 == LOW && Blk_3 == LOW)
{
digitalWrite(Sig_2r, LOW); digitalWrite(Sig_2y, HIGH); digitalWrite(Sig_2g, HIGH);
}
// Set signal 2 to Off
if (Blk_3 == HIGH)
{
digitalWrite(Sig_2r, HIGH); digitalWrite(Sig_2y, HIGH); digitalWrite(Sig_2g, HIGH);
}
}