/* ------------------------------------------------ * SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 01_simple version * by beltran berrocal * * this prog establishes a connection with the pc and waits for it to send him * a long string of characters like "hello Arduino!". * Then Arduino informs the pc that it heard the whole sentence * * this is the first step for establishing sentence long conversations between arduino and the pc. * serialRead() reads one byte at a time from the serial buffer. * so in order to print out the whole sentence at once * (it is actually still printing one byte at a time but the pc will receive it * not interupted by newLines or other printString inside you loop) * You must loop untill there are bytes in the serial buffer and * and print right away that byte you just read. * after that the loop can continue it's tasks. * * created 15 Decembre 2005; * copyleft 2005 Progetto25zero1 * * --------------------------------------------------- */ int serIn; //var that will hold the bytes in read from the serialBuffer void setup() { beginSerial(9600); } //auto go_to_the_line function void printNewLine() { printByte(13); printByte(10); } void loop () { //simple feedback from Arduino printString("Hello World"); printNewLine(); // only if there are bytes in the serial buffer execute the following code if(serialAvailable()) { //inform that Arduino heard you saying something printString("Arduino heard you say: "); //keep reading and printing from serial untill there are bytes in the serial buffer while (serialAvailable()){ serIn = serialRead(); //read Serial serialWrite(serIn); //prints the character just read } //the serial buffer is over just go to the line (or pass your favorite stop char) printNewLine(); } //slows down the visualization in the terminal delay(1000); }