Board: Arduino Mega (ATmega1280)
Hi Folks,
For a few days now I've been racking my brain, searching the web and the forums and I can't seem to find a solution to a little problem I'm having. The backstory: I'm creating an app in processing that gets data from an XML feed and based on that it will write some data to the arduino for it to then process.
However, I have one small problem - the Arduino side of things seem to be incredibly random in that bytes appear to go missing. I can confirm that the processing side is consistent in what it is sending. I'm sure this has most likely been covered off before but I'd love it if someone could look over my code and help point me in the right direction.
Apologies in advance for the lack of commenting in the code.
Kind regards
Mark.
Processing Code (trimmed down):
String []colour_list = {
"102,000,053", "054,000,201", "006,000,249", "000,058,255", "000,106,255", "000,154,255", "000,213,255", "000,255,255",
"000,255,183", "000,255,111", "000,255,000", "088,255,000", "184,255,000", "259,230,000", "255,187,000", "255,140,000",
"255,099,000", "255,047,000", "255,00,000", "195,000,000"
};
int colour_index;
rgbcolour = colour_list[13];
// I've trimmed the rest of the processing code and have pasted in the send
String sendVar = "^" + rgbcolour + "$";
port.write(sendVar);
Processing then sends the following consistently to the Arduino:
^000,255,255$
Arduino Code:
#define START_COLOR_CHAR '^'
#define END_COLOR_CHAR '
The random data after 5 sends from processing to the Arduino:
000,255,255
000,25
02,5
000,255,255
0255
#define COLOR_SIZE 13
#define PIN_RED 9
#define PIN_GREEN 11
#define PIN_BLUE 10
char serialMessage[COLOR_SIZE];
unsigned int readChar;
unsigned int count;
unsigned long color;
unsigned int r;
unsigned int g;
unsigned int b;
boolean readingSerial;
void setup() {
Serial.begin(115200);
readingSerial = false;
}
void loop() {
if (Serial.available() > 0 && !readingSerial) {
if (Serial.read() == START_COLOR_CHAR) {
serialReadColor();
}
}
}
void serialReadColor() {
readingSerial = true;
count = 0;
iniReading:
if (Serial.available() > 0) {
readChar = Serial.read();
if (readChar == END_COLOR_CHAR && count <= COLOR_SIZE) {
//if (readChar == END_COLOR_CHAR) {
goto endReading;
} else {
serialMessage[count++] = readChar;
goto iniReading;
}
}
goto iniReading;
endReading:
readingSerial = false;
serialMessage[count] = '\0';
Serial.println(serialMessage);
//setColor(serialMessage);
}
The random data after 5 sends from processing to the Arduino:
§DISCOURSE_HOISTED_CODE_3§