Skip to content

Instantly share code, notes, and snippets.

@emilyhorsman
Last active June 3, 2018 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilyhorsman/6357f628957060916dfda54a3ff956a8 to your computer and use it in GitHub Desktop.
Save emilyhorsman/6357f628957060916dfda54a3ff956a8 to your computer and use it in GitHub Desktop.
uint8_t byteRead;
uint8_t byteIndex;
uint8_t checksum;
uint8_t buttonNum;
bool buttonVal;
bool validState;
void loop() {
t = millis();
if (ble.available()) {
validState = false;
byteRead = ble.read();
if (byteIndex == 0 && byteRead == '!') {
validState = true;
} else if (byteIndex == 1 && byteRead == 'B') {
validState = true;
} else if (byteIndex == 2) {
validState = true;
buttonNum = byteRead - '0';
} else if (byteIndex == 3) {
validState = true;
buttonVal = byteRead - '0';
}
if (byteIndex <= 3 && validState) {
byteIndex++;
checksum += byteRead;
return;
} else if (byteIndex == 4) {
checksum = ~checksum;
if (checksum != byteRead) {
Serial.print("Checksum mismatch. Received: ");
Serial.print(checksum);
Serial.print(" Expected: ");
Serial.println(byteRead);
return;
}
checksum = byteIndex = 0;
}
Serial.print((char) (buttonNum + '0'));
Serial.print(": ");
Serial.println(buttonVal);
Serial.print("This took: ");
Serial.print(millis() - t);
Serial.println(" mS");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment