//Arduino code for Asset No. 509049A
//This program measures the VCC from power supply
//Then if VCC = 28VDC times are set between 0.5 to 1 s according to CMM
//If VCC = 18VDC times are set between 0 to 2 s according to CMM
//If voltage is not set correctly 18 or 28 VDC arduino Resets until Voltage is set correctly
// time elapsed is equal to operation from one extreme position (CW or CCW) to the other
//Blinks Pass or Fail LED according to Cycle time elapsed
//All the pins sensing voltage should have a Voltage Divisor in order to acconditionate the amplitude from 18-28 VDC to 5VDC max wich what the arduino can accept from digital inputs
#include <avr/wdt.h> // Include the watchdog timer library used in software reset

//Defining Pins
const int sel_CW = 3;          //connected to CW unit pin # 1 
const int sel_CCW = 2;         //connected to CCW unit pin # 2
const int feedback_CW = 11;    //connected to CW feedback unit pin # 3
const int feedback_CCW = 10;   //connected to CCW feedback unit pin # 4
const int CW_Indicator = 8;    //LED output
const int CCW_Indicator = 7;   //LED output
const int Wrong_Voltage = 6;   //LED output
const int Mode_18V = 5;        //LED output
const int Mode_28V = 4;        //LED output
const int Pass_LED = 13;       //LED output
const int Fail_LED = 12;       //LED output
const int VCC_sensor = A0;     //Pin to sense VCC 

//Definig variables
float analog_Voltage = 0;  //Storing VCC to check the mode
float start_time = 0;      //Store time when movement starts
float finish_time = 0;     //Store time when movement is done
float elapsed_time = 0;    //elapsed_time = finish_time - start_time
float minimum_time = 0;    //Set to 0.5 or 0 according to the mode 18VDC or 28VDC
float maximum_time = 0;    //Set to 1 or 2 according to the mode 18VDC or 28VDC
int sel_CW_state = LOW;
int sel_CW_last_state = LOW;
int sel_CCW_state = LOW;
int sel_CCW_last_state = LOW;

//Function to restart the code if the voltage is not set correctly
void resetArduino() 
{
  wdt_enable(WDTO_15MS); // Enable the watchdog timer with a short timeout
  while (1); // Enter an infinite loop to trigger a reset
}  

void setup() 
{
  // Initialize the digital pin as an OUTPUT
  pinMode(CW_Indicator, OUTPUT);
  pinMode(CCW_Indicator, OUTPUT);
  pinMode(Wrong_Voltage, OUTPUT);
  pinMode(Mode_18V, OUTPUT);
  pinMode(Mode_28V, OUTPUT);
  pinMode(Pass_LED, OUTPUT);
  pinMode(Fail_LED, OUTPUT);
  digitalWrite(Pass_LED, LOW);
  digitalWrite(Fail_LED, LOW);
  Serial.begin(115200);
}

void loop() 
{
analog_Voltage = (analogRead(VCC_sensor)* 5 ) / 1023.0;  //Reading voltage from power supply
turn_on_voltage_indicator();  // this function handles the LED indicating voltage and the times for each case
turn_indicators();

sel_CW_state = digitalRead(sel_CW); // detecting Rise edge on sel_CW
if ( sel_CW_state == HIGH && sel_CW_last_state == LOW)
  {
    delay(400);  //reread to check debounce time
    if (digitalRead(sel_CW) == HIGH)
    {
      start_test();  //start if high excitation value was detected
    }
  }
sel_CW_last_state = sel_CW_state;

sel_CCW_state = digitalRead(sel_CCW); // detecting Rise edge on sel_CCW
if ( sel_CCW_state == HIGH && sel_CCW_last_state == LOW)
  {
    delay(400);
    if (digitalRead(sel_CCW) == HIGH)
    {
      start_test();  //start if high excitation value was detected
    }
  }
sel_CCW_last_state = sel_CCW_state;

}

void turn_on_voltage_indicator()
{
if (analog_Voltage >= 4.405 && analog_Voltage <= 4.732)
{
  //here is baceuse VCC is 28 Volts then actuator should move fast
  digitalWrite(Mode_28V, HIGH);
  digitalWrite(Mode_18V, LOW);
  digitalWrite(Wrong_Voltage, LOW);
  minimum_time = 0;
  maximum_time = 1;
}
else if (analog_Voltage >= 2.774 && analog_Voltage <= 3.1)
{
  //here is baceuse VCC is 18 Volts then actuator should move slow
  digitalWrite(Mode_28V, LOW);
  digitalWrite(Mode_18V, HIGH);
  digitalWrite(Wrong_Voltage, LOW);
  minimum_time = 0;
  maximum_time = 2;
}
else
{
  digitalWrite(Mode_28V, LOW);
  digitalWrite(Mode_18V, LOW);
  digitalWrite(Wrong_Voltage, HIGH);
  resetArduino();  //if VCC is not selected correctly Arduino goes to reset until Voltage will be set correctly
}
}

void turn_indicators()
{
if (digitalRead(feedback_CW)==HIGH)
  {
    digitalWrite(CW_Indicator, HIGH);  //turn on CW LED indicator
    digitalWrite(CCW_Indicator, LOW);  //turn off CCW LED indicator
  }
else if (digitalRead(feedback_CCW)==HIGH)
  {
    digitalWrite(CW_Indicator, LOW);   //turn off CW LED indicator
    digitalWrite(CCW_Indicator, HIGH); //turn on CCW LED indicator
  }
else
{
  digitalWrite(CW_Indicator, LOW);   //turn off CW LED indicator
  digitalWrite(CCW_Indicator, LOW);  //turn off CCW LED indicator
}
}

void start_test()
{
int max_time = (maximum_time*1000) - 400;
delay(max_time);
//Serial.println(max_time);
turn_indicators();
if (digitalRead(feedback_CW)==HIGH || digitalRead(feedback_CCW)==HIGH)  //Measuring time if CW starts the movement
{
for (int i = 0; i < 10 ; i++)
  {
    digitalWrite(Pass_LED, !digitalRead(Pass_LED));
    delay(200);
  }
}

else
{
  for (int i = 0; i < 10 ; i++)
  {
    digitalWrite(Fail_LED, !digitalRead(Fail_LED));
    delay(200);
  }
}
//Serial.println(elapsed_time);
}
CW
CCW
CW pin3
CCW pin4
Selectrion
Feedback