/*
Serial Call and Response
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 sensor values 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/SerialCallResponse
*/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:
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, divide by 4 to make the range 0-255:
firstSensor=analogRead(A0)/4;// delay 10ms to let the ADC recover:
delay(10);// read second analog input, divide by 4 to make the range 0-255:
secondSensor=analogRead(1)/4;// read switch, map it to 0 or 255L
thirdSensor=map(digitalRead(2),0,1,0,255);// send sensor values:
Serial.write(firstSensor);Serial.write(secondSensor);Serial.write(thirdSensor);}}