-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathshiftlight_with_CAN-BUS_for_MQB.ino
254 lines (209 loc) · 6.45 KB
/
shiftlight_with_CAN-BUS_for_MQB.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* Shiftlight with CAN-BUS for MQB */
/* Board: Arduino Nano
* Processor: ATmega328P (Old Bootloader)
* Port: /dev/cu.usbserial-1410
*/
#include <Adafruit_NeoPixel.h>
#include <mcp_can.h>
#include <SPI.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
//pin config
#define PINPIXELS 3 // pin D3 to data-in WS2812 module
#define NUMPIXELS 8 // number of LEDs in WS2812 module
#define PINBUZZER 4 // pin D2 to pizo speaker
#define GNDBUZZER 5 // ground for pizo speaker
#define CAN0_INT 2 // MCP2515 INT to pin 2
//MCP2515 config
MCP_CAN CAN0(10); // MCP2515 CS to pin 10
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];
//initial config
Adafruit_NeoPixel pixels(NUMPIXELS, PINPIXELS, NEO_GRB + NEO_KHZ800);
unsigned int initialBrightness = 15; // 0-255
unsigned int currentBrightness = initialBrightness;
unsigned int currentFrequency = 500;
//user config
unsigned int currentRPM;
unsigned int maxRPM = 8000;
unsigned int perfectRPM = 6500;
unsigned int welcomeMode = 2;
bool mute = true;
unsigned long nextLoopTime=0;
//run once
void setup() {
//Serial communication setup
Serial.begin(115200);
//First data output over serial
printInitialDetails();
///MCP2515 setup
mcpSetup();
//WS2812 setup
pixels.begin();
pixels.clear();
pixels.show();
pixels.setBrightness(currentBrightness);
//Buzzer setup
pinMode(PINBUZZER, OUTPUT);
pinMode(GNDBUZZER, OUTPUT);
digitalWrite(GNDBUZZER, LOW);
//Run initial animation
welcomeCeremony(welcomeMode);
}
//run all the time
void loop() {
mcpRead();
//mcpSearch();
currentRPM = getRPM(false); //false = simulation
pixels.clear();
pixels.setBrightness(currentBrightness);
updateAlerts();
pixels.show();
}
void welcomeCeremony(int type) {
if(type==1){
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
pixels.show();
tone(PINBUZZER, (currentFrequency*(i+1)));
delay(25);
noTone(PINBUZZER);
delay(25);
}
}
if(type==2){
for (int i = 0; i < NUMPIXELS; i++) {
pixels.clear();
pixels.setPixelColor(i, pixels.Color(0, 0, 255));
pixels.show();
tone(PINBUZZER, (currentFrequency*(i+1)));
delay(25);
}
for (int i=7; i>=0; i--) {
pixels.clear();
pixels.setPixelColor(i, pixels.Color(0, 0, 255));
pixels.show();
tone(PINBUZZER, (currentFrequency*(i+1)));
delay(25);
}
noTone(PINBUZZER);
}
}
int getRPM(bool real){
if(real){
//read RPM from CAN-BUS
//TODO
}else{
//simulate RPMs for development and debug
return millis()%maxRPM;
}
}
void updateAlerts(){
if(currentRPM<(perfectRPM-1000)){
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
}
if(currentRPM>=(perfectRPM-1000) && currentRPM<(perfectRPM-800)){
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
pixels.setPixelColor(7, pixels.Color(0, 255, 0));
}
if(currentRPM>=(perfectRPM-800) && currentRPM<(perfectRPM-600)){
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
pixels.setPixelColor(1, pixels.Color(0, 255, 0));
pixels.setPixelColor(6, pixels.Color(0, 255, 0));
pixels.setPixelColor(7, pixels.Color(0, 255, 0));
}
if(currentRPM>=(perfectRPM-600) && currentRPM<(perfectRPM-400)){
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
pixels.setPixelColor(1, pixels.Color(0, 255, 0));
pixels.setPixelColor(2, pixels.Color(0, 255, 0));
pixels.setPixelColor(5, pixels.Color(0, 255, 0));
pixels.setPixelColor(6, pixels.Color(0, 255, 0));
pixels.setPixelColor(7, pixels.Color(0, 255, 0));
}
if(currentRPM>=(perfectRPM-400) && currentRPM<(perfectRPM-200)){
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 255, 0));
}
}
if(currentRPM>=(perfectRPM-200) && currentRPM<(perfectRPM+200)){
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 255));
}
}
if(currentRPM>=(perfectRPM+200) && currentRPM<(perfectRPM+500)){
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
}
}
if(currentRPM>=(perfectRPM+500)){
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
}
pixels.show();
if(!mute){
tone(PINBUZZER, currentFrequency*8);
}
delay(50);
pixels.clear();
pixels.show();
noTone(PINBUZZER);
delay(50);
}
}
void mcpSetup(){
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK){
Serial.println("MCP2515 Initialized Successfully!");
}else{
Serial.println("Error Initializing MCP2515...");
}
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
printSpacer();
}
void mcpRead(){
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for(byte i = 0; i<len; i++){
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
}
}
void mcpSearch(){
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
//getting RPM from CAN-BUS. Need to requrest it first. Most likely with "714 03 22 F4 0C 55 55 55 55".
if(rxId == 0x77E){
sprintf(msgString, "Found ID: 0x%.3lX DLC: %1d Data: ", rxId, len);
Serial.print(msgString);
Serial.println();
}
}
}
void printInitialDetails(){
Serial.println("");
Serial.println("Welcome to MQB shiftlight");
Serial.println("Software version: 0.0.2");
printSpacer();
}
void printSpacer(){
Serial.println("================");
}