-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHot_Wheels_Speedometer.ino
66 lines (57 loc) · 1.2 KB
/
Hot_Wheels_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
/*
* Arduino Hot Wheels Speedometer
*
* Ronaldo Rezende Junior
* ronaldo.rrj at gmail
* 02/2019
* https://github.com/naldin
*
* For math was used:
* 100mm = 0.1m (or 100/1000)
* 1ms = 0.001sec (or 1/1000)
* 1m/s = 3.6km/h
* 1km/h = 0.621mi/h
*
*/
#include <LiquidCrystal.h>
// Using for setting LCD
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const float tHold = 1000; //threshold
float ldr1 = 0; // LDR 1
float ldr2 = 0; // LDR 2
float t1 = 0; // Time 1
float t2 = 0; // Time 2
float res = 0; // Result
void setup() {
// Starting LCD
lcd.begin(16, 2);
lcd.clear();
// Label
lcd.setCursor(0, 0);
lcd.print("Speed km/h:");
lcd.setCursor(0, 1);
lcd.print("Speed mi/h:");
// Pins setup
pinMode(A0, INPUT);
pinMode(A1, INPUT);
}
void loop() {
//Reading LDRs
ldr1 = analogRead(A0);
ldr2 = analogRead(A1);
// Setting time 1
if (ldr1 > tHold) {
t1 = millis();
}
//Setting time 2
if (ldr2 > tHold) {
t2 = millis();
// Doing the math and print
res = (100 / (t2 - t1)) * 3.6;
lcd.setCursor(11, 0);
lcd.print(res); // km/h
lcd.setCursor(11, 1);
lcd.print(res * 0.621); // mi/h
}
}