Simple Peer to Peer Protocol Example using Arduino

by Joseph Gundel

Step 1: Wire the Boards. See figure below:

For my example on the receiver I wired the photo-resistor to the 3.3v and analog pin zero. On the transmitter I wired pin 13 to the LED.

Step 2: Read values for light on/off

For the receiver I wrote a short program to just print the values to the serial port. I then set the transmitter Arduino Board up to send Dot , Dot, Dot, Dash, Dash, Dash in a loop. Then using the serial monitor I discovered that when the LED was on the value being read by the photo-resistor was above 779 and when the light was off it was below 779. (your values will be different depending which LED’s/Photo-resistor you use.

Step 3: Write interpreter program.

I set up a counter that increments for every 250ms the light is on. If the counter = 1 then it is a dot, if it equals 4 it is a dash. It ignores everything else. I did not use a start or stop signal because this is just a simple example.

Code Samples:

For Transmitter: Binary sketch size: 968 bytes

// Written by Joseph Gundel

int ledPin = 13; // LED connected to digital pin 13

void setup() {

// initialize the digital pin as an output:

pinMode(ledPin, OUTPUT);

}

void dot()

{

delay(250);

digitalWrite(ledPin, HIGH); // set the LED on

delay(250);

digitalWrite(ledPin, LOW);

}

void dash()

{

delay(250);

digitalWrite(ledPin, HIGH); // set the LED on

delay(1000);

digitalWrite(ledPin, LOW);

}

void loop()

{

dot();dot();dot();

dash();dash();dash();

}

For Receiver: Binary sketch size: 1932 bytes

//written by Joseph Gundel

const int analogInPin = 0; // Analog input pin for photo-resistor

int sensorValue; //value read in from sensor

int count=0; //keep track of millisec;

void setup() {

// initialize serial communications at 9600 bps:

Serial.begin(9600); //to write to serial monitor

}

void loop() {

// read the analog in value:

sensorValue = analogRead(analogInPin);

if (sensorValue > 779) //if light on increment counter

{ count++;}

else

{

if (count==1)

Serial.print(” dot”);

if (count==4)

Serial.print(” dash”);

count=0;

}

delay(250);

}