This repository has been archived by the owner on Jun 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotorduino.ino
111 lines (84 loc) · 2.36 KB
/
motorduino.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
//Arduino references https://www.arduino.cc/en/Reference/HomePage
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12,11,9,8,7,6,5,4,3,2);
const int mphPot = A0;
const int lBlinker = 13;
const int rBlinker = 10;
const long interval1k = 250;
float gearRatio = 0.015455;
int mph = 0;
int rpm = 0;
int mphPotValue = 0;
int blinkerLastVal = 0;
int lBlinkerState = 0;
int rBlinkerState = 0;
int currentBlinker = 0;
int interval = 0;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
void setup() {
pinMode(mphPot,INPUT);
pinMode(lBlinker,INPUT);
pinMode(rBlinker,INPUT);
// set up the LCD's number of columns and rows: (123d.circuits.io only?)
lcd.begin(16,2);
lcd.clear();
//Place MPH and RPM on the screen.
// lcd.setCursor(10,0);
// lcd.print("mph");
// lcd.setCursor(10,1);
// lcd.print("rpm");
}
void loop() {
//MPH Logic
mphPotValue = analogRead(mphPot);
mph = map(mphPotValue,0,1023,0,85);
lcd.setCursor(8,0);
lcd.print(mph);
lcd.print(" mph");
//RPM Logic
lcd.setCursor(6,1);
rpm = mph / gearRatio;
lcd.print(rpm);
lcd.print(" rpm");
//Blinkers
lBlinkerState = digitalRead(lBlinker);
rBlinkerState = digitalRead(rBlinker);
if (lBlinkerState == HIGH || rBlinkerState == HIGH){
if (lBlinkerState == HIGH) {currentBlinker = 1;}
else if (rBlinkerState == HIGH) {currentBlinker = 2;}
} else {currentBlinker = 0;}
switch (currentBlinker){
case 1:
currentMillis = millis();
if (currentMillis - previousMillis >= interval1k) {
// save the last time the blinker blinked
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
lcd.setCursor(0,1);
lcd.print("<");
} else {
lcd.setCursor(0,1);
lcd.print(" ");
}
break;
case 2:
currentMillis = millis();
if (currentMillis - previousMillis >= interval1k) {
// save the last time the blinker blinked
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
lcd.setCursor(15,1);
lcd.print(">");
} else {
lcd.setCursor(15,1);
lcd.print(" ");
}
break;
default:
currentBlinker = 0;
break;
}
}