Created
May 5, 2017 02:27
-
-
Save anonymous/2889e8beab937d15ae5f0813c2d34cba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define HWSERIAL Serial3 | |
const byte numChars = 32; | |
char receivedChars[numChars]; | |
char tempChars[numChars]; // temporary array for use when parsing | |
char messageFromPC[numChars] = {0}; | |
int integerFromPC = 0; | |
float floatFromPC = 0.0; | |
int val = 0; | |
boolean newData = false; | |
//=========================================================================== | |
void setup() { | |
Serial.begin(9600); | |
HWSERIAL.begin(9600); // communication to hardware serial | |
Serial.println("Ready"); | |
pinMode(13, OUTPUT); | |
digitalWrite(13, HIGH); | |
delay(1000); | |
} | |
//=========================================================================== | |
void loop() { | |
recvWithStartEndMarkers(); | |
if (newData == true) { | |
strcpy(tempChars, receivedChars); | |
// this temporary copy is necessary to protect the original data | |
// because strtok() used in parseData() replaces the commas with \0 | |
parseData(); | |
showParsedData(); | |
newData = false; | |
} | |
displayLED(); | |
HWSERIAL.flush(); | |
} | |
//=========================================================================== | |
void recvWithStartEndMarkers() { | |
static boolean recvInProgress = false; | |
static byte ndx = 0; | |
char startMarker = '<'; | |
char endMarker = '>'; | |
char rc; | |
while (HWSERIAL.available() > 0 && newData == false) { | |
rc = HWSERIAL.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() { // split the data into its parts | |
char * strtokIndx; // this is used by strtok() as an index | |
strtokIndx = strtok(tempChars,","); // get the first part - the string | |
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC | |
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off | |
integerFromPC = atoi(strtokIndx); // convert this part to an integer | |
strtokIndx = strtok(NULL, ","); | |
floatFromPC = atof(strtokIndx); // convert this part to a float | |
} | |
//=========================================================================== | |
void showParsedData() { | |
Serial.print("Message "); | |
Serial.println(messageFromPC); | |
Serial.print("Potval "); | |
Serial.println(integerFromPC); | |
Serial.print("Float "); | |
Serial.println(floatFromPC); | |
val = (integerFromPC); | |
} | |
//=========================================================================== | |
void displayLED() { | |
digitalWrite(13, HIGH); | |
delay(val); | |
digitalWrite(13, LOW); | |
delay(val); | |
delay(1100-val-val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment