Oled menus with adjustable perimeters

Hello guys getting used to this arduino environment and enjoying myself ! :slight_smile:

first thing my currant project is a gauge with a pid control and oled with menus i think i have added the menus correctly but I’m not quite sure how i would changer the variables ? ie boost target and the pid perimeters ? i think i am running out of memory space on the nano can my code be streamlined take up less space ?

i am having to attach the full info file as its to big to fit in code brackets ?

boost_1.3_I2C_oled_menu.ino (9.95 KB)

byte sensorArray[128];

  for (count = 0; count <= 128; count++) //zero all elements
  {
    sensorArray[count] = 0;
  }

Writing 129 values into a 128 element array is a REALLY bad idea.

  for (count = 80; count >= 2; count--) // count down from 160 to 2

That is NOT what that code does. If you are going to have useless comments, you MUST make them accurate useless comments.

Comments should precede the code, and should explain WHY you are doing something, not WHAT you are doing. Anyone can see what the code is doing. Why it is doing that is a mystery.

  for (count = 55; count <= 128; count++ )
  {
    if (filled == false)
    {
      display.drawPixel(count, 71 - sensorArray[count - 55], WHITE);
    }
    else
      display.drawLine(count, 1, count, 71 - sensorArray[count - 55], WHITE); 
  }

  for (count = 80; count >= 2; count--) // count down from 160 to 2
  {
    sensorArray[count - 1] = sensorArray[count - 2];
  }

Magic numbers suck. 71? 55? 80?

Inconsistent use of curly braces sucks. Why does the if statement body have curly braces when the else statement body does not? Both bodies should have curly braces.

  for (count = 80; count >= 2; count--) // count down from 160 to 2
  {
    sensorArray[count - 1] = sensorArray[count - 2];
  }
//JUMP TO DEFAULT IF NO CLICK IS DETECTED
if (millis() >= (lastmillis + maxtime))
{
    pic = 0;
}

Inconsistent indenting sucks big time. There is NO excuse for THAT. Tools + Auto Format makes is easy to correct your being too lazy to properly indent code. Do that before you make a fool of yourself posting sloppy code.

  if (970 < boostmbar ) {
    display.setTextSize(1);
    display.setCursor(97, 14);
    display.println("BOOST");
  }
  else if (970 > boostmbar ) {
    display.setTextSize(1);
    display.setCursor(97, 14);
    display.println("VAC");
  }

What makes 970 special, so that nothing needs to be done when boostmbar is 970?

Your camel needs water NOW!

The “menu” display should happen in a function called from loop(), NOT in loop().

Calling header() in every block is pointless. Call it ONCE, then do the stuff that depends on the value in pic, then do the rest of the stuff that doesn’t.

Use the F() macro to keep literal text out of SRAM.

  display.setCursor(0,11);  display.print ("TAGET");

TAGET?

A switch statement beats all those if statements.

void doubleclick(){
  lastmillis = millis();
  if (pic == 11 || pic == 21 || pic == 31) pic=1;
  if (pic == 12) pic=2;
  if (pic == 13) pic=3;
  if (pic == 14) pic=4;
  if (pic == 15) pic=5;
}

So, pic gets assigned the remainder of dividing by 10. Using the modulo operator would reduce that function to two lines of code.

void doubleclick()
{ // Down here, where the damned thing belongs
  lastmillis = millis();
  pic %= 10;
}
  if (pic>0 & pic<10)

&& and & are NOT the same thing. You almost certainly do not want & there.

void refresh()
{
  display.display();
  delay(00);
  display.clearDisplay();
}

WTF? Display whatever has been buffered. Then, wait some REALLY short amount of time. Then, clear the screen.

Inconsistent indenting sucks big time. There is NO excuse for THAT. Tools + Auto Format makes is easy to correct your being too lazy to properly indent code. Do that before you make a fool of yourself posting sloppy code.

i appreciate the feed back but please note I'm a mechanical engineer tying to get involved in a new hobby most of this "crap code" is a mixture of multiple courses i have just attempted to patch them in the right place i am learning as I'm sure you did once ?

enven in mechanical engineering we are not this hostile toward amateurs i respect that there are a load of non topic searchers on here annoying you but as you can see from the patching i have at least attempted my research ?

please note I'm a mechanical engineer

So am I. Despite that, I can recognize poorly indented code, and I can do something about it.

In my mind, the issue is that if you can't properly indent code, so that you can see the structure of the program, you will have an equally hard time thinking logically enough to be a programmer.

That comment was only a small part of what I posted. Apparently, you decided to ignore the rest of what I said. Oh well, I'm over it and moving on.