I made a sketch that receives an rgb code from a remote Arduino using nrf24l01 and while it works for the first receivings then it stops working. I placed a testing Serial.println
in the loop to test if Arduino stops to work completely or it doesn't receive any other data. Finally, I understood that it continues to work but it doesnt receive more data. Bellow is my code:
#include <RF24Network.h>
#include <RF24Network_config.h>
#include <Sync.h>
#include <TimerOne.h>
#include <SPI.h>
//#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(8,10);
RF24Network network(radio);
volatile int i=0;
volatile int d=0; // Variable to use as a counter
volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin= 4; // Output to Opto Triac pin
int dim = 0; // Dimming level (0-128) 0 = ON, 128 = OFF
int freqStep = 65;
struct payload_rgb
{
unsigned int red;
unsigned int green;
unsigned int blue;
};
struct payload_lightuv
{
unsigned int light;
unsigned int uv;
};
RF24NetworkHeader header;
payload_rgb payloadrgb;
payload_lightuv payloadl;
void setup() {
// put your setup code here, to run once:
pinMode(AC_pin, OUTPUT);// Set AC Load pin as output
pinMode(9, OUTPUT);// Set AC Load pin as output
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
attachInterrupt(0, zero_cross_detect, RISING); // Choose the zero cross interrupt # from the table above
Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
Timer1.attachInterrupt(dim_check, freqStep);
Serial.begin(115200);
radio.begin();
network.begin(90,01);
}
void loop() {
network.update();
while ( network.available() )
{
Serial.println("Rec");
network.peek(header);
if(header.type=='r'){
network.read(header,&payloadrgb,sizeof(payloadrgb));
analogWrite(6,payloadrgb.red);
analogWrite(9,payloadrgb.green);
analogWrite(5,payloadrgb.blue);
}
if(header.type=='l'){
network.read(header,&payloadl,sizeof(payloadl));
dim=payloadl.light;
}
}
Serial.println("!"); //this is the test println
}
void zero_cross_detect() {
zero_cross = true; // set the boolean to true to tell our dimming function that a zero cross has occured
i=0;
digitalWrite(AC_pin, LOW); // turn off TRIAC (and AC)
}
void dim_check() {
if(zero_cross == true) {
if(i>=dim) {
digitalWrite(AC_pin, HIGH); // turn on light
i=0; // reset time step counter
zero_cross = false; //reset zero cross detection
}
else {
i++; // increment time step counter
}
}
}}
Additionally, this Arduino controls an ac dimmer. Bu I have not done it yet. How can I fix the problem without removing any feature?