Strange Displaybug (SD1306)

Hi, im programming a short menu for my robot.

I have some do-loop, and i use the exact same loop for multiple variables. But for some variables it dont work. I can upload it to my Arduino Nano, but the arduino just freezes, doesnt even do the void setup().

i marked the lines where the error occurs with "------------------"
with "minDis: " it works
with "minDist: " it doesnt

Anyone got an idea why? i also tried clear the display (display.clearDisplay()) before this line, but didnt worked as well.

the part of my programm, where the error occurs:

#include <EEPROM.h>
#include <Adafruit_SSD1306.h>

.
.
.
  do { //minDist
    displayStandardSetting();
    button1 = digitalRead(button1Pin);
    button2 = digitalRead(button2Pin);
    button3 = digitalRead(button3Pin);
    if (button1 == 0) {
      minDist = minDist - 1;
      if (minDist < 0) {
        minDist = 255;
      }
      delay(delayTime);
    }
    if (button3 == 0) {
      minDist++;
      if (minDist > 255) {
        minDist = 0;
      }
      delay(delayTime);
    }
    if (button2 == 0) {
      EEPROM.update(minDistAdr, minDist);
      display.println("Eingestellt!");
      display.display();
      delay(500);
      break;
    }
//    display.println("Einstellen");
    display.clearDisplay();


    display.print("minDis: "); //<------ This works,-----------------------------------------------------------
//    display.print("minDist: "); <--- This doesnt work. I have no idea why--------------------------------


    display.fillRect(display.getCursorX(), display.getCursorY(), 127 - display.getCursorY(), 7, BLACK);
    display.println(minDist);
    display.println();
    display.println("Ok zum Bestätigen");
    display.display();
  } while (button1 == 1 || button2 == 1 || button3 == 1);

May I point you to here?

But it might be a memory issue but no way of telling for sure.

Bonus tip: arrays!

Bonus tip: F() macro

TheMemberFormerlyKnownAsAWOL:
Bonus tip: F() macro

what about serial.print()? dont get this tip :confused:

Alexander2019:
what about serial.print()? dont get this tip :confused:

Use the F() macro more.

whats the F() Macro?

i cant use Serial.print cause i`m printing in on an OLED which is connectet with my arduino.

Am im missing something. i dont really understand what you wanna explain :frowning:

display.println(F("Eingestellt!")); etc, etc

septillion:
May I point you to here?

But it might be a memory issue but no way of telling for sure.

Bonus tip: arrays!

Thanks, im now pretty sure it has to do something with the memory. now the menu works as standalone, but if i implement in the complete code it wont. and the other part works fine if i dont implement the menu.

The code is kinda big, and i didnt explained/commented (and only in German) much but i tried to kept it simple.
here is the code: 28865f17 - arduino nano programm

The code works if i only run the menuFunctions (line 398-404) in the void loop(){} and if i only run the do-loop (line 406-409).

but if i try to run them both, it doesnt work. The build uses 89% Progmem, 44% global Var, and 56% are free for lokal var.

im new to programming, so if you have any tipps and critics, please tell me

TheMemberFormerlyKnownAsAWOL:

display.println(F("Eingestellt!"));

etc, etc

thanks, i will look it up. never used the F() Macro before

but i thought the compiler knows automatically that "Text" wont change, since i didnt delareced which variable it is. (if it makes sense, my englisch is not the best)

Edit: I just replaced every text with F() and now my code works. Thanks for the tip. i dont understand F() completly but i will look furhter into it!

The F() macro ensures that literal string constants remain in flash memory, and don't get copied to RAM by crt0.
It sounds like the code you didn't post was teetering on the edge of instability due to RAM shortage.

It may well still be.

Is your Google broken? And was the "without all the code we don't really know"-hint really to vague?

septillion:
Is your Google broken? And was the "without all the code we don't really know"-hint really to vague?

Alexander2019:
....
The code is kinda big, and i didnt explained/commented (and only in German) much but i tried to kept it simple.
here is the code: 28865f17 - arduino nano programm
...

i did uplode my code, it was to big for the forum so i uploaded it here:

 --->    https://controlc.com/28865f17     <---

The F()-Macro was solving the problem. Wasn't aware that a string would be stored in the RAM.

and if i dont know what im looking for, a search engine wont help either.

You can always, as you read in "How to use the forum", upload the .ino :slight_smile:

And indeed, Googling something you don't know how to call can be hard. But if someone tells you about the "F macro" it's not that hard anymore. :wink:

if (t <= 300) {
    tone(buzzerPin, c3);
    return;
  }
  if (t <= 400 && t > 300) {
    noTone(buzzerPin);
    return;
  }

  if (t <= 600 && t > 400) {
    tone(buzzerPin, c3);
    return;
//...
  }

Easier withelse-if :wink:

if (t <= 300) {
    tone(buzzerPin, c3);
    return;
  }
  else if (t <= 400) {
    noTone(buzzerPin);
    return;
  }

  else if (t <= 600) {
    tone(buzzerPin, c3);
    return;
  }
//...

An of course arrays :slight_smile: With that, probably halve the code can go :slight_smile:

I must confess that i didnt read the "How to use the forum!"

After i wrote the music code, i realized it would be easier with else if, but it works and for now my code has bigger issues, but thanks for the tipp.

about the macro, thats true, but i was kinda confused, cause he linked it to the serial.print()-site.

Does an array that contains 5 int uses less that 5 ints? otherwise i would jsut use my setup variables, cause i can remember maximalVal better than Val[34].

Alexander2019:
Does an array that contains 5 int uses less that 5 ints? otherwise i would jsut use my setup variables, cause i can remember maximalVal better than Val[34].

No it does not use less space and of course you want (and should actually!) 'maximalVal' instead of 'val[34]'. But an array is the answer when you start numbering variables like 'button1', 'button2' and 'button3'. Make that into an array 'buttons' an tada, you can now even loop over them all!