1. Establish communication between MEGA-1, MEGA-2, and Temp-Hum-Aceler Sensor.
2. Upload the following sketch in MEGA-2. Your sketch is augmented with the codes to send data to MEGA-1 using Serial1 Port.
Sketch-1
#include<Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
int ADXLAddress = 0x53;
int Sensorluz = 0x23;
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();
#include <dht.h>
dht DHT;
#define DHT11_PIN 8
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
if (!accel.begin())
{
Serial.println("sensor inactive");
while (1);
}
sensor.begin();
}
void loop()
{
DHT.read11(DHT11_PIN);
Serial.print("temperature ");
float temp = DHT.temperature;
Serial.println(temp, 2);
Serial.print("humidity ");
float hum = DHT.humidity;
Serial.println(hum, 2);
delay(1000);
sensors_event_t event;
accel.getEvent (&event);
Serial.print("Aceler ");
float aceler = event.acceleration.x;
Serial.println(aceler, 2);
//-------------------------------------
Serial1.print(temp, 2);
Serial1.print(',');
Serial1.print(hum, 2);
Serial1.print(',');
Serial1.print(aceler, 2);
Serial1.print('\n');
delay(1000);
}
3. Upload the following sketch in MEGA-1 to receive Tem-Hum-Aceler data from MEGA-2 using Serial1 Port and show on it Serial Monitor.
Sketch-2
char myData[30] = {0};
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}
void loop()
{
byte n = Serial1.available();
if ( n != 0)
{
byte m = Serial1.readBytesUntil('\n', myData, 30);
myData[m] = '\0'; //null-byte is added
Serial.println(myData);
memset(myData, 0x00, 30); //array reset to 0s
Serial.println("=============================");
}
}
4. Press RESET buttons of both MEGA.
5. Check that MEGA-1 shows Tem-Hum-Aceler data as they are coming from MEGA-1. Post the screen shot of MEGA-1.
6. After that I will show you how to combine distance codes with the codes/Sketch-2 that receives data from MEGA-1. Try the following combined sketch by uploading it into MEGA-1.
Sketch-3
void processA(); //forward declaration
void processB();
char myData[30] = {0};
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
Serial3.begin(115200);
}
void loop()
{
processA(); //collect data from mEGA_1 and show
delay(2000);
Serial.println("==============================");
processB(); //collect data from distance sensor and show
delay(2000);
Serial.println("==============================");
}
void processA()
{
byte n = Serial1.available();
if ( n != 0)
{
byte m = Serial1.readBytesUntil('\n', myData, 30);
myData[m] = '\0'; //null-byte is added
Serial.println(myData);
memset(myData, 0x00, 30); //array reset to 0s
}
}
void processB()
{
int distance = 0;
int strength = 0;
getTFminiData(&distance, &strength);
while (!distance)
{
getTFminiData(&distance, &strength);
if (distance)
{
Serial.print(distance);
Serial.print("cm\t");
Serial.print("strength: ");
Serial.println(strength);
}
}
}
void getTFminiData(int* distance, int* strength)
{
static char i = 0;
char j = 0;
int checksum = 0;
static int rx[9];
if (Serial3.available())
{
rx[i] = Serial3.read();
if (rx[0] != 0x59)
{
i = 0;
}
else if (i == 1 && rx[1] != 0x59)
{
i = 0;
}
else if (i == 8)
{
for (j = 0; j < 8; j++)
{
checksum += rx[j];
}
if (rx[8] == (checksum % 256))
{
*distance = rx[2] + rx[3] * 256;
*strength = rx[4] + rx[5] * 256;
}
i = 0;
}
else
{
i++;
}
}
}