/*
Serial Call and Response in ASCII
Language: Wiring/Arduino
This program sends an ASCII A (byte of value 65) on startup and repeats that
until it gets some data in. Then it waits for a byte in the serial port, and
sends three ASCII-encoded, comma-separated sensor values, truncated by a
linefeed and carriage return, whenever it gets a byte in.
The circuit:
- potentiometers attached to analog inputs 0 and 1
- pushbutton attached to digital I/O 2
created 26 Sep 2005
by Tom Igoe
modified 24 Apr 2012
by Tom Igoe and Scott Fitzgerald
Thanks to Greg Shakar and Scott Fitzgerald for the improvements
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/SerialCallResponseASCII
*/intfirstSensor=0;// first analog sensor
intsecondSensor=0;// second analog sensor
intthirdSensor=0;// digital sensor
intinByte=0;// incoming serial byte
voidsetup(){// start serial port at 9600 bps and wait for port to open:
Serial.begin(9600);while(!Serial){;// wait for serial port to connect. Needed for native USB port only
}pinMode(2,INPUT);// digital sensor is on digital pin 2
establishContact();// send a byte to establish contact until receiver responds
}
voidloop(){// if we get a valid byte, read analog ins:
if(Serial.available()>0){// get incoming byte:
inByte=Serial.read();// read first analog input:
firstSensor=analogRead(A0);// read second analog input:
secondSensor=analogRead(A1);// read switch, map it to 0 or 255
thirdSensor=map(digitalRead(2),0,1,0,255);// send sensor values:
Serial.print(firstSensor);Serial.print(",");Serial.print(secondSensor);Serial.print(",");Serial.println(thirdSensor);}}