-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_light1.ino
164 lines (143 loc) · 4.73 KB
/
test_light1.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
//Shift Light V1.5
#include <Adafruit_NeoPixel.h>
#define PIN 6 //LED Data Pin
#define NUMPIXELS 16 //number of leds connected
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//Calculate the frequency of the tacho signal you are using and adjust the following values accordingly
//example: for a standard tacho signal on a 4 cylinder engine the frequeny is equal to the RPM divided by 30
const unsigned int onFrequency = 20; //startup sequence, value represents engine speed higher than cranking and lower than idle
const unsigned int minFrequency = 123; // minimum frequency to begin turning on LEDs
const unsigned int maxFrequency = 235; // maximum frequency in normal range, after this value shift flash will occur
const unsigned int shiftFrequency = 250; // frequency range from max to shift when shifting should happen, after this value overrev flash will occur
//Set the color for each LED in the neo pixel strip
//Colors are set using an RGB value ranging from (0,0,0) to (255,255,255)
const uint32_t tachColor[NUMPIXELS] = {
Adafruit_NeoPixel::Color(0, 120, 0),//green
Adafruit_NeoPixel::Color(0, 120, 0),
Adafruit_NeoPixel::Color(0, 120, 0),
Adafruit_NeoPixel::Color(15, 105, 0),
Adafruit_NeoPixel::Color(30, 90, 0),
Adafruit_NeoPixel::Color(45, 75, 0),
Adafruit_NeoPixel::Color(60, 60, 0),//orange
Adafruit_NeoPixel::Color(75, 45, 0),
Adafruit_NeoPixel::Color(90, 30, 0),
Adafruit_NeoPixel::Color(105, 15, 0),
Adafruit_NeoPixel::Color(120, 0, 0),//red
Adafruit_NeoPixel::Color(0, 0, 120),//blue
Adafruit_NeoPixel::Color(0, 0, 120),
Adafruit_NeoPixel::Color(0, 0, 120),
Adafruit_NeoPixel::Color(0, 0, 120),
Adafruit_NeoPixel::Color(0, 0, 120),
};
//Set the frequency when each LED should turn on
//First LED turns on at minFrequency
const unsigned int lightShiftFreq[NUMPIXELS] = {
minFrequency,
130,
137,
144,
151,
158,
165,
172,
179,
186,
193,
200,
207,
214,
221,
228,
};
bool hasStartupSequenceRun = false; // only run startup sequence one time
const byte tachPin = 2;
unsigned long igfreq;
void setup() {
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
float ighigh, iglow;
unsigned long igcal1, igcal2;
//measure period of tach signal
ighigh = pulseIn(tachPin, HIGH);
iglow = pulseIn(tachPin, LOW);
igcal1 = 1000 / ((ighigh / 1000) + (iglow / 1000));
//do it again
ighigh = pulseIn(tachPin, HIGH);
iglow = pulseIn(tachPin, LOW);
igcal2 = 1000 / ((ighigh / 1000) + (iglow / 1000));
//to filter out some noise, we only consider our measurement valid if they are similar in value, we accept the average.
if ((igcal1 - igcal2) < 8) {
igfreq = (igcal1 + igcal2) / 2;
}
if (hasStartupSequenceRun == false) {
if (igfreq > onFrequency) {
//run start sequence
//LEDs will light up, flash and light out upon starting the engine.
for ( int i = 0; i < NUMPIXELS; ++i) {
pixels.setPixelColor(i, tachColor[i]);
pixels.show();
delay(50);
}
for(int a=0; a<10; a++) {
pixels.fill(pixels.Color(0, 0, 120));
pixels.show();
delay(20);
pixels.fill(pixels.Color(0, 0, 0));
pixels.show();
delay(20);
}
for ( int i = 0; i < NUMPIXELS; ++i) {
pixels.setPixelColor(i, tachColor[i]);
pixels.show();
}
for ( int i = NUMPIXELS-1; i >= 0; --i) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
delay(50);
}
hasStartupSequenceRun = true;
pixels.fill(pixels.Color(0, 0, 0));
pixels.show();
}
if (igfreq < onFrequency) {
//resets hasStartupSequenceRun to false if engine stops but Arduino remains powered,
//startupsequence will rerun upon restarting the engine
hasStartupSequenceRun = false;
}
}
if (igfreq < maxFrequency) {
// normal operating range
for ( int i = 0; i < NUMPIXELS; ++i) {
if (igfreq > lightShiftFreq[i]) {
pixels.setPixelColor(i, tachColor[i]);
}
else {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
}
pixels.show();
}
else if (igfreq >= maxFrequency && igfreq < shiftFrequency) {
//shift flash
//default color=blue
//to change shift flash color, edit RGB value in following line
pixels.fill(pixels.Color(0, 0, 120));
pixels.show();
delay(20);
pixels.fill(pixels.Color(0, 0, 0));
pixels.show();
delay(20);
}
else if (igfreq >= shiftFrequency) {
//overrev flash
//default color=red
//to change overrev flash color, edit RGB value in following line
pixels.fill(pixels.Color(120, 0, 0));
pixels.show();
delay(20);
pixels.fill(pixels.Color(0, 0, 0));
pixels.show();
delay(20);
}
}