/*
Code by Brian Patton
7/20/2017
Simple RF transmit and recieve demo
Reads an analog input on pin 0, prints the result to the serial monitor and transmits to Hardware serial 2.
I used a sharp GP2YAO21 sensor attached to Analog port 0
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:
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(HWSERIAL.readStringUntil(10)); // Print Collected data until a line feed (Char 10) is detected
}
}
//********************************************************
// Send Hardware Serial data
//********************************************************
void sendHWSerial() {
int sensorValue = analogRead(A0); // read the input on analog pin 0:
Serial.println(sensorValue); // print out the value you read:
HWSERIAL.println(sensorValue); // Send data to Hardware Serial
delay(100);
}
//********************************************************
// Main Loop (loops forever)
//********************************************************
void loop() {
getHWSerial();
// sendHWSerial();
delay(10); // delay in between reads for stability
}