Last active
December 20, 2016 08:11
-
-
Save harrisonhjones/881de16c134d1fa03b04 to your computer and use it in GitHub Desktop.
Particle IO - serialEvent Emulation
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
// #define USING_SERIAL2 // Uncomment if you need to use Serial2 | |
#ifdef USING_SERIAL2 | |
#include "Serial2/Serial2.h" // Only needed if you use Serial2 | |
void serialEvent2(){ | |
Serial.print("Data is available on serial2"); | |
Serial2.read(); | |
} | |
#endif | |
void serialEvent(){ | |
Serial.print("Data is available on serial"); | |
Serial.read(); | |
} | |
void serialEvent1(){ | |
Serial.print("Data is available on serial1"); | |
Serial1.read(); | |
} | |
void checkSerial(); | |
void setup() { | |
Serial.begin(9600); // serial via USB port | |
Serial1.begin(9600); // serial via TX and RX pins | |
#ifdef USING_SERIAL2 | |
Serial2.begin(9600); // serial via D1(TX) and D0(RX) pins | |
#endif | |
} | |
void loop() { | |
checkSerial(); // Add this to your loop(). Calls the function "serialEvent" when there's data on the serial port | |
} | |
void checkSerial() { | |
if (Serial.available() > 0) { | |
serialEvent(); | |
} | |
if (Serial1.available() > 0) { | |
serialEvent1(); | |
} | |
#ifdef USING_SERIAL2 | |
if (Serial2.available() > 0) { | |
serialEvent2(); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment