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?
-
Is it maybe running out of memory? I am not familiar with network.read but does this memory get free'd ?Manatok– Manatok2015年06月03日 12:28:58 +00:00Commented Jun 3, 2015 at 12:28
-
Why network.read should use memory? The only thing that uses memory are the variables and structures. And even if it used, shouldn't the arduino stop completely?Christos Mitsis– Christos Mitsis2015年06月03日 12:32:06 +00:00Commented Jun 3, 2015 at 12:32
-
I think it would have to buffer that input and if it is allocating memory each time and not cleaning up, maybe its running out. But this is just a guess. ..."If you run out of SRAM, your program may fail in unexpected ways; it will appear to upload successfully, but not run, or run strangely"...arduino.cc/en/Tutorial/MemoryManatok– Manatok2015年06月03日 12:54:23 +00:00Commented Jun 3, 2015 at 12:54
-
1Try resetting the nrfs with the below answer: arduino.stackexchange.com/a/54604/35167paulplusx– paulplusx2018年07月20日 06:29:10 +00:00Commented Jul 20, 2018 at 6:29
-
1See this. arduino.stackexchange.com/questions/55042/…paulplusx– paulplusx2018年08月05日 16:15:35 +00:00Commented Aug 5, 2018 at 16:15