-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluethooth_RGB_Control_V7_1.ino
264 lines (225 loc) · 7.54 KB
/
Bluethooth_RGB_Control_V7_1.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
255
256
257
258
259
260
261
262
263
264
#include <SoftwareSerial.h>
#include <FastLED.h>
#include <SPI.h>
#include <SD.h>
const int confArr[][3] = {{49}, {3}, {200, 0, 0}, {0, 0, 200}, {0, 200, 0}}; // Configuration array: number of LEDs, number of colors, colors
const int numOfLeds = confArr[0][0]; // Sets the number of LEDs from the configuration array
const int ledSignalPin = 11; // Signal pin for LED matrix
const int chipSelect = 53; // Pin number for the SD card chip select pin
int ledArr[numOfLeds][2]; // Array for storing the LEDs being turned on, and the color they use, first ellement is the LED posstion, second ellement is LED deafult color
int ledCounter = 0; // Counts the number of LEDs turned on in one command
String boulders[20]; // Array for storing boulders from the SD card
File myFile;
CRGB leds[numOfLeds]; //Sets's up the RGB LEDs
void setup()
{
Serial.begin(74880); //Initates serial cmunication with the PC
Serial1.begin(9600); //Initates serial cmunication with the bluethooth module
while (!Serial)
{
// wait for serial port to connect.
}
FastLED.addLeds<WS2812, ledSignalPin, GRB>(leds, numOfLeds); //Initates LEDs
resetLeds(true);
initializeSDCard();
myFile = SD.open("/");
if (!myFile)
{
Serial.println("No myFile");
}
else
{
Serial.println("All good");
}
readFileNames(myFile, boulders);
Serial.println("Setup finished");
}
void loop()
{
if (Serial1.available())
{
switch (Serial1.read())
{
case 76: //Poslati L i redne brojeve ledioda koje treba upaliti, LL C L C L C ... (L = LED possition, C = LED default color)
//Serial.println();
//Serial.println("Entered case 1");
resetLeds(false);
ledCounter = 0;
memset(ledArr, 0, sizeof(ledArr));
serialReadToArr(ledArr);
controlLeds(ledArr, confArr);
break;
case 82: //Slanje R gasi sve lediode
Serial.println();
Serial.println("Entered case 2");
resetLeds(false);
memset(ledArr, 0, sizeof(ledArr));
break;
case 83:
ledCounter = 0;
memset(ledArr, 0, sizeof(ledArr));
Serial.println();
Serial.println("Entered case 3");
while (!Serial1.available())
{
};
int fileIndex = Serial1.read();
readSDToArr(ledArr, boulders, 5, fileIndex);
printArr(ledArr, ledCounter);
controlLeds(ledArr, confArr);
break;
};
};
}
void resetLeds(bool resetAll)
{ //Resets all LEDs
//Serial.println("Entered resetLeds function");
//Serial.println(ledCounter);
if (resetAll)
{
for (int i = 0; i < numOfLeds; i++)
{
//Serial.println("resetLed cycle");
leds[i] = CRGB(0, 0, 0);
};
}
else
{
for (int i = 0; i < ledCounter; i++)
{
//Serial.println("resetLed cycle");
leds[ledArr[i][0]] = CRGB(0, 0, 0);
}
}
FastLED.show();
}
void controlLeds(int infArr[][2], int colorArr[][3]) //Turns on LEDs specified in the ledArr array
{
//Serial.println("Entered controlLeds function");
//Serial.println(ledCounter);
for (int i = 0; i < ledCounter; i++)
{
leds[infArr[i][0]] = CRGB(colorArr[infArr[i][1] + 1][0], colorArr[infArr[i][1] + 1][1], colorArr[infArr[i][1] + 1][2]);
};
FastLED.show();
}
void serialReadToArr(int fillArr[][2]) //Reads data from bluethooth module and stores them in a two dimensional array of an x by 2 size (only accepts numbers as high as the number of LEDs)
{
//Serial.println("Entered serialReadToArr function");
byte currValue = 0; // Holds value of the current recieved byte
byte tmpStr[3] = {0, 0, 0}; // Temporerly holds recieved values until mergied in one number
int digitCount = 0; // Counts the number of digits in a recieved number
bool typeIndicator = false; // False when recieveing LED possitions, true when recieveing LED color
do
{
while (!Serial1.available()) // Make sure there is available data
{
};
currValue = Serial1.read(); // Reads next Value
tmpStr[digitCount] = currValue; // Puts the values on hold
//Serial.println(currValue);
if (tmpStr[digitCount] == ' ' || tmpStr[digitCount] == '\n') // Check if whole number was recieved
{
for (int i = 0; i < digitCount; i++) // Convert tmpStr to a number
{
fillArr[ledCounter][typeIndicator] = fillArr[ledCounter][typeIndicator] + (tmpStr[i] - 48) * (pow(10, digitCount - (i + 1)));
//Serial.println("fillArr[ledCounter][typeIndicator]");
//Serial.println(fillArr[ledCounter][typeIndicator]);
}
typeIndicator = !typeIndicator;
if (!typeIndicator)
{
ledCounter++;
};
digitCount = 0;
}
else
{
digitCount++;
}
} while (currValue != '\n');
//printArr(fillArr, 49);
}
void printArr(int inArr[][2], int arrLen) //Prints the values stored in a two dimensional array of size x by 2
{
Serial.println("Entered printArr function");
for (int i = 0; i <= arrLen - 1; i++)
{
Serial.print(inArr[i][0]);
inArr[i][0] < 10 ? Serial.print(" ") : Serial.print(" ");
Serial.println(inArr[i][1]);
}
}
void initializeSDCard()
{
Serial.println("Initializing SD card...");
if (!SD.begin(chipSelect))
{
Serial.println("initialization failed!");
}
else
{
Serial.println("initialization done.");
}
}
void readFileNames(File dir, String filesArr[])
{
int fileCounter = 0;
while (true)
{
File entry = dir.openNextFile();
if (!entry)
{
// no more files
break;
}
if (fileCounter)
{
filesArr[fileCounter - 1] = entry.name();
Serial.println(filesArr[fileCounter - 1]);
}
fileCounter++;
entry.close();
}
dir.close();
}
void readSDToArr(int fillArr[][2], String filesArr[], int arrLen, int fileNum) //Reads data from bluethooth module and stores them in a two dimensional array of an x by 2 size (only accepts numbers as high as the number of LEDs)
{
Serial.println("Entered readSDToArr function");
Serial.println("");
char tmpStr[3] = {0, 0, 0}; // Temporerly holds recieved values until mergied in on
int digitCount = 0; // Counts the number of digits in a recieved number
bool typeIndicator = false; //false when recieveing LED possitions, true when recieveing LED color
File currentFile = SD.open(filesArr[fileNum - 48]); // Open the specified file
if (currentFile) // Check if the file was succefully opened
{
Serial.println("Current file good");
}
else
{
Serial.println("Problem with current file");
}
do
{
tmpStr[digitCount] = currentFile.read(); // Get the next byte from the file
if (tmpStr[digitCount] == ' ' || tmpStr[digitCount] == '\r' || tmpStr[digitCount] == '\n' || tmpStr[digitCount] < 0) // Check if end of number
{
for (int i = digitCount - 1; i >= 0; i--) // Convert the digits in tmpStr and save them
{
fillArr[ledCounter][typeIndicator] = fillArr[ledCounter][typeIndicator] + ((int)tmpStr[i] - 48) * (pow(10, (digitCount - (i + 1))));
}
typeIndicator = !typeIndicator;
if (!typeIndicator)
{
ledCounter++;
};
digitCount = 0;
}
else
{
digitCount++;
}
} while (tmpStr[digitCount] >= 0); // Check if end of file
currentFile.close(); // Close the file
}
void emptyArr() {}