Speedometer code keeps track of tire revolutions using a hall effect sensor. Code assumes 26 inch tires but this can be tweaked to your tire size by changing the variable:
float tire_diameter = 26.2; // put in your tire dimaeter in inches
The code that does the heavy lifting is the function:
void updateSpeedAndDistance(){
distance = tire_circumference * revolutions;
distance /= miles_in_inches;
float ipm = tire_circumference / (float)last_interval;
mph = ipm * k_ipm_2_mph;
}
This code translates from the number of tire resolutions to the speed you are going and the distance you have travelled. We know the tire revolutions by checking the hall effect sensor as we do in the following code found in the loop function:
int hall_val = digitalRead(hall_pin);
if (hall_state != hall_val && hall_val == LOW) {
revolutions++;
last_interval = millis()-last_fall;
last_fall = millis();
}
hall_state = hall_val;
So we check to see if the state of the hall pin has changed, and if it has changed to low so we are on the falling edge of the clock. Then we know the tire has made a complete revolution so we increment the revolutions variable. Then we calculate how long this revolution took and we will use that number to derive our speed.
/* LucidTronix bike Speedometer
* For instructions, details and schematic, See:
* http://www.lucidtronix.com/tutorials/13
* Hall effect sensor on pin 16,
* doesn't use hardware interrupt.
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 0, 1, 2, 3);
String message = "Luf";
int mode = 1;
int num_modes = 2;
int cur_scroll = 0;
int cur_msg_length = 0;
int cursor_index = 0;
int char_index = 0;
int btn = 3;
int hall_pin = 16;
byte backlight_pwm = 200;
int hall_state = 1;
int revolutions = 0;
float miles_in_inches = 63360;
float mph = 0.0;
float distance = 0.0;
float tire_diameter = 26.2;
float k_ipm_2_mph;
int tire_circumference;
unsigned long last_fall = 0;
unsigned long last_interval = 0;
int backlight_pin = 9;
int cur_x = 8;
int cur_y = 1;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
delay(500);
k_ipm_2_mph = 3600000 / miles_in_inches;
tire_circumference = tire_diameter*3.14159;
pinMode(btn, INPUT);
pinMode(hall_pin, INPUT);
pinMode(backlight_pin, OUTPUT);
analogWrite(backlight_pin , backlight_pwm);
lcd.blink();
lcd.print("hello, world!");
delay(1000);
}
void loop() {
int hall_val = digitalRead(hall_pin);
if (hall_state != hall_val && hall_val == LOW) {
revolutions++;
last_interval = millis()-last_fall;
last_fall = millis();
}
hall_state = hall_val;
updateSpeedAndDistance();
lcd.setCursor(0, 0);
lcd.print("Hall:" );
lcd.print( hall_state);
lcd.print("Mph:");
lcd.print(mph);
lcd.setCursor(0, 1);
lcd.print("Miles:");
lcd.print(distance);
backlight_pwm = analogRead(0)/4;
analogWrite(backlight_pin , backlight_pwm);
}
void updateSpeedAndDistance(){
distance = tire_circumference * revolutions;
distance /= miles_in_inches;
float ipm = tire_circumference / (float)last_interval;
mph = ipm * k_ipm_2_mph;
}
Comments: