NRF24L01 resets value to 0

I have a transmitter that sends the total count of state-changes (HIGH/LOW) and send it over to a Receiver. but the receiver keeps resetting to 0.

Receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int counter1a = 0;



RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};


void setup() {  
  Serial.begin(9600);
  radio.begin();
  radio.setChannel(933);
  radio.enableDynamicPayloads();
  radio.enableAckPayload();
  radio.openWritingPipe(addresses[0]); // 00001
  radio.openReadingPipe(1, addresses[1]); // 00002
  radio.setPALevel(RF24_PA_MIN);
}
void loop() {
  delay(5);
  radio.startListening();
  if ( radio.available()) {
    while (radio.available()) {
      counter1a++;
      radio.read(&counter1a, sizeof(counter1a)); 
      Serial.println(counter1a);
    }    
  }
}

Transmitter

#define Sensor 2
int counter1a = 0;
int state;
int laststate = HIGH;
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.enableDynamicPayloads();
  radio.enableAckPayload();
  radio.setChannel(933);
  radio.openWritingPipe(addresses[1]); // 00002
  radio.setPALevel(RF24_PA_MIN);
}
void loop() {
  delay(5);
  radio.stopListening();
  bool Detection = digitalRead(Sensor);
    if(Detection == HIGH)
    Serial.println("Motion detected !!");
    if(Detection == LOW)
    Serial.println("Clear");  
    int state = digitalRead(Sensor);
       if ( state != laststate) {
          if (Detection == HIGH)
                
       //was motion detected
       counter1a=counter1a+1;
       Serial.println(counter1a);
             } 
       laststate = state;
    delay(300); 
  radio.write(&counter1a, sizeof(counter1a));
  delay(500);
}

Serial Monitor

Put

Serial.println("Beginning...");

in your setup function so you can see if the Arduino is being reset or not.

Try removing this from the Rx program - it is completely unnecessary. The previous IF is all that is required.

while (radio.available()) {

If that does not solve the problem try the examples in this Simple nRF24L01+ Tutorial

…R

Incrementing a counter and then overwriting it by the incoming packet seem not so clever to me.

     counter1a++;
      radio.read(&counter1a, sizeof(counter1a));

Delta_G:
Put

Serial.println("Beginning...");

in your setup function so you can see if the Arduino is being reset or not.

I tried and it does not reset

Robin2:
Try removing this from the Rx program - it is completely unnecessary. The previous IF is all that is required.

while (radio.available()) {

If that does not solve the problem try the examples in this Simple nRF24L01+ Tutorial

…R

I based from your simplerx/tx example and heres my code same problem.

Rx

// SimpleRx - the slave or the receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define CE_PIN   7
#define CSN_PIN  8
int count = 0;
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN);
bool newData = false;

//===========

void setup() {

    Serial.begin(9600);
    Serial.println("Beginning...");

    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

//=============

void loop() {
    getData();
    showData();
}

//==============

void getData() {
    if ( radio.available() ) {
        radio.read(&count, sizeof(count) );
        Serial.print(count);
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.println("Data received ");
        newData = false;
    }
}

Tx

// SimpleTx - the master or the transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define Sensor 2
#define CE_PIN  7
#define CSN_PIN 8
int state;
int laststate = HIGH;
int count = 0; 
const byte slaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

void setup() {

    Serial.begin(9600);
    Serial.println("SimpleTx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
}

//====================
void loop() {
    currentMillis = millis();
    if (currentMillis - prevMillis >= txIntervalMillis) {
        send();
        prevMillis = millis();
    }
}
//====================
void send() {
    bool Detection = digitalRead(Sensor);
          if(Detection == HIGH)
          Serial.println("Motion detected !!");
          if(Detection == LOW)
          Serial.println("Clear");
    
           int state = digitalRead(Sensor);
            if ( state != laststate)
     {
            if (Detection == HIGH)
             count=count+1;
       Serial.println(count);
     } 
 laststate = state;
 delay(300);
   
 {                  
    bool rslt = radio.write( &count, sizeof(count) );
    
    Serial.print("Data Sent ");
    Serial.print(count); 
    if (rslt) {
        Serial.println("  Acknowledge received");
    }
    else {
        Serial.println("  Tx failed");
    }
}
}

Whandall:
Incrementing a counter and then overwriting it by the incomming packet seem not so clever to me.

      counter1a++;

radio.read(&counter1a, sizeof(counter1a));

a blunder from a noob.

Eahta:
I based from your simplerx/tx example and heres my code same problem.

Slow down a little.

Did you try my examples without any changes? Did they work? If not, get that working first.

Next, why have you code for reading a sensor in a function for sending wireless data?

void send() {
    bool Detection = digitalRead(Sensor);
          if(Detection == HIGH)
          Serial.println("Motion detected !!");
          if(Detection == LOW)
          Serial.println("Clear");
    
           int state = digitalRead(Sensor);
            if ( state != laststate)
     {
            if (Detection == HIGH)
             count=count+1;
       Serial.println(count);
     } 
 laststate = state;
 delay(300);

Put your code into its own function. That way you can test each part separately and identify where the problem is. Have a look at Planning and Implementing a Program

...R

Robin2:
Slow down a little.

Did you try my examples without any changes? Did they work? If not, get that working first.

Next, why have you code for reading a sensor in a function for sending wireless data?

void send() {

bool Detection = digitalRead(Sensor);
          if(Detection == HIGH)
          Serial.println("Motion detected !!");
          if(Detection == LOW)
          Serial.println("Clear");
   
          int state = digitalRead(Sensor);
            if ( state != laststate)
    {
            if (Detection == HIGH)
            count=count+1;
      Serial.println(count);
    }
laststate = state;
delay(300);




Put your code into its own function. That way you can test each part separately and identify where the problem is. Have a look at [Planning and Implementing a Program](http://forum.arduino.cc/index.php?topic=261445.0)

...R

the examples w/o changes worked, the wireless data being sent is the no. of changes in state of the sensor. I'll try testing the functions seperately.

Eahta:
the wireless data being sent is the no. of changes in state of the sensor.

I was aware of that when I wrote my Reply #6. My comment was about the inappropriate place where you put the code to count the changes.

...R