Arduino freezes when I'm writing EEPROM



  • Hi. I'm trying to sa#define EEPROM_LOCAL_ADDRESS Insert Code Here

        (EEPROM_NODE_ID_ADDRESS+8)
    

    and saving like:

    saveState(EEPROM_LOCAL_ADDRESS,  (uint16_t)newPosition);
    

    but always after a saveState instruction, my program freezes.
    I don't know if mysensors uses all the EEPROM area and so my code generates a problem with it.
    Any help would be apreciated.
    thanks


  • Mod

    The addresses used by MySensors are documented here: https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/core/MySensorCore.h#L40
    The first address you can use is at EEPROM_LOCAL_CONFIG_ADDRESS



  • just tested.... when I replace EEPROM_LOCAL_ADDRESS with EEPROM_LOCAL_CONFIG_ADDRESS, I get some compile warnings, all with the same text:
    warning: large integer implicitly truncated to unsigned type [-Woverflow]
    but the code it's uploaded. But again it freezes by the first time that saveState(EEPROM_LOCAL_CONFIG_ADDRESS, 0) is used.


  • Admin

    @jhonnygolpe loadState() and saveState() take care of the addressing internally:

    saveState(pos, val);
    

    will write to EEPROM_LOCAL_CONFIG_ADDRESS + pos

    https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/core/MySensorCore.cpp#L301-L306


  • Mod

    @jhonnygolpe you get compiler warnings because your newPosition is 16 bits and saveState can only save 8 bits. The saveState definition:
    saveState(uint8_t pos, uint8_t value)

    You'll need to use a 8 bit value for newPosition, or something like @bisschopsr's function here to save all 16 bits. Just remember to increment pos by two (to accomodate for 2x8 bits) if you're saving multiple values.



  • Hi again. I've just tried your advises and, of course, I've solve my compilation errors. Thanks for your help !!
    But I can't write and rewrite the EEPROM correctly yet. It seems like I'm writing on a new address an y time, so in 2 or 3 times of savind data, arduino freezes.
    Now I've defined

    #define EEPROM_LOCAL_ADDRESS 2
    

    and after just 2 or 3 times of using

    saveState(EEPROM_LOCAL_ADDRESS,  (uint8_t)newPosition);
    

    arduino freezes.
    I've a test code, very similar, but without saving or reading from the EEPROM and works fine, so I suppose it's my fault using correctly those functions.
    Thanks again


  • Mod

    Strange. Have you tried on another Arduino? Could be that the eeprom is broken.

    How are you powering the Arduino? If I remember correctly, writing to the eeprom can be a bit voltage sensitive.

    On the other hand, the MySensors library writes to the eeprom every now and then and that seems to work.

    Could you post your entire sketch?

    You might also want to add a bunch of Serial.println statements to determine exactly where in the code the Arduino freezes. Is it at the saveState function, or at readState, or somewhere else?


  • Mod

    @jhonnygolpe What Arduino hardware are you using?
    Did you try writing to another address?



  • Well, i'm working with Arduino Uno, directly connected to the 220V/5v converter, so the power can't be the problem. I will enclose the code, and to help the reading of it, i'll try to explain it. On the Arduino there are connected a DHT22 sensor, and 2 relays and 2 buttons to control the blinds. I'm using my former motorized blinds, with its wall-mounted controllers (now one button to UP, another to DOWN). I want to use the previous controllers, and add remote control to move the blinds. So in the loop() function, I check if any of the buttons has been pressed, or if it's the time to send some info to the gateway. I use to have problems when I move down the blind remotely, with the receive function. With the debug activated, it seems to freeze after finishing the downAction() function...
    By the time, I'm gonna try the code with another arduino and another EEPROM address.
    As you will see I'm using another DEBUG class, very similar to the mysensors'oneSerialDebug.h blindsControlEL.ino


  • Mod

    @jhonnygolpe said:

    With the debug activated, it seems to freeze after finishing the downAction() function...

    Are you sure it exits the downAction? How about adding debug statements before and after each funtion call in loop()? That would help determine which fuction freezes.



  • [EDIT] I've found that I missed a return; call on the receive() function... and now it's working fine...
    Really I'm not sure where it fails. And yes, the next step it's write a lot of 'DEBUG'. I've tested changing the eeprom address with the same results, so I'm assuming the fault it's in my code 😟
    But always falls after a downAction movement...


  • Mod

    I think it hangs in this loop:

    while ( valueU == HIGH
                      && ((nowMillis - startMillis) / OPERATION_TIME) < 100
                      && (positionValue + ((nowMillis - startMillis) / OPERATION_TIME)) < 100
                      &&  (nowMillis - startMillis) < OPERATION_TIME );
    

    None of the variables in this loop will be updated while the loop is running, so it will run forever. I don't know what you wanted to do, but I don't think you wanted an infinite loop.



  • Not really... inside the do loop, that it's repeated anytime, updates valueU and nowMillis values, so it's not an infinite loop.
    I think it was a problem with the receive function... after calling

    
        else {
          moveBlind(dimvalue); 
          return;
        }
      }
    

    the moveBlind function I missed the return;
    Now it seems to work fine.... I'm sure I'll have some more errors, but i'm satisfied by the moment.
    Thanks a lot to everybody


  • Mod

    What's inside the while loop? To me it looks like nothing. There is no { after the while conditions, just a ;. The ; means that the while loop has no contents except re-evaluating the conditions.



  • It's a

    do { 
    ...t 
    } while();
    

    loop. It's similar to the while, but with this one you can control that the routine inside the do{...} it's always executed at least once.



  • Nevertheless I must review all my code.... It's been working for a time... But lately it freezes again


  • Mod

    Oh. Sorry. I wasn't aware of the do-while construct. Thanks for teaching me.


Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 2
  • 1
  • 6

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts