// This speedometer shows up to 999.99 km/hr
// This speedometer does not **KEEP** distance
// This speedometer uses one magnet per revolution
// Wiring for speedometer is in Wiring.bmp
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 4, 5, 6, 7, 8);
float time = 1000000;
unsigned long present = 0;
unsigned long present2 = 0;
unsigned long present3 = 0;
unsigned long present4 = 0;
unsigned long past = 0;
unsigned long past2 = 0;
unsigned long past3 = 0;
unsigned long past4 = 0;
unsigned long past5 = 0;
unsigned int interval = 1000; // time in milliseconds between LCD refreshes
unsigned long ridetime = 0; // total ride time in ms (will not count first 10 wheel rotations to get stable data)
unsigned long ridetime2 = 0; // total ride time in seconds (will not count first 10 wheel rotations to get stable data)
float wheelCircum = 2194; // wheel circumference in millimeters
float kmph = 0;
float kmphShown = 0;
float kmphmax = 0; // maximum speed
float mph = 0;
float mphshown = 0;
float mphmax = 0;
float km = 0;
float mi = 0;
float intermid = 0;
float intermid2 = 0;
float intermid3 = 0;
float intermid4 = 0;
float intermid5 = 0;
unsigned long count = 0;
unsigned long count2 = 0;
int page = 0;
uint8_t state = LOW;
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(2, INPUT_PULLUP); // Magnet Sensor
pinMode(A1, INPUT_PULLUP); // page flip button
pinMode(A3, INPUT_PULLUP); // Honk the horn
pinMode(A4, INPUT_PULLUP); // Brake lights switch (normally low)
// Pin A2 is for turn signals where 400-600 is off, less than 400 is left and more than 600 is right
delay(250);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Bicycle Controls");
lcd.setCursor(0, 1);
lcd.print("With Tripmeter ");
delay(2000);
lcd.setCursor(0, 1);
lcd.print("With Speedometer");
delay(2000);
lcd.setCursor(0, 1);
lcd.print("Starting up... ");
attachInterrupt(digitalPinToInterrupt(2), zeroing, FALLING);
delay(2000);
}
void zeroing () {
count = count + 1;
time = present2 - past2;
past2 = present2;
count2 = 0;
ridetime = ridetime + time;
}
void loop() {
ridetime2 = ridetime / 1000;
if (count <= 9) {
ridetime = 0;
}
if (digitalRead(A1) == LOW) {
delay(250);
if (page >= 8) {
page = 0;
}
else {
page = page + 1;
}
}
mph = kmphShown / 1.60;
mphmax = kmphmax / 1.60;
mi = km / 1.60;
present = millis();
present2 = millis();
present3 = millis();
present4 = millis();
intermid5 = wheelCircum * count;
km = intermid5 / 1000000;
intermid = 1000 / time;
intermid2 = intermid * wheelCircum;
intermid3 = intermid2 * 3600;
intermid4 = intermid3 / 1000000;
kmph = intermid4;
if (kmph >= 999.99) {
kmph = 999.99;
}
if (time >= 4001) {
kmph = 0.00;
}
if (present <= 6000) {
lcd.clear();
}
if (kmphmax <= kmph) {
kmphmax = kmph;
}
if (mphmax <= mph) {
mphmax = mph;
}
if (present4 - past5 >= 500) {
switch (page) {
case 0:
lcd.clear();
lcd.print(" Current speed: ");
lcd.setCursor(0, 1);
lcd.print(kmphShown);
lcd.setCursor(6, 1);
lcd.print(" km/hr ");
break;
case 1:
lcd.clear();
lcd.print(" Maximum speed: ");
lcd.setCursor(0, 1);
lcd.print(kmphmax);
lcd.setCursor(6, 1);
lcd.print(" km/hr ");
break;
case 2:
lcd.clear();
lcd.print("Travel Distance:");
lcd.setCursor(0, 1);
lcd.print(km);
lcd.setCursor(6, 1);
lcd.print(" Kilomets ");
break;
case 3:
lcd.clear();
lcd.print(" Ride time: ");
lcd.setCursor(0, 1);
lcd.print(ridetime2);
lcd.setCursor(6, 1);
lcd.print(" Seconds ");
break;
case 4:
lcd.clear();
lcd.print(" Current speed: ");
lcd.setCursor(0, 1);
lcd.print(mph);
lcd.setCursor(6, 1);
lcd.print(" mi/hr ");
break;
case 5:
lcd.clear();
lcd.print(" Maximum speed: ");
lcd.setCursor(0, 1);
lcd.print(mphmax);
lcd.setCursor(6, 1);
lcd.print(" mi/hr ");
break;
case 6:
lcd.clear();
lcd.print("Travel Distance:");
lcd.setCursor(0, 1);
lcd.print(mi);
lcd.setCursor(6, 1);
lcd.print(" Miles ");
break;
case 7:
lcd.clear();
lcd.print(" Ride time: ");
lcd.setCursor(0, 1);
lcd.print(ridetime2);
lcd.setCursor(6, 1);
lcd.print(" Seconds ");
break;
}
past5 = present4;
}
if (present3 - past3 >= interval) {
if (analogRead(A2) <= 400) {
smerovkaLava();
}
if (analogRead(A2) >= 600) {
smerovkaPrava();
}
if (analogRead(A2) >= 400 && analogRead(A2) <= 600) {
smerovkaZiadna();
}
if (digitalRead(A3) == LOW) {
tone(10, 175);
}
if (digitalRead(A3) == HIGH) {
noTone(10);
}
if (digitalRead(A4) == LOW) {
digitalWrite(9, HIGH);
}
if (digitalRead(A4) == HIGH) {
digitalWrite(9, LOW);
}
past3 = present3;
}
if (present4 - past4 >= 20) {
count2 = count2 + 1;
past4 = present4;
}
if (count2 <= 199) {
kmphShown = kmph;
}
if (count2 >= 200) {
kmphShown = 0;
}
}
void smerovkaZiadna() {
digitalWrite(11, LOW);
digitalWrite(12, LOW);
}
void smerovkaLava() {
if (present - past2 >= 300) {
if (state == LOW) {
state = HIGH;
} else {
state = LOW;
}
past2 = present;
}
digitalWrite(11, state);
}
void smerovkaPrava() {
if (present - past2 >= 300) {
if (state == LOW) {
state = HIGH;
} else {
state = LOW;
}
past2 = present;
}
digitalWrite(12, state);
}
/*
lcd.clear();
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print();
lcd.setCursor(6, 1);
lcd.print(" ");
*/