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..");
}
}
// 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);
}
}
}
}
// 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
}
}
}
}
I don't remember seeing the Led flashing but I will do it again with the new code once I'm back home.did the LED bink when you turn the cranks?
I think the value we sent to A5 may be too low.
revised code:
that should produce the sync pulse after the torque pulse like in the picture bottom right.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 } } } }
This would be the latest code, also if you have a problem, remove the line "currentMillis = millis() +2000;" as it worked for me.If you can share the latest version of the code I can start testing first thing tomorrow morning.
// 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
}
}
}
}
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
Values as below: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?
Unfortunately I don't, if I did I would have already placed the exact values here a long time agoEvian, do you have access to an oscilloscope? if you do, can you check the waveform on A3 when the bike is at rest?
A3: TorqueAtRest shows 3.86V = 198. I updated the code to fix this.Values as below:
A0 - 0.83 (ThrottlePin)
A2 - 0.14 (CadenceInputPin)
A3 - 3.86 (TorqueInputPin)
A5 - 0 (TorqueOutputPin)
// 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
}
}
}
}