Hello all. I am having a problem that made me create an account just to post this question.
In essence, I have a hard-coded lookup table (an array) of a sine wave that I would like to access within an interrupt.
Whenever I try to read a value from the array while inside an interrupt, the result is not the correct indexed element. The result is not even an element of the array at all!
I am using a library, the TimerOne library, but I believe it is just a wrapper for easy pwm and timer setups.
I have tried almost every variation of my lookup table variable signature that I can think of. I have tried surrounding different blocks of code with the noInterrupts() and interrupts() method calls. But reading the array still returns a wrong value.
I have no idea what the reasoning could be. Can someone explain why this might be happening?
#include <TimerOne.h>
#define o_pin 10
//I have tried many variants for the variable signature but non seem to work
const static volatile uint8_t my_lut[100] PROGMEM =
{
544, 576, 607, 639, 670, 700, 729, 758, 786, 812,
838, 862, 884, 906, 925, 943, 960, 974, 987, 998, 1007,
1014, 1019, 1022, 1023, 1022, 1019, 1014, 1007, 998, 987, 974,
960, 943, 925, 906, 884, 862, 838, 812, 786, 758, 729,
700, 670, 639, 607, 576, 544, 511, 479, 447, 416, 384,
353, 323, 294, 265, 237, 211, 185, 161, 139, 117, 98,
80, 63, 49, 36, 25, 16, 9, 4, 1, 0, 1,
4, 9, 16, 25, 36, 49, 63, 80, 98, 117, 139,
161, 185, 211, 237, 265, 294, 323, 353, 384, 416, 447,
479, 512
};
volatile uint8_t temp = 0;
void setup() {
Serial.begin(19200);
pinMode(o_pin,OUTPUT);
Timer1.initialize(10000);
Timer1.attachInterrupt(my_int);
}
void loop() {
noInterrupts(); //Have tried with this line and without this line
Serial.println(temp);
interrupts(); //Have tried with this line and without this line
}
void my_int(void){
noInterrupts(); //Have tried with this line and without this line
temp = my_lut[0];
interrupts(); //Have tried with this line and without this line
}
Here are some print results for the above code. I try to access the 0th element (my_lut[0]):
01:23:25.377 -> 0
01:23:25.377 -> 0
01:23:25.377 -> 0
01:23:25.377 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.411 -> 0
01:23:25.445 -> 0
01:23:25.445 -> 0
01:23:25.445 -> 0
01:23:25.445 -> 0
01:23:25.445 -> 0
01:23:25.445 -> 0
And here are some print results from another (random) index of my_lut. In this case I tried reading the 52nd element (my_lut[52]):
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2
01:24:28.945 -> 2