-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteriorLeds.ino
187 lines (165 loc) · 4.73 KB
/
InteriorLeds.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
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
/*
___. ________ .__ .________ _______________ _______ _______ ___ ___
\_ |__ ___.__. \_____ \_______ ____ | | | ____// _____/\ _ \ \ _ \ \ _ \ / / \ \ /\
| __ < | | / | \_ __ \_/ __ \| | |____ \/ __ \ / /_\ \/ /_\ \/ /_\ \ / / \ \ \/
| \_\ \___ | / | \ | \/\ ___/| |__/ \ |__\ \\ \_/ \ \_/ \ \_/ \ ( ( ) ) /\
|___ / ____| \_______ /__| \___ >____/______ /\_____ / \_____ /\_____ /\_____ / \ \ / / )/
\/\/ \/ \/ \/ \/ \/ \/ \/ \__\ /__/
*/
#include <FastLED.h>
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>
/* CONFIG */
#define LED_PIN 7
#define BUTTON_PIN 2
#define NUM_LEDS 40 // Number of leds
#define TIMEZONE 2 // Your country timezone, beacuse the GPS gets the global time
#define KMPH // comment this line if you use mp/h
#define SPEEDMULTIPLY 1.6 // This is the top speed which will display red leds, 1.6 means 100kmp/h or 100mp/h, by this calculation: 160 / (KMP/H or MP/H to top speed) = This value (Speed measurment is depends on the previous define)
#define AFTERNOON 15 // "Night time" - The time when the leds will start display the interior leds (Don't forget to set the timezone value)
#define MORNING 6 // "Morning time" - The time when the leds won't work until the night time (Don't forget to set the timezone value & Remember that it's still possible to be activated with a button click)
//#define DEBUG // debug mode
static const int RXPin = 3, TXPin = 4;
static const uint32_t GPSBaud = 9600;
/* CONFIG END */
CRGB leds[NUM_LEDS];
// The TinyGPSPlus object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
void SetLedColors(int h);
bool forceDisable = false;
void setup() {
#if defined(DEBUG)
Serial.begin(115200);
#endif
pinMode(BUTTON_PIN, INPUT_PULLUP);
ss.begin(GPSBaud);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
forceDisable = EEPROM.read(0) == 1;
#if defined(DEBUG)
Serial.print("Reading EEPROM, forceDisable: ");
Serial.println(forceDisable);
#endif
}
int lastColor = 160;
int hour = -1;
bool effectStarted;
void StartEffect()
{
effectStarted = true;
for(int i = 255; i > 160; i--)
{
SetLedColors(i);
delay(50);
}
delay(5000);
}
void loop() {
// Dispatch incoming characters
while (ss.available() > 0)
gps.encode(ss.read());
while (!effectStarted)
{
if(digitalRead(BUTTON_PIN) == LOW) // Button press when morning time or gps didn't get the time yet
{
#if defined(DEBUG)
Serial.println("Button pressed");
#endif
EEPROM.update(0, 0);
StartEffect();
continue;
}
while (ss.available() > 0)
gps.encode(ss.read());
if(gps.time.isUpdated())
{
int second = gps.time.second();
if (second == 0)
continue;
hour = gps.time.hour() + TIMEZONE;
#if defined(DEBUG)
Serial.print("Hour: ");
Serial.println(hour);
Serial.println("Second: ");
Serial.println(second);
#endif
if(hour > AFTERNOON || hour < MORNING)
{
#if defined(DEBUG)
Serial.println("Night");
#endif
StartEffect();
}
else
{
delay(3000);
}
}
}
if(digitalRead(BUTTON_PIN) == LOW) // Button press after getting the time
{
forceDisable = !forceDisable;
if(forceDisable)
{
// Turn off all LEDs
#if defined(DEBUG)
Serial.println("Turning off all LEDs");
#endif
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
}
else
{
// Turn on all LEDs
#if defined(DEBUG)
Serial.println("Turning on");
#endif
StartEffect();
}
EEPROM.write(0, (forceDisable ? 1 : 0));
delay(1500);
}
if (!forceDisable && gps.speed.isUpdated()) // When gps updates the speed, we update the color
{
#if defined(KMPH)
double speed = gps.speed.kmph();
#else
double speed = gps.speed.mph();
#endif
SetColorsToSpeed(speed);
}
}
void SetColorsToSpeed(float speed)
{
float color = 160 - (speed * SPEEDMULTIPLY);
if (color < 1) color = 1;
int h = (int)color;
if (h > lastColor) // Slowing down
{
for(; lastColor < h; lastColor++)
{
SetLedColors(lastColor);
delay(10);
}
}
else if (h < lastColor) // Speeding up
{
for(; lastColor > h; lastColor--)
{
SetLedColors(lastColor);
delay(10);
}
}
lastColor = h;
}
void SetLedColors(int h)
{
if(forceDisable) return;
fill_solid(leds, NUM_LEDS, CHSV(h, 255, 255));
FastLED.show();
FastLED.setCorrection(TypicalLEDStrip);
}