ok so i have deviated a little from the original pin design we set out but i hope it still makes sense, I attempted to work from the original post in terms of wiring (just for my own sanity so I'm working from something proven). Also I have commented out the digital interrupt while i experiment with throttle. I'll do some more testing on the weekend.
My Pins are as follows (attempting to stay closely with the original pin layout in the original post):
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)
My Pins are as follows (attempting to stay closely with the original pin layout in the original post):
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)
Code:
// Experimenting
int pulseoff = 600;
int pulseon = 0;
int val = 0;
int throttleperc = 0;
int torqueperc = 0;
int torqueval = 0;
bool pulse = false;
bool throttle = false;
int pedalin = 2;
unsigned long startMillis;
unsigned long currentMillis;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(A3, INPUT); //PASTorquePin
pinMode(A5, OUTPUT); //ControllerTorquePin
pinMode(pedalin, INPUT);
digitalWrite(LED_BUILTIN, LOW);
//attachInterrupt(digitalPinToInterrupt(2), pedalon, RISING);
startMillis = millis();
Serial.begin(9600);
}
void loop() {
currentMillis = millis();
val = analogRead(A0); // Read the value from the throttle pin A0
throttleperc = ((float)(val - 290) / (float)577) * 100; // calculate percentage increase decrease in throttle 577 is the range from min-max 290 minimum
torqueperc = 164 * ((float)(val - 290) / (float)577); // calculate the torque increase / decrease value from max 164 range
torqueval = 76 + torqueperc; // 76 minimum using range 164 it will never exceed 240 which is 4.3v
pulseoff = (600 - (val / 1.7)) + 120;
pulseon = int(pulseoff / 3);
if (pulse == false) {
if (pulseoff < 550) {
if (throttle == false) { //removes the halfsecond possible delay on first start up.
digitalWrite(LED_BUILTIN, HIGH);
throttle = true;
pulse = true;
} else {
analogWrite(A5, torqueval); // This value will always be in a range from 1.5v (76) to 4.3v (240) outside of the pulsing IF statement
if (currentMillis > (startMillis + pulseoff)) { //send pulse
digitalWrite(LED_BUILTIN, HIGH);
pulse = true;
startMillis = millis();
throttle = true;
}
}
} else {
throttle = false;
}
} else {
if (currentMillis > (startMillis + pulseon)) {
pulse = false;
digitalWrite(LED_BUILTIN, LOW); // pulse off signal to controller
analogWrite(5, torqueval); // will always be minimum 76
}
}
}
void pedalon() //Called when pedal is moved
{
if (throttle == false) { //Only passes signal if throttle is not being used, otherwise ignored.
if (digitalRead(2) == HIGH) {
digitalWrite(LED_BUILTIN, HIGH);
analogWrite(A5, analogRead(A3)); //Attempt to pass what ever value comes from the Torque sensor straight to the controller
} else {
digitalWrite(LED_BUILTIN, LOW);
analogWrite(A5, analogRead(A3)); //Not pedalling anymore so again just pass the torque value straight through to the controller
}
Serial.println("pedal round..");
}
}
Last edited: