// Experimenting v6
// PAS Cadence Input Pin - D2 (Digital)
// PAS Torque Input Pin - A3 (Analog)
// Throttle Input Pin - A0 (Analog)
// Controller Cadence Output Pin - D13 (Digital)
// Controller Torque Output Pin - A5 (Analog)// Experimenting
const byte ThrottlePin = A0;
const byte CadenceInputPin = A2;
const byte TorqueInputPin = A3;
const byte TorqueOutputPin = A5;
const byte CadenceOutputPin = 13;
int pulseoff = 100; // milliseconds when synthesised cadence is low
int pulseon = 66; // milliseconds when synthesised cadence is high 6Hz
int cadencepulselength = 0; // in milliseconds
bool cadencestate = false;
bool pulse = false;
unsigned long startMillis;
unsigned long currentMillis;
void setup() {
pinMode(CadenceOutputPin, OUTPUT);
pinMode(TorqueInputPin, INPUT);
pinMode(TorqueOutputPin, OUTPUT); //ControllerTorquePin
pinMode(CadenceInputPin, INPUT); //PASCadencePin
digitalWrite(TorqueOutputPin, LOW);
startMillis = millis();
// Serial.begin(9600);
}
void loop() {
if (millis()>currentMillis) // run once every millisecond
{
currentMillis = millis();
// increment cadencepulselength
if (cadencepulselength < 1000) cadencepulselength++;
if (cadencestate)
{
// on cadencestate transtion from high to low, reset cadencepulselength
if (analogRead(CadenceInputPin) < 512)
{
cadencepulselength = 0;
cadencestate = false;
}
}
if (analogRead(CadenceInputPin) > 512) cadencestate = true;
// check throttle
int torqueval = analogRead(ThrottlePin) / 4; // Read the value from the throttle pin A0
if (torqueval < 75) // disable throttle if voltage < 1.5V
{
// I'm pedalling
int pastorque = analogRead(TorqueInputPin)/4; // torque input A3
// if I'm stopping for more than 1 second, set A5 to 3.5V to stop error 21
// if (cadencepulselength > 990) pastorque = 200;
// replicate torque and cadence
analogWrite(TorqueOutputPin, pastorque);
if (analogRead(CadenceInputPin) > 512) digitalWrite(CadenceOutputPin, HIGH);
else digitalWrite(CadenceOutputPin, LOW);
}
else
{
//I'm using the throttle
analogWrite(TorqueOutputPin, torqueval);
pulse = true;
if (currentMillis > (startMillis + pulseon)) pulse = false;
if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis(); // reset timer clock
if (pulse) digitalWrite(CadenceOutputPin, HIGH);
else digitalWrite(CadenceOutputPin, LOW);
}
}
}