/*This is an example of the EasyTransfer Library 2way communications. 


The sketch is for an Arduino with a servo attached to pin 3.


The other Arduino has a potentiometer attached to pin 0.


this arduino has a PING sensor attached to pins 11 and 12.


the other arduino has a PIR sensor attached to pin 3.


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>


#include <Servo.h> 

 

Servo myservo;  // create servo object to control a servo 

                // a maximum of eight servo objects can be created 

 

int pos = 0;    // variable to store the servo position 



//pin which triggers ultrasonic sound

const int pingPin = 12; 


//pin which delivers time to receive echo using pulseIn()

int inPin = 11;


//range in cm which is considered safe to enter, anything 

//coming within less than 5 cm triggers red LED

int safeZone = 5;



//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);  


 myservo.attach(3);  // attaches the servo on pin 3 to the servo object  

  

}


void loop(){

  

  //first, lets read our potentiometer and button and store it in our data structure

  txdata.servoval = analogRead(0);


  //raw duration in milliseconds, cm is the 

  //converted amount into a distance

  long duration, cm;


  //initializing the pin states

  pinMode(pingPin, OUTPUT);

  

  //sending the signal, starting with LOW for a clean signal

  digitalWrite(pingPin, LOW);

  delayMicroseconds(2);

  digitalWrite(pingPin, HIGH);

  delayMicroseconds(5);

  digitalWrite(pingPin, LOW);


  //setting up the input pin, and receiving the duration in 

  //microseconds for the sound to bounce off the object infront

  pinMode(inPin, INPUT);

  duration = pulseIn(inPin, HIGH);


  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);


 


if (cm > safeZone){

    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);

   myservo.write(rxdata.servoval);

    

    //delay

    delay(50);

  }

  

  //delay for good measure

  delay(50);

}



long microsecondsToCentimeters(long microseconds)

{

  // The speed of sound is 340 m/s or 29 microseconds per centimeter.

  // The ping travels out and back, so to find the distance of the

  // object we take half of the distance travelled.

  return microseconds / 29 / 2;

}