/*
Code by Brian Patton
7/20/2017
Multi Analog Read RF transmit and recieve demo
Reads 3 analog inputs on pin 0,1 and 9. Prints the result to the serial monitor and transmits to Hardware serial 2.
I used sharp GP2YAO21 sensors attached to Analog port 0 and 1 and a photoresitor on pin A9.
This example code is in the public domain.
*/
#define HWSERIAL Serial2 // Create Hardware Serial port 2
//********************************************************
// Setup (runs once)
//********************************************************
void setup() {
// initialize serial communications at 9600 bits per second for each port:
pinMode(A9, INPUT_PULLUP);
Serial.begin(9600);
HWSERIAL.begin(9600);
}
//********************************************************
// Recieve Hardware Serial data
//********************************************************
void getHWSerial() {
while (HWSERIAL.available() == 0); // wait until there is data in the buffer
if (HWSERIAL.available() > 0) { //if there is data in the serial buffer.....
Serial.println("A = " + String(HWSERIAL.readStringUntil('A'))); // Print Collected data until a 'A' is detected
Serial.println("B = " + String(HWSERIAL.readStringUntil('B'))); // Print Collected data until a 'A' is detected
Serial.println("C = " + String(HWSERIAL.readStringUntil('C'))); // Print Collected data until a 'A' is detected
Serial.println(" "); //
}
}
//********************************************************
// Send Hardware Serial data
//********************************************************
void sendHWSerial() {
int sensorValue0 = analogRead(A0); // read the input on analog pin 0:
int sensorValue1 = analogRead(A1); // read the input on analog pin 1:
int sensorValue9 = analogRead(A9); // read the input on analog pin 9:
Serial.println('A' + String(sensorValue0)); // print out the value you read:
Serial.println('B' + String(sensorValue1)); // print out the value you read:
Serial.println('C' + String(sensorValue9)); // print out the value you read:
Serial.println(" ");
HWSERIAL.print(String(sensorValue0) + 'A'); // Send data to Hardware Serial
HWSERIAL.print(String(sensorValue1) + 'B'); // Send data to Hardware Serial
HWSERIAL.print(String(sensorValue9) + 'C'); // Send data to Hardware Serial
delay(100);
}
//********************************************************
// Main Loop (loops forever)
//********************************************************
void loop() {
getHWSerial();
// sendHWSerial();
delay(10); // delay in between reads for stability
}