/*

Arduino Nano - old bootloader
Robert Parnell.
27/6/23
Wascoe Siding - points lever indicator
(designed to be mounted on signal module PCB)
#############################

V1 - create basic structure


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


    board outputs:

L5 - lever 5
L6 - lever 6
L7 - lever 7
L9 - lever 9
L10 - lever 10
L11 - lever 11
L12 - lever 12
RevAlert - pulsating light to remind singallers of a reversed lever

HB   heartbeat - indicates that unit is operating

REVpulse - power for pulse effect for when indicators are reversed



    board inputs:

In5
In6
In7
In9
In10
In11
In12

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;

boolean toggle = false;
boolean alerttoggle = false;

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 currentMillis; // for heartbeat timer

unsigned long HBstartMillis; // for heartbeat timer
unsigned long HBcurrentMillis; // what will be the current value to compare countdown to
unsigned long HcurrentMillis;
unsigned long HBtimer = 500; // heartbeat timer

unsigned long AlertstartMillis; // for alert timer
unsigned long alerttimer = 400; // time period for indicator to be on/off 50% duty cycle


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);

  HBstartMillis = millis(); //start timing code for heartbeat
  AlertstartMillis = 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
  currentMillis = millis();
  
  if (currentMillis - AlertstartMillis >= alerttimer) {
    alerttoggle = !alerttoggle;
    AlertstartMillis=currentMillis;
    
    //dispREV = true;
  }
  
  
  
  if (currentMillis - HBstartMillis >= HBtimer){
    toggle = !toggle;
    digitalWrite(HB,toggle); // on
    HBstartMillis=currentMillis;
  }
  
  
  
  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; 
       
       }
     else {
        L5normal = false;
             }
     
     if (In6Val <=500) {
       L6normal = true; //
              }
     else {
        L6normal = false;
             }
     
     if (In7Val <=500) {
       L7normal = true; //
              }
     else {
        L7normal = false;
             }
     
    
     if (In9Val <=500) {
       L9normal = true; //
              }
     else {
        L9normal = false;
             }
     
     if (In10Val <=500) {
       L10normal = true; //
              }
     else {
        L10normal = false;
             }
     
     if (In11Val <=500) {
       L11normal = true; //
              }
     else {
        L11normal = false;
             }
     
     if (In12Val <=500) {
       L12normal = true; //
              }
     else {
        L12normal = false;
             }
     
    if (L5normal==false||L6normal==false||L7normal==false||L9normal==false||L10normal==false||L11normal==false||L12normal==false)PointsRev=true;
    else PointsRev=false;
}

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 (alerttoggle == false && PointsRev == true) {
    digitalWrite(RevAlert, HIGH);
  }
  else   digitalWrite(RevAlert, LOW);
  
  if (PointsRev == false) digitalWrite(RevAlert,LOW);
  
  
 
 
  
}

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
  
  
}