Hello
I am trying to send and receive the data to/from two different nodes to/from the central MCU. The procedure that I am using is as follows:
The central node writes the control information to the panel1, in return panel1 sends the ADC values read and status values to the central node. Then it does the same with panel2.
The code I am using on the central node is
#include<SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
//#include "printf.h"
RF24 radio(9,10);
const uint64_t w_pipe[2] = { 0xA0A0A0A0BBLL, 0xA0A0A0A0CCLL };
const uint64_t r_pipe[2] = { 0xF0F0F0A077LL, 0xF0F0F0A055LL };
struct PICO
{
int power1, power2;
boolean sh1,sh2,sh3,sh4,ma;
};
struct POCI
{
int power1,power2;
boolean ss1,ss2,ss3,ss4,ma;
};
byte sz=sizeof(POCI);
PICO c1,c2;
POCI p1,p2;
void setup()
{
pinMode(8,OUTPUT);
//digitalWrite(8,HIGH);
Serial.begin(9600);
Serial.println("\n This is the Central Node \n");
Serial.flush();
//Setting up the nRF24L01 chip
radio.begin();
radio.setRetries(15,15);
radio.setPayloadSize(sz);
c1.power1=1000;c1.power2=500;c1.sh1=HIGH;c1.sh2=HIGH;c1.sh3=HIGH;c1.sh4=LOW;
c2.power1=1000;c2.power2=500;c2.sh1=HIGH;c2.sh2=LOW;c2.sh3=HIGH;c2.sh4=HIGH;
}
void loop()
{
//open the pipe to write to Panel1 controller
radio.openWritingPipe(w_pipe[0]);
radio.write(&c1,sz);
radio.openReadingPipe(1,r_pipe[0]);
//Finish writing and start listening for the reply
radio.startListening();
//wait for reply, i.e data to be available
while(!radio.available());
digitalWrite(8,HIGH);
radio.read(&p1,sz);
radio.stopListening();
//digitalWrite(8,LOW);
//delay(500);
radio.openWritingPipe(w_pipe[1]);
radio.write(&c2,sz);
radio.openReadingPipe(2,r_pipe[1]);
//Finish writing and start listening for the reply
radio.startListening();
//wait for reply, i.e data to be available
while(!radio.available());
radio.read(&p2,sz);
//stop listening to any replies
radio.stopListening();
//digitalWrite(8,HIGH);
Serial.print("Panel 1 " );
Serial.print("P1 ");
serial_withz(p1.power1);
Serial.print(" P2 ");
serial_withz(p1.power2 );
Serial.print(" ");
Serial.print(p1.ss1);
//Serial.print("\t");
Serial.print(p1.ss2);
//Serial.print("\t");
Serial.print(p1.ss3);
//Serial.print("\t");
Serial.print(p1.ss4);
Serial.print("\n");
Serial.print(" Panel 2=" );
Serial.print("P1 ");
serial_withz(p2.power1);
Serial.print(" P2 ");
serial_withz(p2.power2 );
Serial.print(" ");
Serial.print(p2.ss1);
//Serial.print("\t");
Serial.print(p2.ss2);
//Serial.print("\t");
Serial.print(p2.ss3);
//Serial.print("\t");
Serial.print(p2.ss4);
digitalWrite(8,LOW);
delay(100);
}
The code for panel1
#include<SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
//#include "printf.h"
RF24 radio(9,10);
const uint64_t w_pipe[2] = { 0xA0A0A0A0BBLL, 0xA0A0A0A0CCLL };
const uint64_t r_pipe[2] = { 0xF0F0F0A077LL, 0xF0F0F0A055LL };
struct PICO
{
int power1, power2;
boolean sh1,sh2,sh3,sh4,ma;
};
struct POCI
{
int power1,power2;
boolean ss1,ss2,ss3,ss4,ma;
};
byte sz=sizeof(POCI);
PICO c1;
POCI p1;
int panelpower;
void setup()
{
pinMode(2,OUTPUT);
digitalWrite(2,LOW);
//Serial.begin(9600);
//Serial.println(" This is the Panel1 \n");
//Serial.flush();
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
//Setting up the nRF24L01 chip
radio.begin();
radio.setRetries(15,15);
radio.setPayloadSize(sz);
//Writing pipe 1 for central node is reading pipe for Panel 1
radio.openReadingPipe(1,w_pipe[0]);
//Open the Central node reading pipe 1 for the Panel 1 to write the data to central node
radio.openWritingPipe(r_pipe[0]);
}
void loop()
{
//Temporaryly reading analog values
p1.power1=analogRead(A0);
p1.power2=analogRead(A1);
panelpower=p1.power1+p1.power2;
p1.ss1=digitalRead(5);
p1.ss2=digitalRead(6);
p1.ss3=digitalRead(7);
p1.ss4=digitalRead(8);
//Start listening on the opened reading pipe for Central Node to send information
radio.startListening();
digitalWrite(2,HIGH);
//Wait for the data to be available
while(!radio.available());
//Read the dat sent from Central Node into c1
radio.read(&c1,sz);
//Stop listening to get ready to write back to central node
radio.stopListening();
digitalWrite(2,LOW);
//Write the data available with Panel 1 to Central Node
radio.write(&p1,sz);
}
The code for panel2
#include<SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
//#include "printf.h"
RF24 radio(9,10);
const uint64_t w_pipe[2] = { 0xA0A0A0A0BBLL, 0xA0A0A0A0CCLL };
const uint64_t r_pipe[2] = { 0xF0F0F0A077LL, 0xF0F0F0A055LL };
struct PICO
{
int power1, power2;
boolean sh1,sh2,sh3,sh4,ma;
};
struct POCI
{
int power1,power2;
boolean ss1,ss2,ss3,ss4,ma;
};
PICO c2;
POCI p2;
int panelpower;
byte sz=sizeof(POCI);
void setup()
{
//Serial.begin(9600);
//Serial.println(" This is the Panel 2 \n");
//Serial.flush();
pinMode(2,OUTPUT);
digitalWrite(2,HIGH);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
//Setting up the nRF24L01 chip
radio.begin();
radio.setRetries(15,15);
radio.setPayloadSize(sz);
}
void loop()
{
//Writing pipe 2 for central node is reading pipe for Panel 2
radio.openReadingPipe(2,w_pipe[1]);
//Open the Central node reading pipe 1 for the Panel 1 to write the data to central node
radio.openWritingPipe(r_pipe[1]);
p2.power1=analogRead(A0);
p2.power2=analogRead(A1);
panelpower=p2.power1+p2.power2;
p2.ss1=digitalRead(5);
p2.ss2=digitalRead(6);
p2.ss3=digitalRead(7);
p2.ss4=digitalRead(8);
digitalWrite(2,HIGH);
//Start listening on the opened reading pipe for Central Node to send information
radio.startListening();
//Wait for the data to be available
while(!radio.available());
//Read the data sent from Central Node into c1
radio.read(&c2,sz);
//Stop listening to get ready to write back to central node
radio.stopListening();
//Write the data available with Panel 2 to Central Node
radio.write(&p2,sz);
digitalWrite(2,LOW);
}
The central node is an Arduino UNO clone, and the panels are Atmega8 based homemade Arduino boards.
Now coming to the problem
The code does not work the way I meant it to. When I use the same code on central node with the Panel1 or 2 related code disabled, it works. That is the code works for communication with single panel, when I add the second panel it doesn't work. It only prints "This is the Central Node" and stops working.
I think the homemade boards are fine as I tested all possible single(Central Node-Panel) combinations with all the home made boards and they worked individually.
I used the LED for debugging the found that it was hanging at
while(!radio.available());
What could be the problem?