Code Minimisation

Hi All,

My question is one of code minimisation rather than a code issue. Basically I am assembling a guitar effects pedal for a friend who has sight issues and would struggle to read knob positions on a standard pedal so I am introducing a large tft screen with ascending bar graph to represent each potentiometer value. With all effects pedals the user is never going to reset all the potentiometers to zero when not in use so in order to have an accurate graph representation when the unit is powered on I have to tell the screen to display the current pot value on the graph and all the ones below this, this was a pain to program! Is there a way in the coding I can reference each previous tft.fillRect instruction without having to retype it every time? I end up with a string of more tft.fillRect instructions the higher the pot goes up! I should point out the code works perfectly and the display looks really good. I just want to minimize coding because in it’s permanent state I want to use a nano rather than the mega I’ve prototyped it on. I’m rather new to programming so I’ve probably used a long winded way to achieve the result I’d like! There are 3 separate graphs, the attached code is just for 1 of them. Thanks in advance, Kev

if (analogRead(A11) >10 && analogRead(A11) <170.5){tft.fillRect(240,35,11,15,CYAN);}
  if (analogRead(A11) >170.5 && analogRead(A11) <341){tft.fillRect(240,35,11,15,CYAN);tft.fillRect(252,30,11,20,CYAN);}
  if (analogRead(A11) <170.5){tft.fillRect(252,30,11,20,BLACK);}
  if (analogRead(A11) >341 && analogRead(A11) <511.5){tft.fillRect(240,35,11,15,CYAN);tft.fillRect(252,30,11,20,CYAN);tft.fillRect(264,25,11,25,CYAN);}
  if (analogRead(A11) <341){tft.fillRect(264,25,11,25,BLACK);}
  if (analogRead(A11) >511.5 && analogRead(A11) <682){tft.fillRect(240,35,11,15,CYAN); tft.fillRect(252,30,11,20,CYAN);tft.fillRect(264,25,11,25,CYAN);tft.fillRect(276,20,11,30,CYAN);}
  if (analogRead(A11) <511.5){tft.fillRect(276,20,11,30,BLACK);}
  if (analogRead(A11) >682 && analogRead(A11) <852.5){tft.fillRect(240,35,11,15,CYAN); tft.fillRect(252,30,11,20,CYAN);tft.fillRect(264,25,11,25,CYAN);tft.fillRect(276,20,11,30,CYAN);tft.fillRect(288,15,11,35,CYAN);}
  if (analogRead(A11) <682){tft.fillRect(288,15,11,35,BLACK);}
  if (analogRead(A11) >852.5 && analogRead(A11) <1026){tft.fillRect(240,35,11,15,CYAN); tft.fillRect(252,30,11,20,CYAN);tft.fillRect(264,25,11,25,CYAN);tft.fillRect(276,20,11,30,CYAN);tft.fillRect(288,15,11,35,CYAN);tft.fillRect(300,10,11,40,CYAN);}
  if (analogRead(A11) <852.5){tft.fillRect(300,10,11,40,BLACK);}
  if (analogRead(A11) <10){tft.fillRect(240,35,11,15,BLACK);}
  uint16_t valueRead = analogRead(A11);

  switch (valueRead) {

    case 10 ... 170:   // note range from 10 to 170 inclusive
    //tft.fillRect(240,35,11,15,CYAN);
    // break;

    case 171 ... 340:   
      //tft.fillRect(240,35,11,15,CYAN);
      // break;

  }

hope you are pretty aware of the use of break in switch statement

Hello KevDoog68,

Something like:

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  bargraph(analogueRead(all));
}

void bargraph(int AR) {
  if (AR > 10 {tft.fillRect(240,35,11,15,CYAN);  //If the value is less than or equal to 10, no fill
                                                                //If the value is more than 10, fill to 240
  if (AR > 170 {tft.fillRect(252,35,11,15,CYAN); //If the value is more than 170 fill to 252
                                                                //(Note that the the fill to 240 will also have happened)
  if (AR > 341 {tft.fillRect(264,35,11,15,CYAN); //If the value is more than 341 fill to 264
                                                                //(Note that the the fill to 240 and 252will also have happened)
  if (keep going...)
  
}

Notes:
I might not have put the correct values in for each if(), I leave that to you.

The thing I think you missed is that each time an if() is true, it doesn't stop the one below. For example, if analogueRead is 500 and you have if(analogueRead > 100) followed by if(analogueRead > 200) followed by if(analogueRead > 300) then all 3 of them are true and all 3 of them will get their code run. Therefore you fill the display in blocks, one at at time above the previous one, until the an if() becomes untrue.

As I have put the code each fill will cover the previous one, it would be more elegant for each one to only cover the space above the previous one, edit the area covered to do this.

The value returned from analogueRead is an integer, there is no point in comparing to a decimal;

 if (analogRead(A11) >511.5 ....)

Either use 511 or use 512.

PerryBebbington:
The value returned from analogueRead is an integer, there is no point in comparing to a decimal;

Not only is there no point, if in the process, it actually "casts" to a decimal (float) to do the compare, that would be a massive waste of resources including code space, RAM space and execution time. :astonished:

If you are thinking in terms of "greater than halfway between" then that is the same as greater than or equal to the next integer.

It has been pointed out that you only need to do the (time consuming) analogRead once to compare to the thresholds and the "range" function will generate the necessary comparisons. If the ranges are contiguous, there is certainly no point in checking twice for the top of one range if it is also the bottom of the next range. This is where you use the "break" instead.

Thanks All,

My issue was the way I had the code would only display the range the pot was at on switch on, it wouldn't display the lower. I'm a complete novice so I knew there would be an easier way! I used the decimals simply because I divided the pot range (0-1023) by 6 to have my 6 bar graph. If decimals aren't used I'll just amend my code, it's not like it's a finely calibrated display!

As per attached picture!

display.jpg

My issue was the way I had the code would only display the range the pot was at on switch on, it wouldn't display the lower.

Don't beat yourself up about it! I have written my share of over complicated code only to realise there was a much simpler way. I wish I'd seen the photo right at the start, I was having trouble visualising what you were trying to do.

So you can see what my issue was! Lets say the pot was at halfway and i powered it up my original code would only light up the middle segment and not the preceding ones. By telling it to display all the relevant segments that meet the 'if statement' it did what i wanted it to but just seemed awfully complicated for what it did! I felt sure that there was a solution with integers! I'm not too hard on myself I think I'm doing ok for a 4 week arduino owner!

I'm not too hard on myself I think I'm doing ok for a 4 week arduino owner!

You're doing damn well! ++Karma :slight_smile: