/*

Arduino Nano - old bootloader
Robert Parnell.
28/6/23
Wascoe Siding - tunnel signal controller
(designed to be mounted on signal module PCB)
#############################

V1 - create basic structure


  
  ###########################


    board outputs:

T1 red
T1 yellow
T1 green
T2 red
T2 yellow
T3 green
track circuit mimic
HB   heartbeat - indicates that unit is operating


    board inputs:

signalafterT1
signalafterT2
trackcct
T1 stop
T1 proceed
T2 stop
T2 proceed
emergency stop


1x spare input


*/

//outputs:
int L5 = 2;// D2 etc
int L6 = 3; 
int L7 = 4; 
int L9 = 5;
int L10 = 7; 
int L11 = 8;
int HB = 9; // heartbeat indicator
int L12 = 10; 
int RevAlert = 11; 
//int S2grn  = 12; 

//inputs:
int In5 = A0;
int In6 = A1;
int In7 = A2;
int In9 = A3; 
int In10 = A4; 
int In11 = A6;
int In12 = A7;

// input values:
int In5Val = 1000;
int In6Val = 1000;
int In7Val = 1000;
int In9Val = 1000;
int In10Val = 1000;
int In11Val = 1000;
int In12Val = 1000;



int testtime = 1000; // milliseconds for test lamp sequence


int L5normal = true; //
int L6normal = true; //
int L7normal = true; //
int L9normal = true; //
int L10normal = true; //
int L11normal = true; //
int L12normal = true; //

int PointsRev = false; // are any points reversed?
int dispREV = true; // display reversed points mimic (timer on/off)


unsigned long startMillisH; // for heartbeat timer
unsigned long startMillisA; // for alert timer
unsigned long alerttimer = 400; // time period for indicator to be on
//unsigned long offtimer = 600; // time perion for indicator to be off
unsigned long AcurrentMillis; // what will be the current value to compare countdown to
unsigned long HcurrentMillis;
unsigned long HBtimer = 500; // heartbeat timer



void setup() {
  
  pinMode(L5, OUTPUT);
  pinMode(L6, OUTPUT);
  pinMode(L7, OUTPUT);
  pinMode(L9, OUTPUT);
  pinMode(L10, OUTPUT);
  pinMode(L11, OUTPUT);
  pinMode(L12, OUTPUT);
  pinMode(RevAlert, OUTPUT);
 
  pinMode(HB, OUTPUT);
  
  pinMode(In5, INPUT);
  pinMode(In6, INPUT);
  pinMode(In7, INPUT);
  pinMode(In9, INPUT);
  pinMode(In10, INPUT);
  pinMode(In11, INPUT);
  pinMode(In12, INPUT);

  startMillisH = millis(); //start timing code for heartbeat
  startMillisA = millis(); //start timing code for alert
  
  startuptest();
  
 // Serial.begin(9600); // Any baud rate should work
//  Serial.println("Hello Arduino\n");
    
}

void loop() {  
  // read input values
  // display appropriate signal
  // go back and do it again!
  // flash heartbeat LED along the way
  AcurrentMillis = millis();
  if (AcurrentMillis - startMillisA >= alerttimer) {
    dispREV = true;
  }
  else {
    dispREV = false;
    startMillisA = AcurrentMillis;
  }
  
  HcurrentMillis = millis();
  if (HcurrentMillis - startMillisH >= HBtimer){
    digitalWrite(HB,LOW); // on
  }
  else {
    startMillisH = HcurrentMillis;
    digitalWrite(HB,HIGH); // on
  }
  
  
  readinputvalues();
  displaymimic();
 
  
}

void readinputvalues(){
  
 // Serial.println("reading inputs");
     In5Val = analogRead(In5); // 
     In6Val = analogRead(In6); //
     In7Val = analogRead(In7); //
     In9Val = analogRead(In9); //
     In10Val = analogRead(In10); //
     In11Val = analogRead(In11); //
     In12Val = analogRead(In12); //
     
     if (In5Val <=500) {
       L5normal = true; 
       PointsRev = false;//
       }
     else {
        L5normal = false;
        PointsRev = true;
     }
     
     if (In6Val <=500) {
       L6normal = true; //
       PointsRev = false;//
       }
     else {
        L6normal = false;
        PointsRev = true;
     }
     
     if (In7Val <=500) {
       L7normal = true; //
       PointsRev = false;//
       }
     else {
        L7normal = false;
        PointsRev = true;
     }
     
    
     if (In9Val <=500) {
       L9normal = true; //
       PointsRev = false;//
       }
     else {
        L9normal = false;
        PointsRev = true;
     }
     
     if (In10Val <=500) {
       L10normal = true; //
       PointsRev = false;//
       }
     else {
        L10normal = false;
        PointsRev = true;
     }
     
     if (In11Val <=500) {
       L11normal = true; //
       PointsRev = false;//
       }
     else {
        L11normal = false;
        PointsRev = true;
     }
     
     if (In12Val <=500) {
       L12normal = true; //
       PointsRev = false;//
       }
     else {
        L12normal = false;
        PointsRev = true;
     }
     
    
}

void displaymimic(){
 
  if (L5normal == false)  digitalWrite(L5,HIGH); // reversed
   else digitalWrite(L5,LOW); // normal
   
   if (L6normal == false)  digitalWrite(L6,HIGH); // reversed
   else digitalWrite(L6,LOW); // normal
   
   if (L7normal == false)  digitalWrite(L7,HIGH); // reversed
   else digitalWrite(L7,LOW); // normal
   
   if (L9normal == false)  digitalWrite(L9,HIGH); // reversed
   else digitalWrite(L9,LOW); // normal
   
   if (L10normal == false)  digitalWrite(L10,HIGH); // reversed
   else digitalWrite(L10,LOW); // normal
   
   if (L11normal == false)  digitalWrite(L11,HIGH); // reversed
   else digitalWrite(L11,LOW); // normal
   
   if (L12normal == false)  digitalWrite(L12,HIGH); // reversed
   else digitalWrite(L12,LOW); // normal
   
    
 
  
  if (dispREV == true && PointsRev == true) digitalWrite(RevAlert, LOW);
  else   digitalWrite(RevAlert, HIGH);
  
  
 
    
          
        
    
  
  
}

void startuptest(){
  // cycle through and test all outputs for a short time
  
  digitalWrite(L5,HIGH); // on
  delay (testtime);
  digitalWrite(L5,LOW); // off
  
  digitalWrite(L6,HIGH); // on
  delay (testtime);
  digitalWrite(L6,LOW); // off
  
  digitalWrite(L7,HIGH); // on
  delay (testtime);
  digitalWrite(L7,LOW); // off
  
  digitalWrite(L9,HIGH); // on
  delay (testtime);
  digitalWrite(L9,LOW); // off
  
  digitalWrite(L10,HIGH); // on
  delay (testtime);
  digitalWrite(L10,LOW); // off
  
  digitalWrite(L11,HIGH); // on
  delay (testtime);
  digitalWrite(L11,LOW); // off
  
  digitalWrite(L12,HIGH); // on
  delay (testtime);
  digitalWrite(L12,LOW); // off
  
  
}