Last active
August 29, 2015 14:06
-
-
Save domenicomonaco/d59519e0de7677df42c3 to your computer and use it in GitHub Desktop.
Simple Arduino Serial Communication with Echo of line
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
/** | |
* Author: Domenico Monaco | |
* | |
* Description: Simple Arduino Serial communication, with echo of entire line received. | |
* | |
* Source: https://gist.github.com/kiuz/d59519e0de7677df42c3/ | |
* | |
* License: GNU v2 2014 | |
*/ | |
int incomingByte = 0; // for incoming serial data | |
bool waiting = false; | |
String line = ""; | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
if (Serial.available() > 0) { | |
waiting = false; // Set to false, Arduino not in "Waiting" state but are in "Reading" state | |
incomingByte = Serial.read(); // read Char | |
line = line + (char)incomingByte; // add Char to line | |
delay(1); //Delay of Serial transfer speed | |
}else{ | |
//waiting is False only after "reading" state | |
//with this line are printed only one times into "waiting" state | |
if(waiting==false){ | |
waiting = true; | |
// print entire line | |
Serial.print(line); | |
Serial.print(" "); | |
line = ""; // Resete line | |
Serial.println(""); | |
Serial.println("Waiting for a message..."); | |
Serial.println("");} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment