Hello AWOL
Below is my Slave Sender sketch. It is an Arduino Nano with the HC-SR04 ultrasonic sensor connected to it:
#include <Wire.h>
//******************************************************************************************************************************************************************************
//************************************** Declaring Global Variables *********************************************************
//******************************************************************************************************************************************************************************
#define echoPin 13 // Echo Pin
#define trigPin 12 // Trigger Pin
long duration;
long distance; // Duration used to calculate distance
unsigned char data[4];
//******************************************************************************************************************************************************************************
//************************************** Void Setup *********************************************************
//******************************************************************************************************************************************************************************
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
//******************************************************************************************************************************************************************************
//************************************** Void loop *********************************************************
//******************************************************************************************************************************************************************************
void loop()
{
}
//******************************************************************************************************************************************************************************
//************************************** Request data function *********************************************************
//******************************************************************************************************************************************************************************
void requestEvent()
{
Ultrasonic_Measurement(); // Get the distance measurement.
data[0] = distance; // Split long distance into 4 bytes.
data[1] = distance>>8;
data[2] = distance>>16;
data[3] = distance>>24;
Wire.write(data,4); // Write the 4 bytes over the I2C line.
}
//****************************************************************************************************************************************************************************
//************************************************** Ultasonic measurement fucntion ********************************************************
//****************************************************************************************************************************************************************************
void Ultrasonic_Measurement()
{
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (mm) based on the speed of sound.
distance = duration/5.82;
//Delay 50ms before next reading.
delay(50);
}
//****************************************************************************************************************************************************************************
Here is the Master Receiver part of the code which I'm not sure how to implement. I've got everything hooked up to a GLCD so I'm printing the received variable on the GLCD and I'm not making use of serial.print because of this.
#include <openGLCD.h>
#include <include/openGLCD_GLCDv3.h> // GLCDv3 compatibilty mode
#include <Wire.h>
//******************************************************************************************************************************************************************************
//************************************** Declaring Global Variables *********************************************************
//******************************************************************************************************************************************************************************
union Distance
{
long Distance;
byte Received_Byte_Array[4];
};
//******************************************************************************************************************************************************************************
//************************************** Void Setup *********************************************************
//******************************************************************************************************************************************************************************
void setup()
{
// Initialize the GLCD
GLCD.Init();
// Start I²C bus as master
Wire.begin();
}
//******************************************************************************************************************************************************************************
//************************************** Void loop *********************************************************
//******************************************************************************************************************************************************************************
void loop()
{
Wire.requestFrom(2, 6); // request 5 bytes from slave device #2
if (Wire.available()) // slave may send less than requested
{
for ( int i; i <= 4; i++)
{
Received_Byte_Array[i] = Wire.read;
}
}
GLCD.SelectFont(System5x7, BLACK);
GLCD.CursorToXY(10, 10);
GLCD.print(Distance);
delay(100);
}