to use interrupt as a counter while low power

hi;

i want to use interrupts as a counter, here my codes;

#include <SPI.h>
#include <RF24.h>
#include "LowPower.h"

#define CE 7
#define CSN 8

volatile bool counting = true;
volatile unsigned long events;
unsigned long startTime;
const unsigned long INTERVAL = 1000;  // 1 second

const uint64_t ad = 0xE8E8F0F0E1LL;

RF24 radio(CE, CSN);

void setup() {
    pinMode(8, OUTPUT);
    pinMode(7, OUTPUT);
    Serial.begin(115200);
    radio.begin();
    radio.setChannel(108);
    radio.setDataRate(RF24_250KBPS);
    radio.setPALevel(RF24_PA_MIN);
    radio.openWritingPipe(ad);
    attachInterrupt(digitalPinToInterrupt(2), eventISR, FALLING);
}

void eventISR() {
  if (counting) events++;
}

void SendToServer() {
  int _tmp = events;
  Serial.print("Sending... ");
  //sprintf(msg, "%ld", events);
  radio.write(&_tmp, sizeof(_tmp));
  Serial.println(events);
}

void loop() {
  LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);

  if (counting) {
    counting = false;
    SendToServer();
  }
  
  noInterrupts ();
  events = 0;
  startTime = millis ();
  EIFR = bit (INTF0);  // clear flag for interrupt 0
  counting = true;
  interrupts ();
}

void loop2() {
  if (counting) {
    if (millis () - startTime < INTERVAL) return;
    counting = false;
    SendToServer();
  }
  
  noInterrupts ();
  events = 0;
  startTime = millis ();
  EIFR = bit (INTF0);  // clear flag for interrupt 0
  counting = true;
  interrupts ();
}

“void loop2()” works perfect. serial output like;
"
Sending… 0
Sending… 0
Sending… 0
Sending… 10
Sending… 20
"

but when i use “void loop()”, serial output like;
"
Sending… j
Sending… j
Sending… j
Sending… j
Sending… j
"

what is this “j” anyway.

so, just like i sad, i want to use interrupts as a counter while low power. If this is not possible, what would you recommend for low-power counter. I want to run this project with battery.

You have three things going on - interrupts from some undefined source triggering an ISR, a radio being used to send data, and narcolepsy.

Get rid of TWO of them, until you are sure that the other one works.

volatile unsigned long events;

  int _tmp = events;

Does THAT make any sense?

Hi;

Thank you, i will try this.

I have another question about using interrupts while low power. I assume that external interrupts always wake up arduino right?

I assume that external interrupts always wake up arduino right?

Wrong. Some kinds of external interrupts will wake a sleeping Arduino, not all kinds, from some levels of sleep. You'd have to test whether your type of external interrupt wakes your Arduino from your sleep level.