I come from a .Net background, typically I would 'lock' operations on an integer when I have thread-safe requirements. I'm not sure that applies to my sketch here? Could I potentially bypass an increment from one of my interrupt pins?
In summary, I have 2 interrupt pins - I want to increment a count which (every 5 seconds) writes via serial and resets.
I'm doing this to, kind of, avoid writing serial from within an interrupt - since I've read it's strongly discouraged.
#include <Controllino.h>
const byte interruptPin0 = CONTROLLINO_IN0;
const byte interruptPin1 = CONTROLLINO_IN1;
int counter1 = 0;
int counter2 = 0;
unsigned long previousMillis = 0;
unsigned long interval = 5000;
int a = 60;
char buffer[256];
void setup()
{
pinMode(interruptPin0, INPUT_PULLUP);
pinMode(interruptPin1, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin0), pin1Fired, RISING);
attachInterrupt(digitalPinToInterrupt(interruptPin1), pin2Fired, RISING);
Serial.begin(9600);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
sprintf(buffer,"A%d",counter1);
Serial.println(buffer);
sprintf(buffer,"D%d",counter2);
Serial.println(buffer);
counter1 = counter2 = 0;
}
}
void pin1Fired()
{
counter1++;
}
void pin2Fired()
{
counter2++;
}