Im having problems using a ESP32 as a data adquisition board.
Im using this ESP32-WROOM-32 on a ESP32 DEVKIT V1 from "doit" to send me an array of 16 characters consisting of the values from a MMA7361 accelerometer, im sensing X and Y axes from both and each one is a 4 digit number, that's why the 16 digits.
Thing is, the watchdog timer or something keeps messing up, rebooting the board after 2 messurements. I allready used Chat GPT to modify the code a bit, changing the timer clock and changing the variables so that they are local instead of global.
I leave the code up next:
#define XA_AXIS A0
#define YA_AXIS A3
#define XB_AXIS A6
#define YB_AXIS A7
hw_timer_t *My_timer = NULL;
int xa_sum = 0, ya_sum = 0, xb_sum = 0, yb_sum = 0;
int counter = 0;
int counter_local = 0;
void IRAM_ATTR onTimer() {
int xa_sum_local = 0;
int ya_sum_local = 0;
int xb_sum_local = 0;
int yb_sum_local = 0;
xa_sum_local += analogRead(XA_AXIS);
ya_sum_local += analogRead(YA_AXIS);
xb_sum_local += analogRead(XB_AXIS);
yb_sum_local += analogRead(YB_AXIS);
counter_local++;
if (counter_local == 5) {
int xa_mean = xa_sum_local / 5;
int ya_mean = ya_sum_local / 5;
int xb_mean = xb_sum_local / 5;
int yb_mean = yb_sum_local / 5;
// Format the mean values with leading zeros and a fixed width of 4
char buf[17]; // 16 digits plus a null terminator
sprintf(buf, "%04d%04d%04d%04d\n", xa_mean, ya_mean, xb_mean, yb_mean);
// Write the string to the serial port
Serial.write(buf, 16);
xa_sum_local = ya_sum_local = xb_sum_local = yb_sum_local = 0;
counter_local = 0;
}
}
void setup() {
Serial.begin(115200);
My_timer = timerBegin(0, 80, true);
timerAttachInterrupt(My_timer, &onTimer, true);
timerAlarmWrite(My_timer, 50000, true);
timerAlarmEnable(My_timer);
}
void loop() {}
And here is what the serial terminal is returning, the tipical error im getting:
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1184
load:0x40078000,len:13220
ho 0 tail 12 room 4
load:0x40080400,len:3028
entry 0x400805e4
0390034804050343 <------(this is the array i want to get)
Stack smashing protect failure!
abort() was called at PC 0x400da308 on core 1
Backtrace: 0x40083539:0x3ffbf19c |<-CORRUPTED
ELF file SHA256: 672e7622e5a84396
Rebooting...
I dont think is a timer issue, because when i modify the timer to gather data every 1 second, it still shows the same error.
Any help would be apreciatted, im kind of new to the ESP32 but im lead to belive that it's faster and more usefull to what im trying to do here, witch is to get the data from the accelerometers at about 20-50 times a second, the faster the better.