Hi,
I recently set up a basic prototype of a 2 way communication between an arduino nano and an arduino mega. The communication goes as follows: The arduino nano reads a joystick value and sends that in a structure (as in future there will be more than one reading). The mega recieves this and prints it to the serial monitor and then returns two float values that are meant to be reads from a DHT11 module. Basically when I have the floats set to just 0.00 or any number, everything works fine and each second the two arduinos send and receive the data. As soon as I add in the code to read the DHT11 values and set them to be the floats I'm sending from the mega, the whole thing just loses it and sends a few messages in extremely quick succession before stopping completely. On top of this, when I remove power the 3.3V power cable from the DHT11, the RF24 module stops working. It is really bizarre. If anyone has encountered this issue or may know what it could be causing it that would be great. Unfortunately it wont let me include my code as an attachment.
*Edit: I didnt know how to include code but here it is now:
Node A arduino nano (works fine):
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
// MasterSwapRoles
// code for the nano
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
#define CE_PIN 7
#define CSN_PIN 8
const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
struct dataToSend {
int steeringval;
};
dataToSend data1;
float dataReceived[2];
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;
unsigned long lastReceiveTime = 0;
// send once per second
//============
void setup() {
Serial.begin(9600);
Serial.println("MasterSwapRoles Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openWritingPipe(slaveAddress);
radio.openReadingPipe(1, masterAddress);
radio.setPALevel(RF24_PA_MIN);
radio.setRetries(3,5); // delay, count
send(); // to get things started
prevMillis = millis(); // set clock
lcd.init(); // set LCD
lcd.backlight();
}
//=============
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
getData();
showData();
}
//====================
void send() {
radio.stopListening();
data1.steeringval = map(analogRead(A1),0,1023,0,255);
bool rslt;
rslt = radio.write( &data1, sizeof(dataToSend) );
radio.startListening();
Serial.print("Data Sent ");
Serial.print(data1.steeringval);
if (rslt) {
Serial.println(" Acknowledge received");
}
else {
Serial.println(" Tx failed");
}
}
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
lastReceiveTime = millis();
newData = true;
}
currentMillis = millis();
if (currentMillis - lastReceiveTime > 3000) {
lcd.clear();
lcd.setCursor(2,1);
lcd.print("Connection Lost!");
delay(1000);
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.print(dataReceived[0]);
Serial.print(", ");
Serial.print(dataReceived[1]);
Serial.print(", ");
Serial.println(sizeof(dataReceived));
Serial.println();
lcd.setCursor(2,1);
lcd.print("Temp: ");
lcd.print(dataReceived[0]);
lcd.print(" Deg C");
lcd.setCursor(2,2);
lcd.print("Humidity: ");
lcd.print(dataReceived[1]);
lcd.print(" %");
newData = false;
}
}
//================
Node B arduino mega (has the issue when reading the DHT11):
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <DHT.h>
#include <DHT_U.h>
// SlaveSwapRoles
DHT dht(53, DHT11);
#define CE_PIN 24
#define CSN_PIN 22
const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
struct dataReceived {
int steeringval;// must match dataToSend in master
} data;
float replyData[2];
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println("SlaveSwapRoles Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
radio.openReadingPipe(1, slaveAddress);
radio.setPALevel(RF24_PA_MIN);
radio.setRetries(3,5); // delay, count
radio.startListening();
}
//====================
void loop() {
getData();
showData();
send();
}
//====================
void send() {
if (newData == true) {
radio.stopListening();
bool rslt;
rslt = radio.write( &replyData, sizeof(replyData) );
radio.startListening();
Serial.print("Reply Sent ");
Serial.print(replyData[0]);
Serial.print(", ");
Serial.print(replyData[1]);
Serial.print(", ");
Serial.println(sizeof(replyData));
if (rslt) {
Serial.println("Acknowledge Received");
}
else {
Serial.println("Tx failed");
}
Serial.println();
newData = false;
}
}
//================
void getData() {
if ( radio.available() ) {
radio.read( &data, sizeof(dataReceived) );
newData = true;
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(data.steeringval);
}
}
void checkTemp () {
replyData[0] = dht.readTemperature();
replyData[1] = dht.readHumidity();
}
//================