   
 }
 
-bool verificationSetpointHigh(int sensorValue, float setpointHigh)                           // Check if sensorValue is higher than setpointHigh
-{
+bool verificationSetpointHigh(int sensorValue, float setpointHigh, float maxControllerVoltage, int & limitSetpointHighInAnalog)                           // Check if sensorValue is higher than setpointHigh
+{
+  float limitSetpointHighInVolt;
   limitSetpointHighInVolt = setpointHigh ;
   limitSetpointHighInAnalog = 1024 * ( limitSetpointHighInVolt / maxControllerVoltage) ;
   


volatile int f_wdt=1;                                             // Sleep arduino

long posit = 0;                                                   // This value will be the previous value of the position of the rotary encoder

const int analogInPinV = A0;                 // Analog input pin that the potentiometer (Voltmeter) is attached to / Default = A0
const int analogInPinA = A1;                 // Analog input pin that the potentiometer (Ammeter) is attached to / Default = A1
const int buttonC = 4;                       // Digital input pin that the button of the rotary encoder is attached to / Default = 4

char selection = 0;                          // Choose true for PWM, false for no PWM
const int  overchargeIndicatorLed = 5;       // LedOutput  Default = 2

float VinMax;                                // Maximum Input Voltage Default = 4
unsigned int R1;                             // Bottom resistance Kohm Default = 10
unsigned int R2 ;                            // top resistance Kohm     Default = 100
float maxControllerVoltage ;                 // the arduino Uno boardcard cant excess 5 volt   default = 5v

  }
}

void controlSeriePWM(float maxControllerVoltage, int sensorValue, float setpointHigh, float setpointLow, int limitSetpointHighInAnalog, int limitSetpointLowInAnalog)
{
  if(verificationSetpointHigh(sensorValue,setpointHigh,maxControllerVoltage, limitSetpointHighInAnalog)==1)     //Voltage higher than setpointHigh
  {
    digitalWrite(overchargeIndicatorLed, LOW);                  // TURN on the led // Overcharged
  }
  else if (verificationSetpointLow(sensorValue,setpointLow,maxControllerVoltage, limitSetpointLowInAnalog)==1) //Voltage lower than setpointHigh
  {
    digitalWrite(overchargeIndicatorLed, HIGH);                 // Light off the led // Not overcharged
  }
  else
  {
    PWMwriteShunt(sensorValue, overchargeIndicatorLed, limitSetpointHighInAnalog, limitSetpointLowInAnalog);         // Hysteresis
  }
}

void PWMwriteShunt(int intensity, int outputPin, int limitSetpointHighInAnalog, int limitSetpointLowInAnalog) //Generate the PWM square wave with the right duty cycle
{ 
  int mappedIntensity;
  mappedIntensity = map(intensity,limitSetpointHighInAnalog,limitSetpointLowInAnalog, 0, 255);
  analogWrite(outputPin, mappedIntensity);
}

ISR(WDT_vect)                             //Watchdog Interrupt Service. This is executed when watchdog timed out
{
  if(f_wdt == 0)
  {
    f_wdt=1; 
  }
  else
  {
             //Serial.println("WDT Overrun!!!");
  }
}

void updateValue(float & setpointHigh, float & setpointLow, float VinMax, float hysteresis)                    // Update setpointHigh and setpointLow depending on VinMax and hysteresis
{
  setpointHigh = (VinMax + hysteresis);
  setpointLow = (VinMax - hysteresis) ;
}

void initEEPROMValue (float & VinMax, float & maxControllerVoltage, unsigned int & R1, unsigned int & R2, float & hysteresis, unsigned int & times)               // initialization of VinMax, maxControllerVoltage, R1, R2, hysteresis and times with the last values stored in the EEPROM
{
  VinMax = EEPROM.readFloat(0);
  maxControllerVoltage = EEPROM.readFloat(4);
  R1 = EEPROM.read(8);
  R2 = EEPROM.read(12);
  hysteresis = EEPROM.readFloat(16);
  times = EEPROM.readInt(20);
}

void drawBatteryLimit(float VinMax, float maxControllerVoltage)
{ 
  char i;
  float mappedVinMax;
