How to add a throttle to a Carrera Vengeance E Spec

Woosh

Trade Member
May 19, 2012
20,381
16,878
Southend on Sea
wooshbikes.co.uk
you said the throttle stopped working which is unexpected.
can you think of any reason?
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Yes i can think of a reason.
Plugged the torque pin to the 3.3v and i got no error 21 and the throttle works (didn't test further than this). Think it has something to do with timing or bike firmware, since on previous posts raj was able to get his working and he had his bootloader so his nano would be 2.1 seconds slower on bootup than mine.
 

Woosh

Trade Member
May 19, 2012
20,381
16,878
Southend on Sea
wooshbikes.co.uk
do you still have a version of the code when you've got the throttle working?
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Possibly the first modifications that me and raj were working on.
I'll submit it here once i confirm i got the right one.
In the meantime i just ordered from amazon 3x nano's so i can experiment with bootloaders and other versions (easier to debug and get the project working faster)
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
This is the code, full of bugs and all its glory:
(It only works when i power on the bike and have my thumb on the throttle to supply power right away but when i release the throttle the motor will just slowly lose power until the HZ and the Voltage gets to a level that is unstable to sync and produces error 21)
Now the pedalon function will not work of course and that is expected.

Code:
int val = 0;               //Throttle value from Throttle
int val2 = 0;              //Torque value from PAS Value
int val3 = 0;              //Cadence Signal value from PAS
int ThrottlePin = 1;       //Input Analog Throttle signal
int PASTorque = 2;         //Input Analog Torque Signal from PAS
int PASCadence = 3;        //Input Analog Cadence Signal from PAS
int ControllerC = 4;       //Output Digital Cadence Signal from PAS
int ControllerT = 5;       //Output Digital Torque Signal 1.5v (76) - 4.3v which is 219 max, not 255
unsigned long startMillis;
unsigned long currentMillis;
int pulseoff = 600;
int pulseon = 0;
bool pulse = false;
bool throttle = false;

void setup() {
  pinMode(ThrottlePin, INPUT);
  pinMode(PASTorque, INPUT);
  pinMode(PASCadence, INPUT);
  pinMode(ControllerC, OUTPUT);
  pinMode(ControllerT, OUTPUT);
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(3), Pedalon, CHANGE);
}

void loop()
{
  currentMillis = millis();
  val = analogRead(ThrottlePin);
  pulseoff = (600 - (val / 1.7)) + 210;
  pulseon = int(pulseoff / 3);
  if (pulse == false) {
    if (pulseoff < 550) {
      if (throttle == false) { //removes the halfsecond possible delay on first start up.
        digitalWrite(ControllerC, HIGH);
        throttle = true;
        pulse = true;
      } else {
        analogWrite(5, analogRead(1));
        if (currentMillis > (startMillis + pulseoff)) { //send pulse
          digitalWrite(ControllerC, HIGH);
          Serial.println(pulseoff);
          pulse = true;
          startMillis = millis();
          throttle = true;
        }
      }
    } else {
      throttle = false;
    }
  } else {
    if (currentMillis > (startMillis + pulseon)) {
      pulse = false;
      digitalWrite(ControllerC, LOW);
      analogWrite(5, 75);
    }
  }
}

void Pedalon()    //Called when pedal is moved
{
  if (throttle == false) { //Only passes signal if throttle is not being used, otherwise ignored.
    if (digitalRead(3) == HIGH) { // I'm pedalling according to analog pin 3 the cadence
      digitalWrite(LED_BUILTIN, HIGH); //Light the LED to show we are working
      digitalWrite(4, analogRead(3)); //Pass the cadence value coming from PAS straight to controller
      digitalWrite(5, analogRead(2)); //Pass the torque value coming from PAS straight to controller
    } else {
      digitalWrite(LED_BUILTIN, LOW);
      analogWrite(5, 75);
      analogWrite(4, 0);
    }
    Serial.println("pedal round..");
  }
}
 

Woosh

Trade Member
May 19, 2012
20,381
16,878
Southend on Sea
wooshbikes.co.uk
Thank you for posting that code.
So you only pulse the torque A5 when the cadence D13 is high.

Let's try this code:


Code:
// 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 = 150; // milliseconds when D13 is low
int pulseon = 100;   // milliseconds when D13 is high 4Hz
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
       
        if (analogRead(CadenceInputPin) > 512)
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, pastorque);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 75);
        }
    }
    else
    {
        //I'm using the throttle
        pulse = true;
        if (currentMillis > (startMillis + pulseon)) pulse = false;
        if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
        if (pulse)  // SEND cadence and torque signal when D13 is high 
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, torqueval);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 75);
        }
    }
  }
}
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Tested it, error 21 on power up but if i power it up while holding throttle it works.
Now when i let go it cuts the motor and produces error 21.
This is indeed a step in the right direction!
 

Woosh

Trade Member
May 19, 2012
20,381
16,878
Southend on Sea
wooshbikes.co.uk
does the throttle vary the power as you expect?
try this:
keep turning the cranks and let go of the throttle to see if error 21 is delayed.

also, in your case, change this bold line to add 2 seconds delay to the setup()
startMillis = millis() + 2000;
the code should read:


void setup() {
pinMode(CadenceOutputPin, OUTPUT);
pinMode(TorqueInputPin, INPUT);
pinMode(TorqueOutputPin, OUTPUT); //ControllerTorquePin
pinMode(CadenceInputPin, INPUT); //PASCadencePin
digitalWrite(TorqueOutputPin, LOW);
startMillis = millis() + 2000; // do nothing in first 2 seconds
// Serial.begin(9600);
}
 
Last edited:

Evian1040

Pedelecer
Jul 7, 2020
75
9
Tested it.
Adding the 2 extra seconds will cause the error 21 with and without the throttle.
Removed the extra 2 seconds to try the pedals and there's a bug.
So i power on the bike my throttle to stabilize the torque and not give me 21 and then i let go of the throttle and immediately i turn the cranks, it doesn't work but the bug is that if i just put throttle a tiny bit while cranking the pedals (slowly or normally) the motor kicks in and stays at full speed without me touching the throttle and the pedals. The only way it stabilizes it by turning the pedals again and it cuts the motor and produces error 21.
 

Woosh

Trade Member
May 19, 2012
20,381
16,878
Southend on Sea
wooshbikes.co.uk
did the LED bink when you turn the cranks?
I think the value we sent to A5 may be too low.

revised code:
Code:
// Experimenting v7
// 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 = 125; // milliseconds when D13 is low
int pulseon = 125;   // milliseconds when D13 is high 4Hz
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();
  currentMillis = millis() +2000; // do nothing in first 2 seconds
  // 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 (pastorque < 100) pastorque = 100; //for testing
        // 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
   
        if (analogRead(CadenceInputPin) > 512)
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, pastorque);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 0);  // sync pulse
        }
    }
    else
    {
        //I'm using the throttle
    if (torqueval < 100) torqueval = 100; // testing
        pulse = true;
        if (currentMillis > (startMillis + pulseon)) pulse = false;
        if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
        if (pulse)  // SEND cadence and torque signal when D13 is high
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, torqueval);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 0); // sync pulse
        }
    }
  }
}
that should produce the sync pulse after the torque pulse like in the picture bottom right.
 
Last edited:

Evian1040

Pedelecer
Jul 7, 2020
75
9
did the LED bink when you turn the cranks?
I think the value we sent to A5 may be too low.

revised code:
Code:
// Experimenting v7
// 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 = 125; // milliseconds when D13 is low
int pulseon = 125;   // milliseconds when D13 is high 4Hz
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();
  currentMillis = millis() +2000; // do nothing in first 2 seconds
  // 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 (pastorque < 100) pastorque = 100; //for testing
        // 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
  
        if (analogRead(CadenceInputPin) > 512)
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, pastorque);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 0);  // sync pulse
        }
    }
    else
    {
        //I'm using the throttle
    if (torqueval < 100) torqueval = 100; // testing
        pulse = true;
        if (currentMillis > (startMillis + pulseon)) pulse = false;
        if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
        if (pulse)  // SEND cadence and torque signal when D13 is high
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, torqueval);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 0); // sync pulse
        }
    }
  }
}
that should produce the sync pulse after the torque pulse like in the picture bottom right.
I don't remember seeing the Led flashing but I will do it again with the new code once I'm back home.
Just got a notification the extra arduino Nanos are in so it can help us too.
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Back after an hour late.
EDIT: Deleted my last entry because i was an idiot and didn't see where i just plugged in...
Yes tried the code but it failed just as like before.
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
If you can share the latest version of the code I can start testing first thing tomorrow morning.
This would be the latest code, also if you have a problem, remove the line "currentMillis = millis() +2000;" as it worked for me.

Code:
// Experimenting v7
// 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 = 125; // milliseconds when D13 is low
int pulseon = 125;   // milliseconds when D13 is high 4Hz
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();
  currentMillis = millis() +2000; // do nothing in first 2 seconds
  // 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 (pastorque < 100) pastorque = 100; //for testing
        // 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
  
        if (analogRead(CadenceInputPin) > 512)
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, pastorque);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 0);  // sync pulse
        }
    }
    else
    {
        //I'm using the throttle
    if (torqueval < 100) torqueval = 100; // testing
        pulse = true;
        if (currentMillis > (startMillis + pulseon)) pulse = false;
        if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
        if (pulse)  // SEND cadence and torque signal when D13 is high
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, torqueval);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 0); // sync pulse
        }
    }
  }
}
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
So my test results from the latest code.

1. On power on I receive error 21
2. If I press on throttle and then power up motor runs without any errors.
3. Once I release the throttle I receive error 21.
4. If I turn the cranks (pedal) then power on the motor runs without any issues. Obviously goes off if i exceed 15mph.
5. Once I stop pedalling I get error 21.

Looks like we only get the error 21 when we have stopped pedaling or stopped throttling (stationary).

I did some debugging and found the below :

Code:
if (pastorque < 100) pastorque = 100; //for testing
        // 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
 
        if (analogRead(CadenceInputPin) > 512)
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, pastorque);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 0);  // sync pulse
If I change the analogWrite(TorqueOutputPin,0) to output 200 I no longer get error 21 on start up but is that a correct value?
 
Last edited:

Woosh

Trade Member
May 19, 2012
20,381
16,878
Southend on Sea
wooshbikes.co.uk
Hi Raj,
re error 21 on startup:
can you switch on, wait 5 seconds for things to settle then check the voltage on these pins: A0, A2, A3 and A5?

Evian, do you have access to an oscilloscope? if you do, can you check the waveform on A3 when the bike is at rest?
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
Hi Raj,
re error 21 on startup:
can you switch on, wait 5 seconds for things to settle then check the voltage on these pins: A0, A2, A3 and A5?

Evian, do you have access to an oscilloscope? if you do, can you check the waveform on A3 when the bike is at rest?
Values as below:

A0 - 0.83
A2 - 0.14
A3 - 3.86
A5 - 0
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Evian, do you have access to an oscilloscope? if you do, can you check the waveform on A3 when the bike is at rest?
Unfortunately I don't, if I did I would have already placed the exact values here a long time ago :)
 

Woosh

Trade Member
May 19, 2012
20,381
16,878
Southend on Sea
wooshbikes.co.uk
Thank you for the readings.

Values as below:

A0 - 0.83 (ThrottlePin)
A2 - 0.14 (CadenceInputPin)
A3 - 3.86 (TorqueInputPin)
A5 - 0 (TorqueOutputPin)
A3: TorqueAtRest shows 3.86V = 198. I updated the code to fix this.
Also, the specs say 18 cadence pulses per revolution, we need to shorten the pulseon and pulseoff to 25/25.

revised code:
Code:
// Version 8

const byte ThrottlePin = A0;
const byte CadenceInputPin = A2;
const byte TorqueInputPin = A3;
const byte TorqueOutputPin = A5;
const byte CadenceOutputPin = 13;
const byte TorqueAtRest = 198;

int pulseoff = 25; // milliseconds when D13 is low
int pulseon = 25;   // milliseconds when D13 is high 4Hz
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 (pastorque < 80) pastorque = 80;

        // replicate torque and cadence

        if (analogRead(CadenceInputPin) > 512)
        {
            digitalWrite(CadenceOutputPin, HIGH);
            // if I'm stopping for more than 1 second, set A5 to 3.8V to stop error 21
            if (cadencepulselength > 990) pastorque = TorqueAtRest;
            analogWrite(TorqueOutputPin, pastorque);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            pastorque = 0;
            // if I'm stopping for more than 1 second, set A5 to 3.5V to stop error 21
            if (cadencepulselength > 990) pastorque = TorqueAtRest;
            analogWrite(TorqueOutputPin, pastorque);  // sync pulse
        }
    }
    else
    {
        //I'm using the throttle
    if (torqueval < 100) torqueval = 100; // testing
        pulse = true;
        if (currentMillis > (startMillis + pulseon)) pulse = false;
        if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
        if (pulse)  // SEND cadence and torque signal when D13 is high
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, torqueval);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, 0);  // sync pulse
        }
    }
  }
}
 

Advertisers