Hello programmers,
I'm having some problems with a project.
The setup for the project is that I have 2 arduino's with RF24 chip.
both of them have a HC-SR04 sensor (ultrasonic) connected and one Led.
The idea of my program is that at startup my master arduino Led turns on a LED.
To show that this arduino is active and measuring.
If I then manipulate the ultrasonic sensor the led goes out and it has to send something to the other arduino.
This arduino then receives something and turns his led on and activates his sensor.
If I do the same on this sensor the process needs to repeat itself.
The sensor is working. I see that the led turns up on startup and goes off when I put my hand over the sensor.
But I'm not sending or receiving anyting...
Can someone give me some advice?
Regards,
Steven
Code Arduino 1:
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(10, 9); // nRF24L01 (CE,CSN)
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 00; // Address of this node in Octal format ( 04,031, etc)
const uint16_t node01 = 01; // Address of the other node in Octal format
//const uint16_t node012 = 012;
//const uint16_t node022 = 022;
const int trigPin = 7;
const int echoPin = 6;
const int ledPin = 3;
long duration;
int Master;
void setup() {
SPI.begin();
Serial.begin(57600);
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
pinMode(ledPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Master = 1;
}
void loop() {
network.update();
if (Master == 0) {
Serial.println("Looking for message");
while ( network.available() ) { // Is there any incoming data?
Serial.println("received message");
RF24NetworkHeader header;
unsigned long incoming;
network.read(header, &incoming, sizeof(incoming));
Master = 1;
}
}
if (Master == 1) {
digitalWrite(ledPin, HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
if (duration < 2500) {
digitalWrite(ledPin, LOW);
Master = 0;
Serial.println("Send message");
unsigned long Outgoing = 10;
RF24NetworkHeader header2(node01); // (Address where the data is going)
bool ok = network.write(header2, &Outgoing, sizeof(Outgoing)); // Send the data
}
}
}
Code of the other arduino:
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(10, 9); // nRF24L01 (CE,CSN)
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 01; // Address of this node in Octal format ( 04,031, etc)
const uint16_t node01 = 00; // Address of the other node in Octal format
//const uint16_t node012 = 012;
//const uint16_t node022 = 022;
const int trigPin = 7;
const int echoPin = 6;
const int ledPin = 3;
long duration;
int Master;
void setup() {
SPI.begin();
Serial.begin(57600);
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
pinMode(ledPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Master = 0;
}
void loop() {
network.update();
if (Master == 0) {
Serial.println("Looking for message");
while ( network.available() ) { // Is there any incoming data?
Master = 1;
Serial.println("received message");
RF24NetworkHeader header;
unsigned long incoming;
network.read(header, &incoming, sizeof(incoming));
}
}
if (Master == 1) {
digitalWrite(ledPin, HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
if (duration < 2500) {
digitalWrite(ledPin, LOW);
Master = 0;
Serial.println("Send message");
unsigned long Outgoing = 1;
RF24NetworkHeader header2(node01); // (Address where the data is going)
bool ok = network.write(header2, &Outgoing, sizeof(Outgoing)); // Send the data
}
}
}