This repository was archived by the owner on Jan 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBike_Computer-diag.ino
More file actions
194 lines (150 loc) · 5.52 KB
/
Copy pathBike_Computer-diag.ino
File metadata and controls
194 lines (150 loc) · 5.52 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Bike Computer V1.1 - with diagnostics
Using the serial functions signifigantly increases compiled size, use this if you have a problem in execution or are testing.
Use the version without diagnostics in production (once you work out the bugs of course)
Provides controls to basic functions on a bike, such as turning on and off lights,
Measuring distance traveled, average speed, and logs to a microSD card.
Future versions will also be able to communicate to other
microcontrollers through the serial interface.
Version 1 (This version) will be responsible for detecting ambiant light and ramping
up/down the front and rear LED brightness once light levels reach a certain level.
Other responsibilities will be blinking pairs of LEDs (front and back) on either side
of the bike to indicate turning.
*/
#include <SoftwareSerial.h>
SoftwareSerial lcd(2, 8); // This is required, to start an instance of an LCD
const int dispSw = A3;
// D4 and D5 are reserved for I2C
// Headlight
const int ambientIn = A0; // Analog input pin that the potentiometer is attached to
const int headlight = 3; // Digital output pin that the LED is attached to
const int incrBy = 5; // Amount to increase or decrease LED brightness by
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
// Blinkers
const int lBnkSw = A1; // Left Blinker switch
const int rBnkSw = A2; // Right Blinker switch
const int lBnk = 6; // Left Blinker
const int rBnk = 7; // Right Blinker
const int bnkSpeed = 500; // in ms, 500 = twice a second
int lBnkSt = LOW;
int rBnkSt = LOW;
int lBnkSwSt = 0;
int rBnkSwSt = 0;
unsigned long previousMillis = 0;
void setup() {
lcd.begin(9600); // Start the LCD at 9600 baud
clearDisplay(); // Clear the display
setBacklight(20);
// initialize serial communications at 9600 bps:
Serial.begin(9600);
Serial.println("Loading bike computer diagnostics.");
Serial.println("This computer will log output to the console in the format: key = value\t key = value");
// Set out outputs
Serial.println("Setting our output pins");
pinMode(headlight, OUTPUT);
pinMode(lBnk, OUTPUT);
pinMode(rBnk, OUTPUT);
// Set our inputs
Serial.println("Setting our input pins");
pinMode(ambientIn, INPUT);
pinMode(lBnkSw, INPUT);
pinMode(rBnkSw, INPUT);
pinMode(dispSw, INPUT);
setLCDCursor(0);
lcd.print("c0defox.es BCPU");
setLCDCursor(18);
lcd.print("Bike Computer");
Serial.println("Let the games begin!");
}
void echo(String key, String value){ // This function logs all information to the console and saves on space without having to repeat itself
Serial.print(key);
Serial.print(" = ");
Serial.print(value);
Serial.print("\t");
}
void headLight(){ // Main function for determining whether or not to ramp up or ramp down the headlight and taillight. Uses ambient light sensor for that
sensorValue = analogRead(ambientIn);
if(sensorValue <= 45 && outputValue <= 250){
outputValue = outputValue + incrBy;
}
if(sensorValue >= 50 && outputValue > 0){
outputValue = outputValue - incrBy;
}
analogWrite(headlight, outputValue);
if(outputValue >= 20){ // Ramp up/down lcd brightness with headlight - anything lower than 20 is too hard to read
setBacklight(outputValue);
setLCDCursor(16);
lcd.print("*"); // Headlight indicator on lcd
lcd.print(outputValue); // Brightness level of headlight
}
echo("Ambient", String(sensorValue));
echo("Headlight", String(outputValue));
}
void blinker(){ // Controls the blinkers
unsigned long currentMillis = millis();
// Get info from switches
lBnkSwSt = digitalRead(lBnkSw);
rBnkSwSt = digitalRead(rBnkSw);
// Automatically turn off LEDs if switch is off
if(lBnkSwSt == LOW){ digitalWrite(lBnk, LOW); }
if(rBnkSwSt == LOW){ digitalWrite(rBnk, LOW); }
if(lBnkSwSt == HIGH){ // Test if left blinker switch is on
echo("lBlinkSw", "ON");
if(currentMillis - previousMillis >= bnkSpeed){
previousMillis = currentMillis;
if(lBnkSt == LOW){
lBnkSt = HIGH;
setLCDCursor(0);
lcd.print("<");
echo("lBlinker", "ON");
}else{
lBnkSt = LOW;
setLCDCursor(0);
lcd.print(" ");
echo("lBlinker", "OFF");
}
digitalWrite(lBnk, lBnkSt); // Begin blinking
}
}
if(rBnkSwSt == HIGH){ // Test if right blinker is on
echo("rBlinkSw", "ON");
if(currentMillis - previousMillis >= bnkSpeed){
previousMillis = currentMillis;
if(rBnkSt == LOW){
rBnkSt = HIGH;
setLCDCursor(15);
lcd.print(">");
echo("rBlinker", "ON");
}else{
rBnkSt = LOW;
setLCDCursor(15);
lcd.print(" ");
echo("rBlinker", "OFF");
}
digitalWrite(rBnk, rBnkSt); // Begin blinking
}
}
}
void setBacklight(byte brightness){
lcd.write(0x80); // send the backlight command
lcd.write(brightness); // send the brightness value
}
void clearDisplay(){
lcd.write(0xFE); // send the special command
lcd.write(0x01); // send the clear screen command
}
void setLCDCursor(byte cursor_position){
lcd.write(0xFE); // send the special command
lcd.write(0x80); // send the set cursor command
lcd.write(cursor_position); // send the cursor position
}
void loop(){
delay(500);
clearDisplay();
setLCDCursor(4);
lcd.print("MPH: 7.5");
headLight();
blinker();
// End of secondary functions, append new line to console
Serial.println();
}