/*This is an example of the EasyTransfer Library 2way communications.
The sketch is for the Arduino with a potentiometer attached to analog pin 0.
The other Arduino has a servo attached to pin 3.
this arduino has a PIR sensor attached to pin 3.
the other arduino has a PING sensor attached to pins 11 and 12.
Both show the sensor status using the LED on pin 13.
The idea is each arduino will read their respective sensors and parse the data through a simple
if/else statement. the corresponding LED on pin 13 will light up accordingly.
the Arduino with the potentiometer will send it's value to the one with the servo.
The servo will move to the position based on the potentiometer.
*/
#include <EasyTransfer.h>
int pirPin = 3; //digital 3 this is for the PIR sensor on one of the arduinos
//create two objects
EasyTransfer ETin, ETout;
struct RECEIVE_DATA_STRUCTURE{
//put your variable definitions here for the data you want to receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int PIRorSONAR;
int servoval;
};
struct SEND_DATA_STRUCTURE{
//put your variable definitions here for the data you want to receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int PIRorSONAR;
int servoval;
};
//give a name to the group of data
RECEIVE_DATA_STRUCTURE rxdata;
SEND_DATA_STRUCTURE txdata;
void setup(){
Serial.begin(9600);
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ETin.begin(details(rxdata), &Serial);
ETout.begin(details(txdata), &Serial);
pinMode(13, OUTPUT);
pinMode(pirPin, INPUT);
}
void loop(){
//first, lets read our potentiometer and button and store it in our data structure
txdata.servoval = map(analogRead(0),0,1023,0,180);
//second, read PIR
int pirVal = digitalRead(pirPin);
if(pirVal == LOW){ //was motion detected
txdata.PIRorSONAR = LOW;
} else {
txdata.PIRorSONAR = HIGH;
}
//then we will go ahead and send that data out
ETout.sendData();
//there's a loop here so that we run the recieve function more often then the
//transmit function. This is important due to the slight differences in
//the clock speed of different Arduinos. If we didn't do this, messages
//would build up in the buffer and appear to cause a delay.
if(ETin.receiveData()){
//set our LED on or off based on what we received from the other Arduino
digitalWrite(13, rxdata.PIRorSONAR);
//delay
delay(50);
}
//delay for good measure
delay(50);
}