#define rearencoderA 0
#define pedalencoderA 2
unsigned long duration;
unsigned long duration1;
unsigned long freq;
unsigned long freq1;
unsigned long rpm;
unsigned long rpm1;
unsigned long U;
#define handencoderA 4
#define handencoderB 5
#define forkencoderA 6
#define forkencoderB 7
int counter = 0;
int aState;
int aLastState;
void setup() {
pinMode (rearencoderA,INPUT);
pinMode (pedalencoderA,INPUT);
pinMode (handencoderA,INPUT);
pinMode (handencoderB,INPUT);
Serial.begin (9600);
aLastState = digitalRead(handencoderA);
}
void loop() {
// This program calculates the bicycle forward velocity in km/h
duration = pulseIn(rearencoderA, HIGH,250000); // Returns the length of the pulse in microseconds when signal output A is high
freq=1200000/(duration2); // The period of the is 2 times the duration of the pulse of output A https://tushev.org/articles/arduino/9/measuring-frequency-with-arduino
rpm = (freq60)/96; // 96 are the line counts of the encoder http://www.quantumdev.com/finding-the-rpm-of-an-optical-encoder-using-an-oscilloscope/
U=6.280.3355rpm60/1000; // http://answers.tutorvista.com/489133/what-is-the-formula-for-converting-rpm-to-kph.html#
// This program calculates the pedal cadence speed in rpm
duration1 = pulseIn(pedalencoderA, HIGH,250000); // Returns the length of the pulse in microseconds when signal output A is high
freq1=1200000/(duration12); // The period is 2 times the duration of the pulse of output A. 12e6 is set based on speedometer output comparison https://tushev.org/articles/arduino/9/measuring-frequency-with-arduino
rpm1 = (freq160)/96; // 96 are the line counts of the encoder http://www.quantumdev.com/finding-the-rpm-of-an-optical-encoder-using-an-oscilloscope/
{
aState = digitalRead(handencoderA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
{
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(handencoderB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("velocity:");
Serial.print(U);
Serial.print("\t");
Serial.print("rpm:");
Serial.print(rpm1);
Serial.print("\t");
Serial.print("deg: ");
Serial.print(counter360/147454);
Serial.print("\t");
Serial.println();
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}
}
I am using the above code to print the values of my sensors but unfortunately I get no output of the handlebar encoder counts. However I get an output whenever I use the code which gives me the counter values independently. I guess there is something going on with the pulse function.
Print_sensors.ino (2.45 KB)