Let's say I want to modify a global variable from inside an interrupt service routine. I must ensure that any variables I modify are volatile. Does it work to use a volatile pointer to data I want to modify if the data itself is not volatile?
In my case the data is a struct, but does the answer change if the data is a class, array, or just a variable?
#include <Arduino.h>
#define PIN_INTERRUPT 2
typedef struct MyStruct
{
int a_field;
} MyStruct;
MyStruct struct_data;
int array_data[32];
volatile bool _flag_ISR = false;
volatile MyStruct *_struct_ISR = &struct_data;
void myISR()
{
// just some example stuff
int read = digitalRead(PIN_INTERRUPT);
_struct_ISR->a_field = read;
_arr_data_ISR[4] = read;
_flag_ISR = true;
}
void setup()
{
attachInterrupt(digitalPinToInterrupt(PIN_INTERRUPT), myISR, CHANGE);
}
void loop()
{
if (_flag_ISR)
{
// do processing here...
_flag_ISR = false;
}
}