Last active
August 29, 2015 14:05
-
-
Save mpflaga/634212e440b81d2311b9 to your computer and use it in GitHub Desktop.
Example of looking for specific text from serial input stream
This file contains hidden or 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
char buffer[5]; | |
#define buffer_size sizeof(buffer)/sizeof(buffer[0]) | |
int8_t buffer_pos = 0; | |
void setup() { | |
//Initialize serial and wait for port to open: | |
Serial.begin(115200); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for Leonardo only | |
} | |
Serial.println("Serial to mySerial demo started"); | |
Serial.print("buffer_size = "); Serial.print(buffer_size); Serial.println(""); | |
buffer_pos = 0; | |
} | |
void loop() { | |
char inByte; | |
while (Serial.available()) { | |
inByte = Serial.read(); | |
Serial.print("inByte = "); Serial.print(inByte); Serial.println(""); | |
Serial.print("buffer_pos = "); Serial.print(buffer_pos); Serial.println(""); | |
while (buffer_pos >= buffer_size) { | |
for (int8_t i = 0; i < buffer_size - 1; i++) { // rotate buffer left if at end of buffer | |
buffer[i] = buffer[i + 1]; | |
} | |
buffer_pos--; | |
} | |
buffer[buffer_pos++] = inByte; | |
Serial.print("buffer = "); | |
for (int8_t i = 0; i < buffer_size; i++) { | |
Serial.print(buffer[i]); | |
} | |
Serial.println(""); | |
if (strcasestr(buffer, "abc")) { | |
Serial.println("BINGO"); | |
} else { | |
Serial.println("No Joy"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is example out. Note that inByte is the character sent from a serial stream. The trigger text was ABC either upper or lower case.