Arduino Code
// Copyright (C) 2014 Paul T Middlehurst.
// Permission is granted to copy, distribute and/or modify this document
// under the terms of the GNU Free Documentation License, Version 1.3
// or any later version published by the Free Software Foundation;
// with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
// A copy of the license is included in the section entitled “GNU Free Documentation License”.
//
// Code for colour light signals, WDMES wrtitten bt DAWebster, based on swing link signal code by PTMiddlehurst
// 1st release, 08 dec 2015 functional but doesn’t cater for drooping battery voltage
// modified 18 Jan 2016 to keep green constant brightness (on mark space)
// Modified 9 feb to cater for altered pin assignments and mark space on red optimised for fusion online LEDs
bool homeOccupied;
bool distOccupied;
bool homeError;
bool distError;
bool bothError;
bool batFlat = false;
int homeCheck;
int Vbat;
int i;
// name the pins
const int BatIn = 0; //actually A0
const int HomeSection = 1; //actually A1
const int DistSection = 2; //actually A2
const int Red = 6;
const int Yellow = 5;
const int Green = 3;
const int OutToDist = 8;
void setup() {
pinMode(Red, OUTPUT);
pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(OutToDist, OUTPUT);
// pinMode(BatOut, OUTPUT);
digitalWrite(Red, LOW);
digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
digitalWrite(OutToDist, HIGH);
// digitalWrite(BatOut, LOW);
lamp_test();
}
void lamp_test() {
for(i = 0; i < 4; i++)
{digitalWrite(Red, !digitalRead(Red));
delay(200);
}
for(i = 0; i < 4; i++)
{digitalWrite(Yellow, !digitalRead(Yellow));
delay(200);
}
for(i = 0; i < 4; i++)
{digitalWrite(Green, !digitalRead(Green));
delay(200);
}
// for(i = 0; i < 4; i++)
// {digitalWrite(BatOut, !digitalRead(BatOut));
// delay(200);
// }
}
void statusCheck() {
if(analogRead(HomeSection)>682)
homeError =true;
else
homeError = false;
if(analogRead(DistSection)>682)
distError =true;
else
distError = false;
if(homeError && distError)
bothError = true;
else
bothError = false;
if(analogRead(HomeSection)<341)
homeOccupied = true;
else
homeOccupied = false;
if(analogRead(DistSection)<341)
distOccupied = true;
else
distOccupied = false;
Vbat = analogRead(BatIn);
if(Vbat < 355)
batFlat = true;
else
batFlat = false;
}
void HomeErrorFlash() {
digitalWrite(OutToDist, HIGH);
while (homeError && !bothError) {
digitalWrite (Red, HIGH);
delay(500);
digitalWrite (Red, LOW);
delay(500);
statusCheck();
}
digitalWrite(OutToDist, LOW);
}
void DistErrorFlash() {
digitalWrite(OutToDist, HIGH);
while (distError && !bothError) {
digitalWrite (Yellow, HIGH);
delay(500);
digitalWrite (Yellow, LOW);
delay(500);
statusCheck();
}
digitalWrite(OutToDist, LOW);
}
void BothErrorFlash() {
digitalWrite(OutToDist, HIGH);
while (bothError) {
digitalWrite(OutToDist, HIGH);
digitalWrite (Red, HIGH);
digitalWrite (Yellow, HIGH);
delay(500);
digitalWrite (Red, LOW);
digitalWrite (Yellow, LOW);
delay(500);
statusCheck();
}
digitalWrite(OutToDist, LOW);
}
void Home() {
digitalWrite(OutToDist, HIGH);
analogWrite(Red, 17720/(Vbat-214));
homeCheck = 300;
while (homeCheck > 1)
{
statusCheck();
delay(10);
homeCheck –;
if(homeOccupied)
homeCheck = 300;
}
digitalWrite(OutToDist, LOW);
analogWrite(Red, 0);
}
void Dist() {
digitalWrite(Yellow, HIGH);
while (distOccupied && !distError && !bothError && !homeError && !homeOccupied)
statusCheck();
digitalWrite(Yellow, LOW);
}
void Clear() {
if(!batFlat)
{analogWrite(Green, 17720/(Vbat-214));
while (!homeError && !distError && !bothError && !homeOccupied && !distOccupied)
statusCheck();
analogWrite(Green, 0);
}
else
{while (!homeError && !distError && !bothError && !homeOccupied && !distOccupied)
{analogWrite(Green, 147);
delay(500);
analogWrite(Green, 0);
delay(500);
statusCheck();
}
}
}
// pecking order for lights
//home occupied
//both section faults
//home section fault
//dist section fault
//dist occupied
// if none of above green
void loop() {
statusCheck();
// if(batFlat)
// digitalWrite(BatOut,HIGH);
if(homeOccupied)
Home();
else if (bothError)
BothErrorFlash();
else if(homeError)
HomeErrorFlash();
else if(distError)
DistErrorFlash();
else if(distOccupied)
Dist();
else Clear();
}