How to add a throttle to a Carrera Vengeance E Spec

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
I've just tested this new code and it is exhibiting the same behaviour.

1. No errors when I power on.
2. Throttle runs nicely.
3. Release the throttle, when mph reading drops to approx 10 then error 22 is displayed.

But I'm wondering if this error is coming from the pedalon code like you said earlier?
 

Woosh

Trade Member
May 19, 2012
20,370
16,870
Southend on Sea
wooshbikes.co.uk
not sure. Try changing the formula first.
Error 22 may come from the fact that the controller knows the speed of the bike, your code doesn't, so you may have to add a procedure to gracefully exit the throttle.
Can you test the new formula for me?
Run this code:
Code:
Code:
// Experimenting
// 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)

int pulseoff = 150; // milliseconds when synthesised cadence is low
int pulseon = 100;   // pulse low  - cadence reduced to 4Hz
int val = 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);
  startMillis = millis();
  Serial.begin(9600);
}

void loop() {
    if (millis()>currentMillis) // run once every millisecond
    {
        currentMillis = millis();
        analogWrite(A5, (analogRead(A0) > 290) ? analogRead(A0)*2 + 308 : 0);
        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);
   }
}
 
Last edited:

Nealh

Esteemed Pedelecer
Aug 7, 2014
20,917
8,533
61
West Sx RH
All this techy figures and Arduino stuff is way over my head, but kudos for sticking and persevering to finding a solution that will make the code work.
 
  • Like
  • Agree
Reactions: snafu and Woosh

Woosh

Trade Member
May 19, 2012
20,370
16,870
Southend on Sea
wooshbikes.co.uk
Raj, I reduced the frequency of the cadence from 6Hz to 4Hz to see if error 22 gets shifted to lower speed accordingly.
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
not sure. Try changing the formula first.
Error 22 may come from the fact that the controller knows the speed of the bike, your code doesn't, so you may have to add a procedure to gracefully exit the throttle.
Can you test the new formula for me?
Run this code:
Code:
Code:
// Experimenting
// 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)

int pulseoff = 150; // milliseconds when synthesised cadence is low
int pulseon = 100;   // milliseconds when synthesised cadence is high 6Hz
int val = 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);
  startMillis = millis();
  Serial.begin(9600);
}

void loop() {
    if (millis()>currentMillis) // run once every millisecond
    {
        currentMillis = millis();
        analogWrite(A5, (analogRead(A0) > 290) ? analogRead(A0)*2 + 308 : 0));
        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);
   }
}
Just tested this code. Results as follows:

1. Power on the bike, returned error 21.

FYI there were too many brackets in the analogWrite line so i removed the last bracket on that line to make it compile.
 

Woosh

Trade Member
May 19, 2012
20,370
16,870
Southend on Sea
wooshbikes.co.uk
yes, there was one ) too many. I amended it and thank you for testing that code.
Can you test the last working version but with lower cadence?

int pulseoff = 150; // milliseconds when synthesised cadence is low
int pulseon = 100; // pulse low - cadence reduced to 4Hz

I try to figure out why the errors 21 and 22.
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
yes, there was one ) too many. I amended it and thank you for testing that code.
Can you test the last working version but with lower cadence?

int pulseoff = 150; // milliseconds when synthesised cadence is low
int pulseon = 100; // pulse low - cadence reduced to 4Hz

I try to figure out why the errors 21 and 22.
Just tested the previous version of the code with lower cadence, results as follows:

1. Power on the bike, no errors.
2. Run throttle, no errors, release throttle approx 3secs later error 22 displayed.

Below is the code just for reference the error may have been caused by the pedalon code shall i remove the pedalon code?:

Code:
// 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);
        }

    }
}
 

Woosh

Trade Member
May 19, 2012
20,370
16,870
Southend on Sea
wooshbikes.co.uk
Below is the code just for reference the error may have been caused by the pedalon code shall i remove the pedalon code?:
yes, it can be removed by simply supressing the cadence:

if (analogRead(A2) > cadence_threshold) digitalWrite(LED_BUILTIN, HIGH);
else digitalWrite(LED_BUILTIN, LOW);

change to:
digitalWrite(LED_BUILTIN, LOW);

could you let me know the test result?
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
yes, it can be removed by simply supressing the cadence:

if (analogRead(A2) > cadence_threshold) digitalWrite(LED_BUILTIN, HIGH);
else digitalWrite(LED_BUILTIN, LOW);

change to:
digitalWrite(LED_BUILTIN, LOW);

could you let me know the test result?
Just tested this and

1. on powering on the bike I get error 21.
2. I press the throttle then power on the bike motor runs I then let go of throttle and get error 22.

i actually think the throttle code is fine I think we now need to nail the code that runs when we are pedalling.

i think we need to take the reading from the PAS torque pin which will be between 0-1023 however when we are writing out to the controller we need to divide that value by 4 for the correct PWM. (I could be completely wrong).

to test this we should just write a minimum value of 76 to the torque output pin without the PAS wired in.

I won’t get loads of time to test this weekend but I’ll be back on testing Monday.
 
Last edited:

Woosh

Trade Member
May 19, 2012
20,370
16,870
Southend on Sea
wooshbikes.co.uk
yes, I think you are right on this.

analogWrite(A5, analogRead(A3));

Change to:
analogWrite(A5, analogRead(A3)/4);

also:

analogWrite(A5, torqueval);

change to:
analogWrite(A5, torqueval /4);
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
yes, I think you are right on this.

analogWrite(A5, analogRead(A3));

Change to:
analogWrite(A5, analogRead(A3)/4);

also:

analogWrite(A5, torqueval);

change to:
analogWrite(A5, torqueval /4);
I have made these changes and am ready to test, will update you once I have tested.
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
I'm following what has been posted here, so the code is the latest from raj with your modifications you requested to change and the diagram is the same as raj.
Now i can't debug mine since i don't have access to it (long story short my nano does not have a bootloader so it can be instant on power up on the bike, if it does have a bootloader then i get error 21 because its too slow to give the torque power, on power up)
 

Woosh

Trade Member
May 19, 2012
20,370
16,870
Southend on Sea
wooshbikes.co.uk
did your throttle work before error 21 shows up?
also, could you run this test to help me figure out where the error comes from?
note: connect cadence input to A2.
The reason is we need to be sure we can debounce the signal if needed.

Code:
// Experimenting
// 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)

unsigned long currentMillis;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(A2, INPUT); // PAS cadence Pin
  pinMode(A3, INPUT); //PASTorquePin
  pinMode(A5, OUTPUT); //ControllerTorquePin
  digitalWrite(LED_BUILTIN, LOW);
}


void loop() {   // does cadence input cause errors?
    if (millis()>currentMillis) // run once every millisecond
    currentMillis = millis();
    {
    if (analogRead(A2) < 500) digitalWrite(LED_BUILTIN, LOW);
    else  digitalWrite(LED_BUILTIN, HIGH);   // blink LED
    }
}
If all is well, it should blink the LED when you turn the cranks without producing error 21 or 22.
 
Last edited:

Evian1040

Pedelecer
Jul 7, 2020
75
9
Tried it, just on power up it went error 21.
EDIT: Tested to see if the arduino is ok and its responding ok. Also confirmed the wiring is ok by connecting the torque without the nano so it proves its the code. (wanted to make sure i'm ok on my side before sending us all on a wild goose chase)
 

Woosh

Trade Member
May 19, 2012
20,370
16,870
Southend on Sea
wooshbikes.co.uk
did you connect cadence input to A2?
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Yes, and also tried without.
EDIT: I just saw that i copied your old code before it was edited.
 

Woosh

Trade Member
May 19, 2012
20,370
16,870
Southend on Sea
wooshbikes.co.uk
did the arduino LED blink when you turn the cranks?
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
No, if it turn on the pedals there is no blinking. Turning on the throttle is a solid light.
Also it still failed with error 21 on power on
 

Advertisers