Skip to content

Instantly share code, notes, and snippets.

@gazolla
Created April 11, 2015 18:48
Show Gist options
  • Save gazolla/00278db86636a35ac958 to your computer and use it in GitHub Desktop.
Save gazolla/00278db86636a35ac958 to your computer and use it in GitHub Desktop.
Convert serial.read() into a useable string using Arduino
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup() {
Serial.begin(9600);
Serial.write("Power On");
}
char Comp(char* This) {
while (Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
if (strcmp(inData,This) == 0) {
for (int i=0;i<19;i++) {
inData[i]=0;
}
index=0;
return(0);
}
else {
return(1);
}
}
void loop()
{
if (Comp("m1 on")==0) {
Serial.write("Motor 1 -> Online\n");
}
if (Comp("m1 off")==0) {
Serial.write("Motor 1 -> Offline\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment