 This file ensures the control of a continuous current motor and gearbox through an H-bridge.
 A Pulse Width Modulation is used. The algorithm uses varying cycle frequency to allow control at low rotation speed.
 Due to minimum quantity of energy to make the motor turn, the motor will not turn smoothly at low speed.
-There is now feedback nor stall control on speed, so the output rotation speed may depend on load torque.
+There is no feedback nor stall control on speed, so the output rotation speed may depend on load torque.
 The motor is controlled through a parameter alpha which is signed and vary from -1 to -1.
-This parameter can be set thanks to a an analog voltage (set through a potentiometer for example) or sent over serial connection
+This parameter can be set thanks to a an analog voltage (set through a potentiometer used as joystick for example) or sent over serial connection
 The analog voltage is taken by default.
 If the analog voltage is not zero, the motor is left free of moving
 To start the serial control send "i" in a serial monitor


void loop()
{
  digitalWrite(HbridgeEnablePin, HIGH);
  // Read the analog in value
  // The range was reduced 
  double sensorValueMin = 255;//0
  double sensorValueMax = 767;//1023
  double deadBand = 0.05; // Use to ensure zero speed. Should not be zero
  sensorValue = analogRead(analogInPin);
  
  // If the analog value is changed, it means that the user wants to take control manually
  // so fallback to manual control
  if (fabs(initialSensorValue-sensorValue)>10)
  {
    isSerialControl = false;
  }
  if (sensorValue < 10)
  { 
    // Motor is free to move to enable manual control
    digitalWrite(HbridgeEnablePin, LOW);
  }
  if (false==isSerialControl)
  {
    alphaSigned = 2*((sensorValue-sensorValueMin)/(sensorValueMax - sensorValueMin)-0.5);  
  }
  double alpha = max(fabs(alphaSigned), deadBand);
  alpha = min(alpha, 1);

  if (alphaSigned>fabs(deadBand))
  {
    // Forward rotation
    digitalWrite(HbridgeLogicPin1, HIGH);

    }
    else
    {
      // Brake ?
      digitalWrite(HbridgeLogicPin1, LOW);
      digitalWrite(HbridgeLogicPin2, LOW);
    }
	// \todo: add an input to enable manual rotation of the motor
  }
  return alpha;
}


