// Experimenting v4
// PAS Cadence Input Pin - A2 (Analog)
// PAS Torque Input Pin - A3 (Analog)
// Throttle Input Pin - A0 (Analog)
// Controller Cadence Output Pin - D13 (Digital)
// Controller Torque Output Pin - A5 (Analog)
int pulseoff = 150; // milliseconds when synthesised cadence is low
int pulseon = 100; // milliseconds when synthesised cadence is high 6Hz
int val = 0;
bool pulse = false;
bool throttle = false;
int throttleperc = 0;
int torqueperc = 0;
int torqueval = 76;
//int pedalin = 2;
unsigned long startMillis;
unsigned long currentMillis;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(A3, INPUT); //PASTorquePin
pinMode(A5, OUTPUT); //ControllerTorquePin
pinMode(A2, INPUT); //PASCadencePin
digitalWrite(LED_BUILTIN, LOW);
startMillis = millis();
Serial.begin(9600);
}
void loop() {
if (millis()>currentMillis) // run once every millisecond
{
currentMillis = millis();
val = analogRead(A0); // Read the value from the throttle pin A0
//throttleperc = ((float)(val - 290) / (float)577) * 100;
//torqueperc = 164 * ((float)(val - 290) / (float)577);
//torqueval = 76 + torqueperc;
torqueval = 0;
if (val < 290)
{
// I'm pedalling
int pascadence = 0;
int pastorque = 0;
pascadence = analogRead(A2); // Read the value from the PASCadence Pin
pastorque = analogRead(A3); // Read the value from the PASTorque Pin
//Serial.print("Pas Cadence value is ");
//Serial.print(pascadence);
//Serial.print(" PAS Torque value is ");
//Serial.print(pastorque);
//Serial.println("");
int cadence_threshold = 500;
analogWrite(A5, analogRead(A3)); // you need to check synthesised torque with a multitester
if (analogRead(A2) > cadence_threshold) digitalWrite(LED_BUILTIN, HIGH);
else digitalWrite(LED_BUILTIN, LOW);
}
else
{
//I'm using the throttle
//torqueval = 76+(val-290)*164/287; // your experimental formulae
torqueval = (308 + val * (880-308)/(577-290));
analogWrite(A5, torqueval);
pulse = true;
if (currentMillis > (startMillis + pulseon)) pulse = false;
if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis(); // reset timer clock
if (pulse) digitalWrite(LED_BUILTIN, HIGH);
else digitalWrite(LED_BUILTIN, LOW);
}
}
}