Skip to content

Instantly share code, notes, and snippets.

@johnmckerrell
Created July 24, 2013 09:56
Show Gist options
  • Save johnmckerrell/6069317 to your computer and use it in GitHub Desktop.
Save johnmckerrell/6069317 to your computer and use it in GitHub Desktop.
char serialFormat = { 'M', 'K', 'E', 'W', 'D', ' ', ' ', ' ', ' ', 'D', 'W', 'E', 'K', 'M' };
int preambleStopIndex = 4;
int postambleStartIndex = 9;
int formatLength = 13;
int valueLength = 4;
void loop() {
int formatIndex = 0;
char value[valueLength] = { 0, 0, 0, 0 };
while (1) {
char in = Serial.read();
if (formatIndex > preambleStopIndex && formatIndex < postambleStartIndex) {
value[formatIndex-preambleStopIndex-1] = in;
} else if (formatIndex >= formatLength) {
break;
} else if (in != serialFormat[formatIndex]) {
// Start matching from the beginning again
formatIndex = 0;
} else {
// Last character was useful, look for the next one
++formatIndex;
}
}
// value should now contain 4 useful characters which we've confirmed are valid
if (value[0] == 'T') {
// Turn
} if (value[0] == 'L') {
// do LEDs
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment