//Revision 5.1 better random and new indicator code
//Revision 5 Add flashing front lights using random()
//Revision 4.3 Add flashing front lights sequence 20x RANDOM INTERVALS
//Revision 4.2 Add anti click of relays on start up
//Revision 4.1 going on 4.25 - Dash lights fixed
// Bounce2 - Version: Latest 
#include <Arduino.h>
#include <Bounce2.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "OneButton.h" //for crazy stuff with two push buttons

//OUTPUTS $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
const int ris = 46; //Right Indicators
const int lis = 48; //Left Indicators 
const int FAN = 39; //Radiator FAN motor power
const int frd = 38; //Front Right Dipped Beam Head light
const int fld = 40; //Front Left Dipped Beam Head light
const int frm = 42; //Front Right Main Beam Head light
const int flm = 44; //Front Left Main Beam Head light
const int frs = 50; //Front Right Side light
const int fls = 52; //Front Left Side light
const int tls = 53; //Tail Lights

const int wip = 41; //Windscreen Wiper Output

const int wsh = 43;//Windscreen Washer Output

const int hrn = 45; //Horn Output

const int dil = 27; //LED in Dash for Indicator Left - Brown CAT5
const int dir = 26; //LED in Dash for Indicator Right - Blue CAT5
const int dmb = 25; //LED in Dash for Main Beam
const int dls = 47; //LEDs in Dash for illumination Changed to 12v lights running on Relay

//const int fanlamp = ??; // LED on dash to show Radiator FAN is running

//INPUTS  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
const int sl = 14; //button side lights      Pink
const int db = 15; //button dipped lights    Grey
const int mb = 16; //button main beam        Yellow
const int li = 17; //left indicators         Purple
const int ri = 18; //right indicators        Red
const int hz = 28; //Hazard warning lights   Blue & White CAT5
const int ws = 11; //Windscreen Washer Switch - Momentary? (should be 11)
const int wp = 10; //Windscreen Wiper toggle switch  Blue / poteniomiter click..
const int dl = 12; // dash light on and off (should be 12)
const int hn = 19; //Horn switch on shalk
const int wk = 7; //Windcsreen Wiper Park Limit switch
const int fn = 5; //Fan switch

#define ONE_WIRE_BUS 6  //For Termometer prob in radiator (Three lines) Input 6
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const int OneSec = 3000;
unsigned long TimeNow = 0;
unsigned long LastTempReading = 1000;
float Celcius=0;
float Fahrenheit=0;

//INPUT mementary buttons for silly effects
const int FlashLeftHL = 8;  //white
const int FlashRightHL = 9; //white

//VARIABLES $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
int riState;
int liState;
int hzState;
unsigned long indicatorPrevMillis = 1;        // will store last time LED was updated
const long indicatorInterval = 400;           // indicatorInterval at which to blink (milliseconds)
int riLatch = HIGH;
int liLatch = HIGH;
int hzLatch = HIGH;
unsigned long indicatorCurrMillis;
int riValLast = HIGH;
int liValLast = HIGH;
int hzValLast = HIGH;
unsigned long riValTime = 1;
unsigned long liValTime = 1;
unsigned long hzValTime = 1;

//____Windscreen wiper stuff 7 lines
int wkVal = LOW; //The state of the park limit switch effect at read.
int wpVal = HIGH; //wiper
int potValue;
int long DelayVAL = 1000;
int long potPin = A1;
int unsigned long WiperPreviousMillis = 1;
int RunOnce = 0;

// Instantiate a Bounce object :
Bounce debouncer = Bounce(); 

//___Washer
int wsVal;

//Dash Lamp states for indicators

int dilstate; //Dash Indicator lamp valve Left
int dirstate; //Dash Indicator lamp valve Right

//Horn

int hnVal; // Horn Button state

int fnVal = LOW; //Fan Switch set to LOW

unsigned long RandomNumber;
int FrontLights;
int RandomOnOff;
int unsigned long DelayTime;
int RandomVal = LOW;

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@    Void Setup    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void setup() {

  pinMode(sl, INPUT_PULLUP);
  pinMode(db, INPUT_PULLUP);
  pinMode(mb, INPUT_PULLUP);
  pinMode(FlashLeftHL, INPUT_PULLUP);
  pinMode(FlashRightHL, INPUT_PULLUP);
  pinMode(ri, INPUT_PULLUP);
  pinMode(li, INPUT_PULLUP);
  pinMode(hz, INPUT_PULLUP);
  pinMode(ws, INPUT_PULLUP);
  pinMode(wp, INPUT_PULLUP);
  pinMode(dl, INPUT_PULLUP); //dash lights input switch on
  pinMode(hn, INPUT_PULLUP); //Horn input switch stalk
  pinMode(wk, INPUT_PULLUP); //Wind screen wiper park limit switch input
  pinMode(fn, INPUT_PULLUP); //Fan switch

  pinMode(potPin, INPUT);   //A1
  
  debouncer.attach(7,INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncer.interval(25); // Use a debounce interval of 25 milliseconds
  debouncer.attach(8,INPUT_PULLUP); //Attach debouncer to FlashLeft
  debouncer.interval(25); // Use a debounce interval of 25 milliseconds
  debouncer.attach(9,INPUT_PULLUP); //Attach debouncer to FlashRight
  debouncer.interval(25); // Use a debounce interval of 25 milliseconds

  digitalWrite(fld, HIGH);
  pinMode(fld, OUTPUT);
  digitalWrite(frd, HIGH);
  pinMode(frd, OUTPUT);
  digitalWrite(frm, HIGH);
  pinMode(frm, OUTPUT);
  digitalWrite(flm, HIGH);
  pinMode(flm, OUTPUT);
  digitalWrite(frs, HIGH);
  pinMode(frs, OUTPUT);
  digitalWrite(fls, HIGH);
  pinMode(fls, OUTPUT);
  digitalWrite(tls, HIGH);
  pinMode(tls, OUTPUT);
  digitalWrite(ris, HIGH);
  pinMode(ris, OUTPUT);
  digitalWrite(lis, HIGH);
  pinMode(lis, OUTPUT);
  digitalWrite(wip, HIGH);
  pinMode(wip, OUTPUT);
  pinMode(dil, OUTPUT);
  pinMode(dir, OUTPUT);
  digitalWrite(dmb, LOW);
  pinMode(dmb, OUTPUT);
  digitalWrite(dls, HIGH);
  pinMode(dls, OUTPUT);
  digitalWrite(wsh, HIGH);
  pinMode(wsh, OUTPUT);
  digitalWrite(hrn, HIGH);
  pinMode(hrn, OUTPUT);
  digitalWrite(FAN, HIGH);
  pinMode(FAN, OUTPUT);

  randomSeed(analogRead(4));
  
  Serial.begin(9600);
  sensors.begin();
}

void loop() { //Loop START START START START START START START START START START START START START START START START START START
  
  int slVal = digitalRead(sl); //Side Lights switch
  int dbVal = digitalRead(db); //Dipped Beam Switch
  int mbVal = digitalRead(mb); //Main beam switch
  int FlashLeftHLVal = digitalRead(FlashLeftHL); //Funny Flashing button Left
  int FlashRightHLVal = digitalRead(FlashRightHL); //Funny Flashing button Right
  int riVal = digitalRead(ri); //Indicator switch RIGHT
  int liVal = digitalRead(li); //Indicator switch LEFT
  int hzVal = digitalRead(hz); //Hazard switch
  int dlval = digitalRead(dl); //dash lights value
  int hnVal = digitalRead(hn); //read if horn button is pressed
  int wkVal = digitalRead(wk); //windscreen wiper park limit switch value
  int wpVal = digitalRead(wp); //Windscreen wiper switch
  
  //Head Lights on ****************HEAD LIGHTS********************//Turns on all side lights, tail lights and dipped beam lights by setting db LOW.
  if (dbVal == LOW && slVal == LOW && mbVal == HIGH && FlashLeftHLVal == HIGH && FlashRightHLVal == HIGH) {
    digitalWrite(frs, LOW);
    digitalWrite(fls, LOW);
    digitalWrite(frd, LOW);
    digitalWrite(fld, LOW);
    digitalWrite(frm, HIGH);
    digitalWrite(flm, HIGH);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, LOW);
    RandomVal = LOW;
  } else if (dbVal == HIGH && slVal == LOW && mbVal == HIGH && FlashLeftHLVal == HIGH && FlashRightHLVal == HIGH) {//If lights switch is just for side lights in DB will still be high..
    digitalWrite(frs, LOW);
    digitalWrite(fls, LOW);
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, HIGH);
    digitalWrite(flm, HIGH);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, LOW); //Dash Main beam Inicator lamp (Oposite high low!!)
    RandomVal = LOW;
    Serial.println(" Side lights ON ");
  } else if (dbVal == LOW && slVal == LOW && mbVal == LOW && FlashLeftHLVal == HIGH && FlashRightHLVal == HIGH) {//Main dipped lights are on, so db and sl and both Low, and also MB has been made low then turn off dipped and put on high beam
    digitalWrite(frs, LOW);
    digitalWrite(fls, LOW);
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, LOW);
    digitalWrite(flm, LOW);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, HIGH); //Dash Main beam Inicator lamp (Oposite high low!!)
  } else if (dbVal == HIGH && slVal == HIGH && mbVal == LOW && FlashLeftHLVal == HIGH && FlashRightHLVal == HIGH) {//If I want to flash main beam using stalk flash.. mb will go LOW
    digitalWrite(frs, HIGH);
    digitalWrite(fls, HIGH);
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, LOW);
    digitalWrite(flm, LOW);  //*********************************************************END OF REGULAR LIGHTS*********************
    digitalWrite(tls, HIGH); //*********************************************************END OF REGULAR LIGHTS*********************
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!) ******END OF REGULAR LIGHTS*********************
  } else if (dbVal == HIGH && slVal == HIGH && mbVal == HIGH && FlashLeftHLVal == LOW && FlashRightHLVal == HIGH) {//*************
    digitalWrite(frs, HIGH);  //£££££££££££££££££££££    All Lights OFF   ££££££££££££££££££££££££££££££££££££££££££££££££££££££££
    digitalWrite(fls, HIGH);   //This is all lights OFF, flash LEFT head light
    digitalWrite(frd, HIGH);
    digitalWrite(fld, LOW);
    digitalWrite(frm, HIGH);
    digitalWrite(flm, LOW);
    digitalWrite(tls, HIGH);
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!)
  } else if (dbVal == HIGH && slVal == HIGH && mbVal == HIGH && FlashLeftHLVal == HIGH && FlashRightHLVal == LOW) {
    digitalWrite(frs, HIGH);
    digitalWrite(fls, HIGH);   //This is all lights OFF, flash RIGHT head light
    digitalWrite(frd, LOW);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, LOW);
    digitalWrite(flm, HIGH);
    digitalWrite(tls, HIGH);
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!)
  } else if (dbVal == HIGH && slVal == LOW && mbVal == HIGH && FlashLeftHLVal == LOW && FlashRightHLVal == HIGH) {//*************
    digitalWrite(frs, HIGH);  //£££££££££££££££££££££    Side Lights ON    ££££££££££££££££££££££££££££££££££££££££££££££££££££££££
    digitalWrite(fls, LOW);   //This is side lights ON flash LEFT head light
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, HIGH);
    digitalWrite(flm, LOW);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!)
  } else if (dbVal == HIGH && slVal == LOW && mbVal == HIGH && FlashLeftHLVal == HIGH && FlashRightHLVal == LOW) {
    digitalWrite(frs, LOW);
    digitalWrite(fls, HIGH);   //This is side lights ON flash RIGHT head light
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, LOW);
    digitalWrite(flm, HIGH);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!)
  } else if (dbVal == HIGH && slVal == LOW && mbVal == HIGH && FlashLeftHLVal == LOW && FlashRightHLVal == LOW) {
    digitalWrite(frs, LOW);
    digitalWrite(fls, LOW);   //This is side lights ON flash LEFT and RIGHT head light
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, LOW);
    digitalWrite(flm, LOW);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!)
  } else if (dbVal == LOW && slVal == LOW && mbVal == HIGH && FlashLeftHLVal == LOW && FlashRightHLVal == HIGH) {//*************
    digitalWrite(frs, HIGH);  //£££££££££££££££££££££    Dipped beam Lights ON    £££££££££££££££££££££££££££££££££££££££££££££££££
    digitalWrite(fls, LOW);   //This is Dipped beam Lights ON, flash LEFT head light
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, HIGH);
    digitalWrite(flm, LOW);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!)
  } else if (dbVal == LOW && slVal == LOW && mbVal == HIGH && FlashLeftHLVal == HIGH && FlashRightHLVal == LOW) {
    digitalWrite(frs, LOW);
    digitalWrite(fls, HIGH);   //This is Dipped beam Lights ON, flash RIGHT head light
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, LOW);
    digitalWrite(flm, HIGH);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!)
  } else if (dbVal == LOW && slVal == LOW && mbVal == HIGH && FlashLeftHLVal == LOW && FlashRightHLVal == LOW) {
    digitalWrite(frs, HIGH);
    digitalWrite(fls, HIGH);   //This is Dipped beam Lights ON, flash RIGHT and LEFT head light
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, HIGH);
    digitalWrite(flm, HIGH);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, LOW); //Dash Main beam indicator lamp (Oposite high low!!)
  } else if (dbVal == LOW && slVal == LOW && mbVal == LOW && FlashLeftHLVal == LOW && FlashRightHLVal == HIGH) {//*************
    digitalWrite(frs, HIGH);  //£££££££££££££££££££££    Main beam Lights ON    £££££££££££££££££££££££££££££££££££££££££££££££££
    digitalWrite(fls, LOW);   //This is Main beam Lights ON, flash LEFT head light
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, HIGH);
    digitalWrite(flm, LOW);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!)
  } else if (dbVal == LOW && slVal == LOW && mbVal == LOW && FlashLeftHLVal == HIGH && FlashRightHLVal == LOW) {
    digitalWrite(frs, LOW);
    digitalWrite(fls, HIGH);   //This is Main beam Lights ON, flash RIGHT head light
    digitalWrite(frd, HIGH);
    digitalWrite(fld, HIGH);
    digitalWrite(frm, LOW);
    digitalWrite(flm, HIGH);
    digitalWrite(tls, LOW);
    digitalWrite(dmb, HIGH); //Dash Main beam indicator lamp (Oposite high low!!)
    //**************Sets the sadft lights going below***************************************************************
  } else if (dbVal == HIGH && slVal == HIGH && mbVal == HIGH && FlashLeftHLVal == LOW && FlashRightHLVal == LOW) {
    
//RandomVal = HIGH;
    

  } else if (dbVal == HIGH && slVal == HIGH && mbVal == HIGH && FlashLeftHLVal == HIGH && FlashRightHLVal == HIGH && RandomVal == LOW) {
   //********************************************Turn all to OFF condition********************************************
    digitalWrite(frs, HIGH); //****************************Turn all to OFF condition********************************************
    digitalWrite(fls, HIGH); //****************************Turn all to OFF condition********************************************
    digitalWrite(frd, HIGH); //****************************Turn all to OFF condition********************************************
    digitalWrite(fld, HIGH); //*************************************************************************************************
    digitalWrite(frm, HIGH); //*************************************************************************************************
    digitalWrite(flm, HIGH); //*************************************************************************************************
    digitalWrite(tls, HIGH); //*************************************************************************************************
    digitalWrite(dmb, LOW);  //Dash Main beam indicator lamp (Oposite high low!!)
  }
/*
if(RandomVal == HIGH){
RandomOnOff = random(2);
    if(millis() > DelayTime) {
    RandomNumber = random(9);
 Serial.println(RandomNumber);
    }
 if(RandomNumber == 0) {
  digitalWrite(frs, RandomOnOff);
  DelayTime = millis() + random(80,250);
  Serial.println(" FRS lights turned on   ");
  }
  if(RandomNumber == 1) {
    digitalWrite(fls, RandomOnOff);
    DelayTime = millis() + random(80,250);
  }
  if(RandomNumber == 2) {  
    digitalWrite(frd, RandomOnOff);
    digitalWrite(dmb, RandomOnOff);
    DelayTime = millis() + random(130,400);
     }
  if(RandomNumber == 3) { 
    digitalWrite(fld, RandomOnOff);
    digitalWrite(dmb, RandomOnOff);
    DelayTime = millis() + random(130,400);
      }
  if(RandomNumber == 4) {
    digitalWrite(ris, RandomOnOff);
    digitalWrite(dir, RandomOnOff);
    DelayTime = millis() + random(80,250);
  }
   if(RandomNumber == 5) {
    digitalWrite(lis, RandomOnOff);
    digitalWrite(dil, RandomOnOff);
    DelayTime = millis() + random(80,250);
  }
  if(RandomNumber == 6) {
    digitalWrite(frs, RandomOnOff);
    digitalWrite(frd, RandomOnOff);
    digitalWrite(dmb, RandomOnOff);
    digitalWrite(ris, RandomOnOff);
    digitalWrite(dir, RandomOnOff);
    DelayTime = millis() + random(120,250);
    }
    if(RandomNumber == 7) {
    digitalWrite(fls, RandomOnOff);
    digitalWrite(fld, RandomOnOff);
    digitalWrite(dmb, RandomOnOff);
    digitalWrite(lis, RandomOnOff);
    digitalWrite(dil, RandomOnOff);
    DelayTime = millis() + random(120,250);
    }
    if(RandomNumber == 8) {
    digitalWrite(fls, RandomOnOff);
    digitalWrite(fld, RandomOnOff);
    digitalWrite(dmb, RandomOnOff);
    digitalWrite(lis, RandomOnOff);
    digitalWrite(dil, RandomOnOff);
    digitalWrite(frs, RandomOnOff);
    digitalWrite(frd, RandomOnOff);
    digitalWrite(ris, RandomOnOff);
    digitalWrite(dir, RandomOnOff);
    DelayTime = millis() + random(120,250);
    }
} else {
  RandomVal = LOW;
}*/
    
  //*****************Dash illumination LEDs***************************************

  if (dlval == LOW && (dbVal == LOW || slVal == LOW || mbVal == LOW || FlashLeftHLVal == LOW || FlashRightHLVal == LOW)) {
    digitalWrite(dls, LOW);
  }
  else
  {
    digitalWrite(dls, HIGH);
  }

  //*****************end of dash illumination LEDs*********END*******************************

  //**START************Indicators Script************************************************************

  //****RIGHT INDICATOR

if (riVal != riValLast && riVal == LOW  && hzVal == HIGH && liLatch == HIGH) { //check to see if state of switch changed.
  riValTime = millis();
  Serial.println("  first if  ");
  riValLast = riVal;
}
  if(riValTime + 20 > millis() && millis() < riValTime + 25 && riVal == LOW){ // debounce switch
  riLatch = LOW;
  indicatorCurrMillis = millis();
    Serial.println("  riVal latched  ");
}
if(riLatch == LOW) {
if (indicatorCurrMillis + indicatorInterval >= millis()) {
riState = LOW; //Turning on the Relay for the actual light
dirstate = HIGH; //Turning on the LED on the dash
Serial.println("  Turned ON RIGHT  ");
   digitalWrite(ris, riState);
    digitalWrite(dir, dirstate);
} else if (indicatorCurrMillis + indicatorInterval + indicatorInterval >= millis() && indicatorCurrMillis + indicatorInterval < millis()) {
riState = HIGH; //Turning off the Relay for the actual light
dirstate = LOW; //Turning off the LED on the dash
   digitalWrite(ris, riState);
    digitalWrite(dir, dirstate);
} else if (indicatorCurrMillis + indicatorInterval + indicatorInterval < millis() && riVal == LOW) {
indicatorCurrMillis = millis();
} else {
riLatch = HIGH;
riValLast = HIGH;
}
}

//****LEFT INDICATOR

if (liVal != liValLast && liVal == LOW && hzVal == HIGH && riLatch == HIGH) { //check to see if state of switch changed.
  liValTime = millis();
  Serial.println("  first if  ");
  liValLast = liVal;
}
  if(liValTime + 20 > millis() && millis() < liValTime + 25 && liVal == LOW){ // debounce switch
  liLatch = LOW;
  indicatorCurrMillis = millis();
  Serial.println("  riVal latched  ");
}
if(liLatch == LOW) {
  if (indicatorCurrMillis + indicatorInterval >= millis()) {
    liState = LOW; //Turning on the Relay for the actual light
    dilstate = HIGH; //Turning on the LED on the dash
    Serial.println("  Turned ON LEFT  ");
    digitalWrite(lis, liState);
    digitalWrite(dil, dilstate);
  } else if (indicatorCurrMillis + indicatorInterval + indicatorInterval >= millis() && indicatorCurrMillis + indicatorInterval < millis()) {
    liState = HIGH; //Turning off the Relay for the actual light
    dilstate = LOW; //Turning off the LED on the dash
    Serial.println("  Turned OFF LEFT  ");
    digitalWrite(lis, liState);
    digitalWrite(dil, dilstate);
  } else if (indicatorCurrMillis + indicatorInterval + indicatorInterval < millis() && liVal == LOW) {
    indicatorCurrMillis = millis();
  } else {
    liLatch = HIGH;
    liValLast = HIGH;
  }
}
//****Hazard Waring Lights:

if (hzVal != hzValLast && hzVal == LOW && liLatch == HIGH && riLatch == HIGH) { //check to see if state of switch changed.
  hzValTime = millis();
  Serial.println("  first if  ");
  hzValLast = hzVal;
}
  if(hzValTime + 20 > millis() && millis() < hzValTime + 25 && hzVal == LOW){ // debounce switch
  hzLatch = LOW;
  indicatorCurrMillis = millis();
  Serial.println("  riVal latched  ");
}

if(hzLatch == LOW && riLatch == HIGH && liLatch == HIGH) {
  if (indicatorCurrMillis + indicatorInterval >= millis()) {
    liState = LOW; //Turning on the Relay for the actual light
    dilstate = HIGH; //Turning on the LED on the dash
    riState = LOW; //Turning on the Relay for the actual light
    dirstate = HIGH; //Turning on the LED on the dash
    Serial.println("  Turned Hazards On  ");
    digitalWrite(ris, riState);
    digitalWrite(dir, dirstate);
    digitalWrite(lis, liState);
    digitalWrite(dil, dilstate);
  } else if (indicatorCurrMillis + indicatorInterval + indicatorInterval >= millis() && indicatorCurrMillis + indicatorInterval < millis()) {
    liState = HIGH; //Turning off the Relay for the actual light
    dilstate = LOW; //Turning off the LED on the dash
    riState = HIGH; //Turning off the Relay for the actual light
    dirstate = LOW; //Turning off the LED on the dash
    Serial.println("  Turned OFF Hazards  ");
    digitalWrite(lis, liState);
    digitalWrite(dil, dilstate);
    digitalWrite(ris, riState);
    digitalWrite(dir, dirstate);
  } else if (indicatorCurrMillis + indicatorInterval + indicatorInterval < millis() && liVal == LOW) {
    indicatorCurrMillis = millis();
  } else {
    hzLatch = HIGH;
    hzValLast = HIGH;
  }
}

//****end of hazard warning lights***

  //***************END of Indicators Script*********************************************************

  //*******Windscreen Wipers script***************************************************************
  //Potentioniter wiring:
  // Red = 5v
  // Green = A1 - wiper - signal
  // Yellow = wp = 10; //Windscreen Wiper toggle switch  Blue / poteniomiter click..
  // Black = 0v

  wpVal = digitalRead(wp); //Read all of the relvent inputs for wipers
  wkVal = digitalRead(wk); //Read the wiper park limit switch
  potValue = analogRead(potPin); //read the pot value for the delay
  DelayVAL = map(potValue, 0, 1023, 20000, -11000); //map pot value to be low then high as turned
  debouncer.update(); // Update the Bounce instance

  if (wpVal == LOW) {
    if (potValue > 600) { //if low delay value then just run constantly
      digitalWrite(wip, LOW);

    } else { //if delay val greater than WP Val do intermittant
    if (RunOnce == 0){
      digitalWrite(wip, LOW);
      RunOnce = 1;
    }
       
   if (debouncer.fell()) {  // Call code if button transitions from HIGH to LOW
       digitalWrite(wip, HIGH); //stops the wiper motor
       WiperPreviousMillis = millis(); //sets the time at stop
   } 
  if (wkVal == LOW && (WiperPreviousMillis + DelayVAL) < millis()) {
    digitalWrite(wip, LOW); //once the delay is past then run again
  }
      // end of delay wiping
    }
  } else {
    if (debouncer.fell() || wkVal == LOW) {  // Stop when reached park not while parked or anything else.
      digitalWrite(wip, HIGH);  //stops the wiper motor
      RunOnce = 0; //Reset then run once
    }
  }
  //*******END of Windscreen Wipers script********************************************************

  //**********Windscreen Washer********************************************************************
  wsVal = digitalRead(ws);
  if (wsVal == LOW) {
    digitalWrite(wsh, LOW);
  } else {
    digitalWrite(wsh, HIGH);
  }
  //**********End of Windscreen Washer**************************************************************

  //********** Horn ********************************************************************
  if (hnVal == LOW) {
    digitalWrite(hrn, LOW);
  } else {
    digitalWrite(hrn, HIGH);
  }
  //********** End of Horn **************************************************************
  
  //*************Start of Radiator Temp and FAN control**********************************************
  
  fnVal = digitalRead(fn); //Switch value to turn fan on.
  TimeNow = millis();
  if((LastTempReading + OneSec) < TimeNow) {
  sensors.requestTemperatures(); 
  Celcius=sensors.getTempCByIndex(0);
  Fahrenheit=sensors.toFahrenheit(Celcius);
  Serial.print(Celcius);
  Serial.println(" C");
  LastTempReading = millis();
  }
  if(fnVal == HIGH){
  if(Celcius >= 85) { //turn on fan only at the this temperature (ºC)
  digitalWrite(FAN, LOW);
  //digitalWrite(fanlamp, LOW);
  }
  if(Celcius <= 75) { //turn fan off again once temp (ºC) has dropped below this
  digitalWrite(FAN, HIGH);
  //digitalWrite(fanlamp, HIGH);
  }
  } else {
  digitalWrite(FAN, LOW); //Hard trun on off the fan regarless of the sensor
  //digitalWrite(fanlamp, LOW);
  }
  
  //*************END of Radiator Temp and FAN control**********************************************
  
  

  //--------------------------------The VERY END--------------------------------
}