//Revision 6.1 Silly lights, using switch cases for lights, and wipers new code
// 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 long potValue;
int long DelayVAL = 1000;
int long potPin = A1;
int unsigned long WiperPreviousMillis = 1;
int RunOnce = 0;
int wipers;
int WipeMode = 4;

// Instantiate a Bounce object :
Bounce WKdebouncer = 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

int unsigned long serialPrintOutTime = 1;
int long serialPrintDelay = 2000;

int lights = 62;
int mainLeftOut = 0;
int mainRightOut = 0;

int RandomVal = LOW;
int RandomOnOff;
int unsigned long DelayTime = 2;
int RandomNumber;
int OldRandomVal = 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

  WKdebouncer.attach(7, INPUT_PULLUP); // Attach the WKdebouncer to a pin with INPUT_PULLUP mode
  WKdebouncer.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);

  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

  if (millis() >= serialPrintOutTime) {
    Serial.print("Lights total ");
    Serial.print(lights);
    Serial.print(" ** Tempurature ");
    Serial.print(Celcius);
    Serial.print("deg C");
    Serial.print(" ** Wiper Pot Value ");
    Serial.print(potValue);
    Serial.print(" ** Wipe Mode ");
    Serial.print(WipeMode);
    Serial.println("");
    Serial.println("");

    serialPrintOutTime = millis() + (serialPrintDelay);
  }

  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

  lights = (slVal * 2) + (dbVal * 4) + (mbVal * 8) + (FlashLeftHLVal * 16) + (FlashRightHLVal * 32);

  if (lights == 60) { //This gets us out of the below being true, turing on side lights cancels the daft lights.
    RandomVal = LOW;
    if (RandomVal != OldRandomVal) {//One time it resettes the lights to off.
      digitalWrite(fld, HIGH);
      digitalWrite(frd, HIGH);
      digitalWrite(dmb, HIGH);
      digitalWrite(dil, HIGH);
      digitalWrite(dir, HIGH);
      digitalWrite(ris, HIGH);
      digitalWrite(lis, HIGH);
      OldRandomVal = RandomVal;
    }
  }

  if (RandomVal == HIGH) { //This fixes the case so that we're stuck in that being true
    lights = 14;
    OldRandomVal = RandomVal;
  }

  if (dlVal == LOW && lights != 62) { //Dash lights will come on if any lights are turns on AND the switch is on.
    digitalWrite(dls, LOW);
  } else {
    digitalWrite(dls, HIGH);
  }

  mainLeftOut = digitalRead(44); //Turns on the mainbeam indiator lamp only if the mainbeam lights are on
  mainRightOut = digitalRead(42);
  if (mainLeftOut == 0 || mainRightOut == 0) {
    digitalWrite(dmb, HIGH);
  } else {
    digitalWrite(dmb, LOW);
  }

  switch (lights) {
    //*****************START****This is regular lights functions as normal car:
    case 60:   // Side Lights
      digitalWrite(frs, LOW);  // HIGH is off
      digitalWrite(fls, LOW);
      digitalWrite(frd, HIGH);
      digitalWrite(fld, HIGH);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, LOW);  // HIGH is off
      break;
    case 56: //Side lights and Dipped beam
      digitalWrite(frs, LOW);  // HIGH is off
      digitalWrite(fls, LOW);
      digitalWrite(frd, LOW);
      digitalWrite(fld, LOW);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, LOW);  // HIGH is off
      break;
    case 48: //Side lights and Mainbeam on
      digitalWrite(frs, LOW);  // HIGH is off
      digitalWrite(fls, LOW);
      digitalWrite(frd, HIGH);
      digitalWrite(fld, HIGH);
      digitalWrite(frm, LOW);
      digitalWrite(flm, LOW);
      digitalWrite(tls, LOW);  // HIGH is off
      break;
    //*****************END****This is the end of regular lights function.

    //*****************START****This left OR right buttons silly:
    case 46: //No lights on and press left buttom
      digitalWrite(frs, HIGH);  // HIGH is off
      digitalWrite(fls, LOW);
      digitalWrite(frd, HIGH);
      digitalWrite(fld, LOW);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, HIGH);  // HIGH is off
      break;
    case 30: //No lights on and press RIGHT buttom
      digitalWrite(frs, LOW);  // HIGH is off
      digitalWrite(fls, HIGH);
      digitalWrite(frd, LOW);
      digitalWrite(fld, HIGH);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, HIGH);  // HIGH is off
      break;
    case 44: //Side lights on and press left buttom
      digitalWrite(frs, HIGH);  // HIGH is off
      digitalWrite(fls, LOW);
      digitalWrite(frd, HIGH);
      digitalWrite(fld, LOW);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, LOW);  // HIGH is off
      break;
    case 28: //Side lights on and press RIGHT buttom
      digitalWrite(frs, LOW);  // HIGH is off
      digitalWrite(fls, HIGH);
      digitalWrite(frd, LOW);
      digitalWrite(fld, HIGH);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, LOW);  // HIGH is off
      break;
    case 38:
    case 40: //Side lights and dipped on and press left buttom
      digitalWrite(frs, LOW);  // HIGH is off
      digitalWrite(fls, HIGH);
      digitalWrite(frd, LOW);
      digitalWrite(fld, HIGH);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, LOW);  // HIGH is off
      break;
    case 22:
    case 24: //Side lights and dipped on and press RIGHT buttom
      digitalWrite(frs, HIGH);  // HIGH is off
      digitalWrite(fls, LOW);
      digitalWrite(frd, HIGH);
      digitalWrite(fld, LOW);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, LOW);  // HIGH is off
      break;
    case 32: //Side lights and Mainbeam on and press left buttom
      digitalWrite(frs, LOW);  // HIGH is off
      digitalWrite(fls, HIGH);
      digitalWrite(frd, HIGH);
      digitalWrite(fld, HIGH);
      digitalWrite(frm, LOW);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, LOW);  // HIGH is off
      break;
    case 16: //Side lights and Mainbeam on and press RIGHT buttom
      digitalWrite(frs, HIGH);  // HIGH is off
      digitalWrite(fls, LOW);
      digitalWrite(frd, HIGH);
      digitalWrite(fld, HIGH);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, LOW);
      digitalWrite(tls, LOW);  // HIGH is off
      break;
    //*****************START****This left AND right buttons silly:
    case 0: //Mainbeam lights on and press left AND Right buttom
      digitalWrite(frs, HIGH);  // HIGH is off
      digitalWrite(fls, HIGH);
      digitalWrite(frd, LOW);
      digitalWrite(fld, LOW);
      digitalWrite(frm, HIGH);
      digitalWrite(flm, HIGH);
      digitalWrite(tls, HIGH);  // HIGH is off
      break;
    case 54: //NO lights on and flick mainbeam on stalk
    case 12: //Side lights on and press left AND Right buttom
    case 8: //Dipped lights on and press left AND Right buttom
      digitalWrite(frs, HIGH);  // HIGH is off
      digitalWrite(fls, HIGH);
      digitalWrite(frd, HIGH);
      digitalWrite(fld, HIGH);
      digitalWrite(frm, LOW);
      digitalWrite(flm, LOW);
      digitalWrite(tls, HIGH);  // HIGH is off
      break;

    case 14: //No lights on and press left AND Right buttom - DAFT LIGHTS GO GO GO
      RandomVal = HIGH;
      if (millis() >= DelayTime) {
        RandomNumber = random(9);
        RandomOnOff = random(2);
        DelayTime = millis() + random(80, 250);
        Serial.println(RandomNumber);
      }
      switch (RandomNumber) {
        case 0:
          digitalWrite(frs, RandomOnOff);
          Serial.println(" FRS lights turned on   ");
          break;
        case 1:
          digitalWrite(fls, RandomOnOff);
          break;
        case 2:
          digitalWrite(frd, RandomOnOff);
          digitalWrite(dmb, RandomOnOff);
          break;
        case 3:
          digitalWrite(fld, RandomOnOff);
          digitalWrite(dmb, RandomOnOff);
          break;
        case 4:
          digitalWrite(ris, RandomOnOff);
          digitalWrite(dir, RandomOnOff);
          break;
        case 5:
          digitalWrite(lis, RandomOnOff);
          digitalWrite(dil, RandomOnOff);
          break;
        case 6:
          digitalWrite(frs, RandomOnOff);
          digitalWrite(frd, RandomOnOff);
          digitalWrite(dmb, RandomOnOff);
          digitalWrite(ris, RandomOnOff);
          digitalWrite(dir, RandomOnOff);
          break;
        case 7:
          digitalWrite(fls, RandomOnOff);
          digitalWrite(fld, RandomOnOff);
          digitalWrite(dmb, RandomOnOff);
          digitalWrite(lis, RandomOnOff);
          digitalWrite(dil, RandomOnOff);
          break;
        case 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);
          break;
        default:
          digitalWrite(fls, HIGH);
          digitalWrite(fld, HIGH);
          digitalWrite(dmb, HIGH);
          digitalWrite(lis, HIGH);
          digitalWrite(dil, HIGH);
          digitalWrite(frs, HIGH);
          digitalWrite(frd, HIGH);
          digitalWrite(ris, HIGH);
          digitalWrite(dir, HIGH);
          break;
      }

      break;
    default:
      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); //*************************************************************************************************
      break;
  }










  //***************Indicators Script************************************************************
  //****RIGHT INDICATOR

  if (riVal != riValLast && riVal == LOW  && hzVal == HIGH && liLatch == HIGH) { //check to see if state of switch changed.
    riValTime = millis();
    if (millis() >= serialPrintOutTime) {
      Serial.println("   riVal LOW    ");
    }
    riValLast = riVal;
  }
  if (riValTime + 20 > millis() && millis() < riValTime + 25 && riVal == LOW) { // debounce switch
    riLatch = LOW;
    indicatorCurrMillis = millis();
    if (millis() >= serialPrintOutTime) {
      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
      if (millis() >= serialPrintOutTime) {
        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();
    if (millis() >= serialPrintOutTime) {
      Serial.println("  first if  ");
    }
    liValLast = liVal;
  }
  if (liValTime + 20 > millis() && millis() < liValTime + 25 && liVal == LOW) { // debounce switch
    liLatch = LOW;
    indicatorCurrMillis = millis();
    if (millis() >= serialPrintOutTime) {
      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
      if (millis() >= serialPrintOutTime) {
        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
      if (millis() >= serialPrintOutTime) {
        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();
    if (millis() >= serialPrintOutTime) {
      Serial.println("  first if  ");
    }
    hzValLast = hzVal;
  }
  if (hzValTime + 20 > millis() && millis() < hzValTime + 25 && hzVal == LOW) { // debounce switch
    hzLatch = LOW;
    indicatorCurrMillis = millis();
    if (millis() >= serialPrintOutTime) {
      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
      if (millis() >= serialPrintOutTime) {
        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
      if (millis() >= serialPrintOutTime) {
        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

  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
  WKdebouncer.update(); // Update the Bounce instance

  if (potValue >100 && potValue < 200) {
    WipeMode = 2; // RUN Constantly
  } else if (potValue > 200) {
    WipeMode = 1; // Intermitant wiping
  } else {
    WipeMode = 0;
  }

  if (WipeMode == 2) {
    digitalWrite(wip, LOW); //Set wipers running
  }
  if (WipeMode == 1) {
    if (WKdebouncer.fell()) {  // IF Park just enagaed then..
      digitalWrite(wip, HIGH); //Trun off Wipers as they're done and parked.
      WiperPreviousMillis = millis(); //sets the time at stop
    }
    if (millis() >= WiperPreviousMillis + DelayVAL) {
      digitalWrite(wip, LOW); //Run Wipers
    }
  }
  if (wkVal == LOW && potValue > 200) {
    digitalWrite(wip, HIGH); //Trun off Wipers as they're done and parked.
  }

  /*
      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 (WKdebouncer.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 (WKdebouncer.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);
    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--------------------------------
}