I'm getting stuck with assigning values to my struct.
Calling this piece of program results in not receiving events anymore from my nextion display.
It seems like the program get stuck or slowing down a lot.
I declared the next struct in my core.h file:
// Declaration of constants
const int ARRSIZE_SWITCHES = 4;
const int ARRSIZE_LAMPS = 9;
// Declaration of structures
typedef struct
{
int state[ARRSIZE_SWITCHES];
int lastState[ARRSIZE_SWITCHES];
} Button_type;
// Declaration of variables
int dataEEPROM[ARRSIZE_EEPROM]; //maxmem/offset
Button_type Button[ARRSIZE_LAMPS] = {};
Everything runs fine until the call to the SetupSwitches() function in my sketch:
void SetupSwitches(){
// Get current switch states
readSwitches();
// Init the button state
for (int i = 0; i < ARRSIZE_LAMPS; i=i+1) {
for (int j = 0; j < ARRSIZE_SWITCHES; j=j+1) {
Button[i].lastState[j] = Button[i].state[j];
}
}
}
The call to the readSwitches() function causes the same problem.
Sorry I wasn't clear.
Unfortunately your code runs into the same problem, despite both dumps are printed right.
I have a nextion display connected to my mega via serial port 1.
When i do NOT run the setupSwitches() function than every time i push a button on my screen this command is send to my mega. (the rx light on my mega board flashes).
This command is handled by the easynext library by calling the associated trigger function.
In this case Trigger(0).
This works fine for all buttons unless i do NOT call the setupSwitches() function.
When i DO call this function the pressing button command of my display will not be received anymore through serial port. (the rx light on my mega board does NOT flash anymore).
This means that when I press the button about 10 times in a row, it is maybe captured once.
It seams that after the call to the setupSwitches() function the mega get stuck somewhere.
When i do NOT run the for next loop in my setupSwitches() function, thus only calling the readSwitches() function i will run into the same problem.
In my opinion it has something to do the way the values are assigned to the Button structure.
I really get stuck here!
The code you posted does not have any obvious problems, it’s likely something else is accessing the same memory as the struct and getting corrupted when you write to the struct.
Thanks, that was exactly the right direction!
When i try to update the EEPROM data in the instance of my display class it goes wrong.
This means when i call the HMI.updateEEPROMdata(dataEEPROM, ARRSIZE_EEPROM) function.
Arduino Sketch:
#include "display.h" // Include display library
const int ARRSIZE_EEPROM = 32; // 4096 bytes max for ATmega2560
int dataEEPROM[ARRSIZE_EEPROM];
Display HMI(Serial1);
void setup() {
Serial.begin(9600); // initialize serial communication at 9600 bits per second
HMI.Setup();
getDataEEPROM();
HMI.updateEEPROMdata(dataEEPROM, ARRSIZE_EEPROM);
}
Display library:
int EEPROMdata[32];
void Display::updateEEPROMdata (int* _eepromdata, int lenght){
for (int i = 0; i <= lenght; i = i+1) {
EEPROMdata[i] = _eepromdata[i];
}
}
I really need that piece of code so any help on how to solve this memory problem will be appreciated.
The array boundary is exceeded here when i is equal to lenght:
i <= lenght
Because in C++ the index of the first element of an array is 0, the last element of the array will be EEPROMdata[lenght - 1] and _eepromdata[lenght - 1] so the condition needs to be:
for (int i = 0; i < lenght; i = i+1) {
}
Hopefully this EEPROM array is NOT intended to hold the content of the Button array? Just wondering as it appears to be declared in the mix giving the impression it might be related, but we don't see the rest of the code.
@settecolli Observation: had you posted your entire code to begin with, someone would likely have posted pointing that out immediately. Remember, for every response you get, there are usually three or more other readers who won't engage unless you provide complete code, because context is essential.