-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspeedometer.ino
111 lines (86 loc) · 2.09 KB
/
speedometer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <LiquidCrystal.h>
// pin connected to reed switch
#define reed A0
int reedVal;
int maxDebounceTicks = 100;
int currentDebounceTicks;
int time = 0;
// the wheel radius, in kilometers
float radius = 0.00034;
float circumference;
float velocity = 0.00;
float distance = 0.00;
// initialize LCD with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows
lcd.begin(8, 2);
Serial.begin(9600);
currentDebounceTicks = maxDebounceTicks;
circumference = 2 * 3.14 * radius;
pinMode(reed, INPUT);
cli();
// set entire TCCR1A register to 0
TCCR1A = 0;
// set entire TCCR1B register to 0
TCCR1B = 0;
// initialize counter value to 0;
TCNT1 = 0;
// set timer count for 1khz increments
// = (16*10^6) / (1000*8) - 1
OCR1A = 1999;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();
}
void loop() {
displayText();
delay(1000);
}
ISR(TIMER1_COMPA_vect) {
reedVal = digitalRead(reed);
if (reedVal) {
// wait the given number of ticks, before calculating the velocity
if (currentDebounceTicks == 0) {
// circumference in kilometers, time in hours
velocity = circumference/(float(time)/1000/3600);
time = 0;
currentDebounceTicks = maxDebounceTicks;
distance = distance + circumference;
} else {
if (currentDebounceTicks > 0) {
currentDebounceTicks -= 1;
}
}
} else {
if (currentDebounceTicks > 0) {
currentDebounceTicks -= 1;
}
}
if (time > 2000) {
// set velocity to 0 when tire is still for 2 seconds
velocity = 0;
} else {
time += 1;
}
}
void displayText(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(int(velocity));
lcd.print(" km/h");
lcd.setCursor(0, 1);
if (distance > 1) {
lcd.print(distance);
lcd.print(" km");
} else {
lcd.print(int(distance * 1000));
lcd.print(" m");
}
Serial.println(velocity);
Serial.println(distance);
}