one odd thing i didnt' expect... last time i 'scoped a bike speedo, it was 3 pulses per output shaft revolution, which in my head i was thinking was a pretty low polling rate required.
this one is apparently 27. no idea why they'd make it so high resolution, after the sprockets, it gives me 83 pulses per wheel revolution.
since my tire is about 1 meter in circumfrence, and my top speed is 290kph = approx 80 meters per second (not that i'd ever go that fast....), that means at top speed i'd be in for about 6600hz of maximum sample rate.
arduino docs say:
It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.
i have to ensure my code polls it quickly enough to not miss a bunch of clock cycles at higher speeds... things like EEPROM WRITE clock in at 3ms each, so i'm going to ensure the odo/trip values don't get stored unless at low speed.
i tried my best to make it work on a digital pin, but the hall effect sensor signal is too unreliable, and the ECM runs in parallel and supplies its own pull up and 5v (actually more like 4.5v) reference which doesn't quite match the arduino... with the analog input i pretty much have an oscilloscope at my disposal, i just have to translate the analog state to binary with a large switching threshold to reject any noise near the switch point.
Code:
// the speed sensor's voltage switching threshold, 0=0v 1023=5v
#define SPEED_SWITCH_HIGH 700
#define SPEED_SWITCH_LOW 300
Code:
int speed_input_state() {
static int previous_state = LOW;
int v_in = analogRead(PIN_SPEED);
if(previous_state == LOW && v_in > SPEED_SWITCH_HIGH) { // switched to HIGH state
previous_state = HIGH;
return HIGH;
}
if(previous_state == HIGH && v_in < SPEED_SWITCH_LOW) { // switched to LOW state
previous_state = LOW;
return LOW;
}
// state unchanged
return previous_state;
}
then the accumulator only counts rising edge:
Code:
void process_speedometer() {
static int speed_state_last = LOW;
int speed_state_current = speed_input_state();
if(speed_state_last == LOW && speed_state_current == HIGH) { // rising clock edge
speed_switch_count++;
...
since this code relies on detection of the falling edge even though it's not detected, it might start acting up at high speed. might need to think of a better way.
Bookmarks