First of all, I would like to add that this is my first post and first 'bigger' project. I have set up two Arduinos connected wirelessly with an NRF module. Arduino 2 sends distance, and Arduino 1 turns on 240V. I want to double check if the 240V is on and if it is turn on an LED on arduino 2. I can easily communicate to Arduino 1 by sending the distance, but I can't get any information back from Arduino 1. It seems like Arduino 2 isn't receiving any data because Arduino 1 isn't even sending it. Why is that?
arduino 1
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <avr/wdt.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
const byte address1[6] = "00002";
const unsigned long ONE_MINUTE = 5000; // Duration of one minute in milliseconds
unsigned long lastTrueTime = 0; // Variable to store the time when open became true
bool open = false; // Initialize open as false
bool buffor = false; // Initialize buffer as false
void setup() {
wdt_enable(WDTO_8S);
Serial.begin(9600);
// Initialize radio and check if initialization was successful
if (!radio.begin()) {
Serial.println("Failed to initialize radio!");
// Hang here forever if initialization fails
}
radio.openReadingPipe(0, address);
radio.openWritingPipe(address1);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS);
pinMode(5, OUTPUT);
}
void loop() {
radio.startListening();
unsigned long currentMillis = millis();
unsigned int text;
if (radio.available()) {
radio.read(&text, sizeof(text)); // Read the received data
Serial.println(text);
if (text < 10 and text > 2 and !open and !buffor) {
open = true;
lastTrueTime = currentMillis; // Update the last time open became true
buffor = true; // Set buffer to true when the door is first opened
}
if (text > 10 || text < 2) {
open = false;
buffor = false; // Reset buffor to false when the door is closed
}
}
int openS = digitalRead(5);
if (radio.write(&openS, sizeof(open))) {
Serial.println("Y");
}
radio.stopListening();
if (open) {
digitalWrite(5, HIGH);
// Check if open has been true for more than one minute
if (currentMillis - lastTrueTime >= ONE_MINUTE) {
open = false; // Set open to false if it has been true for one
}
} else {
digitalWrite(5, LOW);
}
wdt_reset();
}
arduino 2
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <NewPing.h>
#include <avr/wdt.h>
#define trigPin 4
#define echoPin 3
#define DISTANCE 50
NewPing sonar(trigPin, echoPin, DISTANCE);
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
const byte address1[6] = "00002";
void setup() {
wdt_enable(WDTO_4S);
Serial.begin(9600);
if (!radio.begin()) {
Serial.println("Failed to initialize radio!");
// Hang here forever if initialization fails
}
radio.openWritingPipe(address);
radio.openReadingPipe(1, address1);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(5, OUTPUT);
}
void loop() {
unsigned int distance = sonar.ping_cm();
radio.stopListening();
// Send the distance measurement wirelessly
bool success = radio.write(&distance, sizeof(distance));
Serial.println(distance);
if (success) {
Serial.println("Distance sent successfully");
return;
} else {
Serial.println("F");
}
radio.startListening();
int open;
if (radio.available()) {
radio.read(&open, sizeof(open));
Serial.println(open);
}
if (open) {
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
wdt_reset();
}