/* Source: ALucidTronix bike Speedometer * http://www.lucidtronix.com/tutorials/13 */ #include LiquidCrystal lcd(13,12,11,10,9,8); //switch out pins for my board (prev. 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 = 2; //change to 2 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 wheel_diameter = 35.98; //wheel diameter in inches float k_ipm_2_mph; int wheel_circumference; unsigned long last_fall = 0; unsigned long last_interval = 0; int backlight_pin = 5; //switch from pin 9 to 5 int cur_x = 3; //switch from pin 8 to 3 int cur_y = 10; //switch pin 1 to 10 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; wheel_circumference = wheel_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); //revolutions from hall effect sensor if (hall_state != hall_val && hall_val == LOW) { revolutions++; last_interval = millis()-last_fall; last_fall = millis(); } hall_state = hall_val; updateSpeedAndDistance(); lcd.setCursor(13, 0); //changed from 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(){ //converts wheel revolutions to speed and distance distance = wheel_circumference * revolutions; distance /= miles_in_inches; float ipm = wheel_circumference / (float)last_interval; mph = ipm * k_ipm_2_mph; }