How can I verivy RAM problem in atmega 328p

132 Views Asked by At

I have been banging my head for several hours because I have a rare problem. I suspect I have a memory issue

I have a pcb with an atmega328p in DIP format and an I2C OLED display with 128x64 pixels. At first I was using the adafruit library but I quickly met stability issues when my RAM reached about 48%. I learned that the adafruit library uses more RAM than my compiler (arduino cli) would tell me..

So I migrated to the <U8x8lib.h> library. Which works signaficantly better.

But now my program starts to grow I again face strange behaviour.

The 'weird' part is happening in this switch-case

void updateLCD() 
{
    clearDisplay() ;
    delay(10);
    printNumberAt(5,5,2,mode) ;
    delay(1000);

    switch( mode )
    {  
    case locos :
        drawSpeed( speed ) ;
        drawFunctions() ;
        printAt(0,0, F("Loco:")) ; printNumberAt( 7, 0, 3, currentAddress ) ;
        break ;

    case points :
        printAt(0, 0, F("POINT #") ) ;
        printNumberAt( 7, 0, 3, pointAddress ) ;
        uint8_t bit      = pointAddress % 8 ;
        uint8_t group    = pointAddress / 8 ;

        bool state = !bitRead( pointStates[ group ], bit) ;
        if( !state )  printCustom( 13, 0, 0, straight ) ;
        else          printCustom( 13, 0, 0,   curved ) ; 
        break ;   

    case gettingAddress:
        printAt(0, 0, F("ENTER ADDRESS") ) ;
        break ;

    case gettingSlot:
        printAt(0, 0, F("ENTER SLOT") ) ;
        break ;
    
    case pointStreets :
        printAt(0, 0, F("setting street" ) ) ;
        printAt(0, 1, F("enter number" ) ) ;
        break ;

    case locoSlots:
        drawFunctions() ;
        drawSpeed( speed) ;
        printAt(0,0, F("Loco Slot")) ; printNumberAt( 9, 0, 3, slot ) ;
        printDescription( eepromLoco.name, 1 );
        break ;

    case programs:
        printAt(0, 0, F("PROGRAM MODE") ) ;
        printAt(0, 1, F("CHANNEL #") ) ;
        printNumberAt(5,2, 4, channel ) ;
        uint8 state1 = program1.getState() ;
        if( state1 == recording ) printAt(0, 3, F("recording") ) ;
        if( state1 == playing )   printAt(0, 3, F("playing") ) ;
        if( state1 == finishing ) printAt(0, 3, F("finishing") ) ;
        if( state1 == idle )      printAt(0, 3, F("idle") ) ;
        break; 
    }
}

What is happening. Each and every last case has worked without flaw. And now only the one or two top most cases work. If I change the order of these cases I can let some texts 'dissapear' and others 'reappear'.

To verify that the switch variable mode was not containing an invalid value, I added these lines.

    delay(10);
    printNumberAt(5,5,2,mode) ;
    delay(1000);

Mode is always correct in al situations... yet the text of the following case.. it is not to be seen.

My best guess is that my heap and stack are colliding. From what I read, that can cause rather vague symptoms like I am having now.

As my program is this big and the bug is related to the OLED display with which I had earlier stability issues. I think I may be on the right track.

Sketch uses 22554 bytes (69%) of program storage space. Maximum is 32256 bytes.
Global variables use 1414 bytes (69%) of dynamic memory, leaving 634 bytes for local variables. Maximum is 2048 bytes.

Are there methods to verify or detect that heap and stack are colliding? What more could be propable causes?

0

There are 0 best solutions below