Hi guys,
I need to send more than 40 unsigned long int (they come from hex color) trough serial com from a Photon to an Arduino Mega, but after 11 unsigned long I start getting errors and 4 ou 5 commas (see In the code bellow) after I don’t receive anything. I seems to happened after 100 chars received.
Any idea of the problem ?
Code of the transmitter (Photon):
unsigned long var[55] = {
L1L, L1R,
L2L, L2R,
L3L, L3R,
L4L, L4R,
L5L, L5R,
L6L, L6R,
L7L, L7R,
L8L, L8R,
L9L, L9R,
L10L, L10R,
BGL1L, BGL1R,
BGL2L, BGL2R,
BGL3L, BGL3R,
BGL4L, BGL4R,
BGL5L, BGL5R,
BGL6L, BGL6R,
BGL7L, BGL7R,
BGL8L, BGL8R,
BGL9L, BGL9R,
BGL10L, BGL10R,
Mode, Pwr,
Rot, RotO,
RotT, Rdm,
RdmT,Mood,
MoodT, MoodS,
IndexColorS
};
Serial.println("Sending values to arduino");
Serial1.print("<");
for(int i = 0; i < 51; i++){
Serial1.print(var[i]);
Serial1.print(",");
Serial.println(var[i]);
}
Serial1.print(">");
Serial.println("Data sent");
Code of the receiver (Arduino mega):
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() {
if (newData == true) {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(receivedChars,","); // get the first part - the string
var[0] = atoi(strtokIndx); // copy it to messageFromPC
for(int i = 1; i<51; i++) {
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
var[i] = atoi(strtokIndx); // convert this part to an integer
}
for(int i = 0; i<51; i++) {
Serial.println(var[i]);
EEPROM.write(i, var[i]);
}
LL[0] = var[0];
LR[0] = var[1];
LL[1] = var[2];
LR[1] = var[3];
LL[2] = var[4];
LR[2] = var[5];
LL[3] = var[6];
LR[3] = var[7];
LL[4] = var[8];
LR[4] = var[9];
LL[5] = var[10];
LR[5] = var[11];
LL[6] = var[12];
LR[6] = var[13];
LL[7] = var[14];
LR[7] = var[15];
LL[8] = var[16];
LR[8] = var[17];
LL[9] = var[18];
LR[9] = var[19];
BGLL[0] = var[20];
BGLR[0] = var[21];
BGLL[1] = var[22];
BGLR[1] = var[23];
BGLL[2] = var[24];
BGLR[2] = var[25];
BGLL[3] = var[26];
BGLR[3] = var[27];
BGLL[4] = var[28];
BGLR[4] = var[29];
BGLL[5] = var[30];
BGLR[5] = var[31];
BGLL[6] = var[32];
BGLR[6] = var[33];
BGLL[7] = var[34];
BGLR[7] = var[35];
BGLL[8] = var[36];
BGLR[8] = var[37];
BGLL[9] = var[38];
BGLR[9] = var[39];
ActiveMode = var[40];
power = var[41];
rotation = var[42];
rotation_order = var[43];
rotation_time = var[44];
random_mode = var[45];
mode_time = var[46];
mood = var[47];
mood_type = var[48];
moodSpeed = var[49];
colorIndexSpeed = var[50];
newData = false;
}
Reception test code (Mega):
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("SERIAL: BEGIN");
}
void loop() {
if(Serial1.available()){
Serial.println(Serial1.read());
}
}