Problem with the use of a struct

Hello,

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.

void readSwitches(){
  // Frontdoor
  Button[0].state[0] = digitalRead(di_SW_LMP_FRONTDOOR);
  Button[0].state[1] = LOW;
  Button[0].state[2] = LOW;
  // Bedroom
  Button[1].state[0] = digitalRead(di_SW1_LMP_BEDROOM);
  Button[1].state[1] = digitalRead(di_SW2_LMP_BEDROOM);
  Button[1].state[2] = digitalRead(di_SW3_LMP_BEDROOM);
  // Bed Left
  Button[2].state[0] = digitalRead(di_SW_LMP_BEDLEFT);
  Button[2].state[1] = LOW;
  Button[2].state[2] = LOW;
  // Bed Right
  Button[3].state[0] = digitalRead(di_SW_LMP_BEDRIGHT);
  Button[3].state[1] = LOW;
  Button[3].state[2] = LOW;
  // Kitchen
  Button[4].state[0] = digitalRead(di_SW1_LMP_KITCHEN);
  Button[4].state[1] = digitalRead(di_SW2_LMP_KITCHEN);
  Button[4].state[2] = LOW;
  // Cook
  Button[5].state[0] = digitalRead(di_SW_LMP_COOK);
  Button[5].state[1] = LOW;
  Button[5].state[2] = LOW;
  // Flush
  Button[6].state[0] = digitalRead(di_SW_LMP_FLUSH);
  Button[6].state[1] = LOW;
  Button[6].state[2] = LOW;
  // Bathroom
  Button[7].state[0] = digitalRead(di_SW_LMP_BATHROOM);
  Button[7].state[1] = LOW;
  Button[7].state[2] = LOW;
  // Backdoor
  Button[8].state[0] = digitalRead(di_SW_LMP_BACKDOOR);
  Button[8].state[1] = LOW;
  Button[8].state[2] = LOW;
}

Any suggestions where things goes wrong?

Can you post the entire code?

what is not fine?

what is the problem?

please describe what Button_type is expected to do?

it looks like a 9 element array of struct, each struct containing 2 4 element arrays of states

results from running your code (with fewer and simpler symbols)

dump:
  0: ( 0  1) ( 0  2)
  1: ( 0  3) ( 0  4)
  2: ( 0  5) ( 0  6)
  3: ( 0  7) ( 0  8)
dump:
  0: ( 1  1) ( 2  2)
  1: ( 3  3) ( 4  4)
  2: ( 5  5) ( 6  6)
  3: ( 7  7) ( 8  8)
const int Nsw   = 2;   
const int Nlamp = 4;     

struct Button
{
  int st    [Nsw];
  int stLst [Nsw];
};

Button buts  [Nlamp] = {
    {{ 1, 2 }},
    {{ 3, 4 }},
    {{ 5, 6 }},
    {{ 7, 8 }},
};

// -----------------------------------------------------------------------------
void
readSwitches ()
{
}

// -----------------------------------------------------------------------------
void setupSwitches ()
{
  readSwitches();

  for (int i = 0; i < Nlamp; i++) {
    for (int j = 0; j < Nsw; j++) {
      buts [i].stLst [j] = buts [i].st [j];
    }
  }
}

// -----------------------------------------------------------------------------
#include <stdio.h>

void dump ()
{
    printf ("dump:\n");
    for (int i = 0; i < Nlamp; i++) { 
        printf ("  %d:", i);
        for (int j = 0; j < Nsw; j++)
            printf (" (%2d %2d)", buts [i].stLst [j], buts [i].st [j]);
        printf ("\n");
    }
}


// -----------------------------------------------------------------------------
int
main () 
{
    dump ();
    setupSwitches ();
    dump ();
    return 0;
}

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).

void printTrigger(int objnr, int btnNr){
  Serial.print("Button pressed => Object: ");
  Serial.print(objnr); Serial.print(" Button: "); Serial.print(btnNr); Serial.print(" Value: ");
  Serial.println(Button[objnr].state[btnNr]);
}

void trigger0(){ // To call this void send from Nextion's component's Event:  printh 23 02 54 00
  Button[0].state[3] = !Button[0].state[3];
  printTrigger(0, 3);
}

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.

HMI.updateEEPROMdata(dataEEPROM, ARRSIZE_EEPROM);

Are writing to the eeprom memory on the Nextion or the eeprom on the Mega?

again

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.

1 Like

The EEPROM on the mega.

Thanks a lot, this was exactly the problem.
Everything is running fine now.
fyi: the eeprom is holding the setting of my installation.

1 Like

@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.

1 Like

Ok thank you. Next time I will post the entire code.

1 Like