// Program: SerialInputDemo.ino
/* A reference example for students to implement Ex.3 Q2
*/
int incomingByte(int/bool) = 0; // for incoming serial data byte
int numBytes = 0; // no. of bytes received
void setup() {
Serial.begin(115200); // initialize the Serial port
}
void loop() {
// reply only when you receive data
if (Serial.available() > 0) // check the Serial port if a
// data byte is received
{
// check and print the no. of bytes received
numBytes = Serial.available();
Serial.print("No. of bytes received = ");
Serial.println(numBytes);
delay(1000);
// read a data byte
incomingByte = Serial.read();
// print the data byte received
Serial.print("Data byte received [printed by println()] = ");
Serial.println(incomingByte);
Serial.print("Data byte received [printed by write()] = ");
Serial.write(incomingByte);
Serial.println();
delay(1000);
} else // print when no data byte received
{
Serial.println("No data byte is received!");
delay(1000);
}
}
/* End of program */