-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk11_tacho.ino
240 lines (174 loc) · 5.39 KB
/
k11_tacho.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <SPI.h>
/**
* Micra K11 Tachometer
*
* 500 to 4000 rpm = 7 green leds
* 4500 to 5500 rpm = 3 orange leds
* 6000 to 7000 rpm = 3 red leds
* 7500 to 8000 = unused
*
* shift light = 1 blue led
*/
#define DATA_PIN 3
#define INTERRUPT_PIN 1
#define LED_SHIFT_LIGHT_PIN 2
#define LATCH_PIN 10
#define CLEAR_PIN 8
/*----------------------------------------*/
const byte SHIFT_REGISTERS_COUNT = 2;
const byte LEDS_COUNT = SHIFT_REGISTERS_COUNT * 8;
const int LED_ILLUMINATE_RPM[LEDS_COUNT] = {500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000};
const int REV_LIMITER_RPM = 6500; //ecu limiter 6650rpm
const int SHIFT_LIGHT_RPM = 4000;
const byte NUMBER_OF_CYLINDERS = 4;
const byte LED_UPDATE_INTERVAL = 80;
/**
* The Micra has a 4 stroke engine firing twice every rev.
* This snippet, with thanks to https://github.com/deepsyx/arduino-tachometer
* calculates the number of fires per revolution for any number of cylinders.
*/
const int FIRES_PER_REV = (360 / (720 / NUMBER_OF_CYLINDERS));
SPISettings tpic6c595(10000000UL, MSBFIRST, SPI_MODE0);
/*----------------------------------------*/
unsigned long lastUpdateTime = 0;
int sparkCount = 0;
int lastRpmValue = 0;
bool revLimiterBlink = false;
int ledPattern = 0b0000000000000000;
/*----------------------------------------*/
void setLedState(int rpm, bool limit = true, bool shift = true);
/**
* Interrupt increment spark count.
*/
void incrementSparkCount() {
sparkCount++;
}
void transferLedPattern() {
SPI.beginTransaction(tpic6c595);
digitalWrite(LATCH_PIN, LOW);
SPI.transfer(highByte(ledPattern));
SPI.transfer(lowByte(ledPattern));
digitalWrite(LATCH_PIN, HIGH);
SPI.endTransaction();
Serial.print("LED pattern: ");
Serial.println(String(ledPattern, BIN));
}
/**
* Switch all leds on or off.
*/
void setGlobalState(bool state) {
setShiftLightState(state);
for (int i = 0; i < LEDS_COUNT; i++) {
bitWrite(ledPattern, i, state);
}
Serial.print("\n");
Serial.print("Global state: ");
Serial.println(state);
transferLedPattern();
}
/**
* Switch on or off the shift light.
*/
void setShiftLightState(bool state) {
digitalWrite(LED_SHIFT_LIGHT_PIN, state);
}
/**
* Cycle the led state.
*/
void cycleLedState() {
Serial.print("\n");
Serial.println("Ramping up...");
Serial.print("\n");
for (int i = 0; i < LEDS_COUNT; i++) {
setLedState(LED_ILLUMINATE_RPM[i], false, false);
delay(LED_UPDATE_INTERVAL);
}
Serial.print("\n");
Serial.println("Hitting limiter...");
for (int i = 0; i <= LEDS_COUNT; i++) {
setLedState(REV_LIMITER_RPM, true, false);
delay(LED_UPDATE_INTERVAL);
}
Serial.print("\n");
Serial.println("Ramping down...");
Serial.print("\n");
for (int i = LEDS_COUNT - 1; i >= 0; i--) {
setLedState(LED_ILLUMINATE_RPM[i], false, false);
delay(LED_UPDATE_INTERVAL);
}
setGlobalState(LOW);
Serial.print("\n-----------\n\n");
}
/**
* Illuminate leds based on RPM.
*/
void setLedState(int rpm, bool limit, bool shift) {
//blink when rev limiter hit
if (limit && rpm >= REV_LIMITER_RPM) {
setGlobalState(revLimiterBlink);
revLimiterBlink = !revLimiterBlink;
return;
}
//illuminate shift light at set rpm
setShiftLightState(shift && rpm >= SHIFT_LIGHT_RPM);
//set led pattern based on rpm
for (int i = 0; i < LEDS_COUNT; i++) {
if (rpm >= LED_ILLUMINATE_RPM[i]) {
bitWrite(ledPattern, i, 1);
} else {
bitWrite(ledPattern, i, 0);
}
}
transferLedPattern();
}
/**
* Setup and run through startup sequence.
*/
void setup() {
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLEAR_PIN, OUTPUT);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(CLEAR_PIN, LOW);
digitalWrite(CLEAR_PIN, HIGH);
pinMode(LED_SHIFT_LIGHT_PIN, OUTPUT);
//initialise serial
Serial.begin(9600);
//initialise spi
SPI.begin();
//run though startup sequence
setShiftLightState(HIGH);
delay(100);
setShiftLightState(LOW);
delay(100);
setShiftLightState(HIGH);
delay(100);
setShiftLightState(LOW);
delay(200);
cycleLedState();
//ready for data
pinMode(DATA_PIN, INPUT_PULLUP);
attachInterrupt(INTERRUPT_PIN, incrementSparkCount, FALLING);
}
/**
* Update leds at configured interval based on rpm.
*/
void loop() {
if ((millis() - lastUpdateTime) >= LED_UPDATE_INTERVAL) {
//calculate the current rpm by multiplying the number of sparks in one update interval
//by the number of intervals per second - finding the number of sparks in one second
int sparksPerSecond = sparkCount * (1000 / LED_UPDATE_INTERVAL);
//then multiply by 60 to find sparks in one minute
int sparksPerMinute = sparksPerSecond * 60;
//finally, divide by the number of fires per revolution to find the current rpm.
int currentRpm = sparksPerMinute / FIRES_PER_REV;
//find an average between the current and last rpm to provide a little smoothing
int smoothedRpm = (currentRpm + lastRpmValue) / 2;
setLedState(smoothedRpm);
Serial.print("Smoothed RPM: ");
Serial.println(smoothedRpm);
Serial.println("\n");
lastRpmValue = currentRpm;
sparkCount = 0;
lastUpdateTime = millis();
}
}