   choix = 0;
   delay(1500);
 }
-updateValue();
-}
-}
-
-bool configureSelection()                             // Changes the value of "selection" depending on the position of the encoder
-{
-  long newPos;
+updateValue(setpointHigh, setpointLow, VinMax, hysteresis);
+}
+}
+
+char configureSelection(int sensorValue, float setpointHigh, float setpointLow, float maxControllerVoltage, long & newPos, long & posit, int limitSetpointHighInAnalog, int limitSetpointLowInAnalog)                             // Changes the value of "selection" depending on the position of the encoder
+{
+  char selection;
+  int intensityInPercent;
+  
   newPos = rotEncoder.read() - posit;
 
   Serial.println(newPos);
   if(( newPos >= -2) & (newPos < 2))
   {
     selection = 1;
-    
+    return(selection);
   }
   else if(((newPos >= 2) && (newPos < 6)) || ((newPos < -10) && (newPos >= -14)))
   {
     selection = 0;
+    intensityInPercent = getIntensityInPercent(selection,sensorValue,setpointHigh,setpointLow, maxControllerVoltage, limitSetpointHighInAnalog, limitSetpointLowInAnalog);
     if(intensityInPercent != 100)
     {
       digitalWrite(overchargeIndicatorLed, LOW);
       intensityInPercent =0;
     }
+    return(selection);
   }
   else if(((newPos >= 6) && (newPos < 10)) || ((newPos < -6) && (newPos >= -10)))
   {
     selection = 2;
+    return(selection);
   }
   else if(((newPos >= 10) && (newPos < 14)) || ((newPos < -2) && (newPos >= -6)))
   {
     selection = 3;
+    return(selection);
   }
   else
   {

  }
}

void seriesRegulator()                            // The function that does the regulation
{
  char i;
  for (i=0; i<11; i++)
  {
    delay(50);
    sensorValue = analogRead(analogInPinV);
    getVin(sensorValue);   
    controlMosfet();
    //CheckButtonA();
  } 
}

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

void controlShuntPWM(void)                        
{
  if(verificationSetpointHigh(sensorValue,setpointHigh)==1)     //Voltage higher than setpointHigh
  {
    digitalWrite(overchargeIndicatorLed, HIGH);                 // Turn on the led // Overcharged
  }
  else if (verificationSetpointLow(sensorValue,setpointLow)==1) //Voltage lower than setpointLow
  {
    digitalWrite(overchargeIndicatorLed, LOW);                  // Turn off the led // Not overcharged
  }
  else
  {
    PWMwrite(sensorValue, overchargeIndicatorLed);              // Hysteresis
  }
}

void controlShuntNoPWM(void)
{
  if(verificationSetpointHigh(sensorValue,setpointHigh)==1)     //Voltage higher than setpointHigh
  {
    digitalWrite(overchargeIndicatorLed, HIGH);                 // Turn on the led // Overcharged
  }
  else if (verificationSetpointLow(sensorValue,setpointLow)==1) //Voltage lower than setpointLow
  {
    digitalWrite(overchargeIndicatorLed, LOW);                  // Turn off the led // Not overcharged
  }
  else
  {
                                                                // Hysteresis
  }
}

void controlMosfet()                                            // Selection of the control mode
{
  if (selection == 0)
  {
    controlSerieNoPWM(); 
  }
  else if (selection == 1)
  {
    controlSeriePWM(); 
  }
  else if (selection == 2)
  {
    controlShuntNoPWM(); 
  }
  else if (selection == 3)
  {
    controlShuntPWM(); 
  }
}

char getIntensityInPercent()                                    // Turn sensorValue into a percentage depending on the value of "selection"
{
  
  if (selection == 2)
  {
    if (verificationSetpointHigh(sensorValue,setpointHigh) == 1)
    {
      intensityInPercent = 100; 
    }
    else if (verificationSetpointLow(sensorValue,setpointLow) == 1)
    {
      intensityInPercent = 0;
    }

float setpointHigh = (VinMax + hysteresis);  // Set setpointHigh Caution the battery may go beyond the overcharge
float setpointLow = (VinMax - hysteresis) ;  // Set setpointLow
unsigned int times;                          // Defines the time between two points when drawing the curve
char selection = 0;                          // Choose true for PWM, false for no PWM*/

//float valuesArray[8]; // 0: Vinmax; 1: R1; 2: R2; 3: maxControllerVoltage; 4: hysteresis; 5: setPointHigh; 6: setPointLow; 7: times
//int limitOverchargeInAnalog;                 // Limit of the overcharge in analog value
//float limitOverchargeInVolt;                 // Limit of the overcharge in voltage
//float limitSetpointHighInVolt;               // Limit of the setpointhigh in voltage
//float limitSetpointLowInVolt;                // Limit of the setpointLow in voltage
//bool verification;                           // False = Overcharged / True = not overcharged
//float Vin;                                   // Actual Value of the voltage Input
//int intensityInPercent;
//char state = 0;
/* Value read from the potentiometers */
//int sensorValue = 0;       
//int ampvalue = 0;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
