Continuous display in 16x2 LCD

Hello! I'm having trouble displaying continuous digit in my LCD.

I need to display the number of sensor selected by the user.

The display is ok when I display 6 single digits on line 1 and 3 single digits plus 3 double digits on line 2.

Nos.1,2,3,4,5,6,
_7,8,9,10,11,12?

and it's not ok when there's a double digit in line 1.

Nos.1,2,3,101112

should be

Nos.1,2,3,10,11,
_12?

This is my code:
fan[] contains the number of selected sensors

        for(int i = 0; i <= 11; i++){               
          int x=0;
          int y=0;
          if(fan[i] != 0){                   
            if(i <= 5){
              x=4+(2*i);
              y=0;            
            }
            else{
              if(i <= 9){
                x=(2*i)-11;
              }
              else{
                x=(3*i)-20;
              }
              y=1;
            }
            Serial.print("x "); Serial.println(x);
//            Serial.print("y "); Serial.println(y);
            lcd.setCursor(x,y);
            lcd.print(fan[i]);
            
            if(fan[i+1] != 0){
              lcd.print(",");
            }
            else{
              lcd.print("?");
            }                    
          }
        }

I think the problem is how will I set the cursor from line 1 to line 2 if there's no space left?

use

lcd.setCursor()

inly once per line and then print the array with commas without using the setCursor() function inside that loop.

I need setCursor() because I don't know when the selected sensor number exceed the line?

example:

Nos.1,2,10,11,
_12?

vs.

Nos.1,2,3,4,5,
_10,11,12?

ok, then do like this

set cursor
if sensor number < 10, lcd print a space
lcd print the number
lcd print the comma
etc...

wil print like this:

 1, 2, 3, 4, 5, 6,11,14,15

Then I cannot display all numbers :frowning:

There are 12 sensors, if I do that I can only display up to

Nos._1,_2,_3,_4,
_5,_6,_7,_8,_9,1

This is my new code:

      int x=0;
      int y=0; 
      for(int i = 0; i <= 11; i++){        
        if(fan[i] != 0){
          if(fan[i] <= 9){ // single digit
            if(i == 0){ 
              x=4;
            }
            else{
              if(y == 0 && x < 14){ // line1
                x=4+(2*i);
              }
              else{
                x=(2*i)-11;
                y=1;
              }
            }
          }
          else{ // double
            if(y == 0 && x < 12){ // line1
              x=2+(3*i);
            }
            else{
              x=(3*i)-20;
              y=1;
            }
          }

          Serial.print("i "); Serial.println(i);
          Serial.print("x "); Serial.println(x);
          Serial.print("y "); Serial.println(y);
          Serial.println("----");
          lcd.setCursor(x,y);
          lcd.print(fan[i]);
          
          if(fan[i+1] != 0){
            lcd.print(",");
          }
          else{
            lcd.print("?");
          }                    
        }
      }

This is the algorithm:

  1. Last fan?
    N, is the digit single?
  2. Single?
    Y, cursor on line1?
    N, is the digit double?
  3. Single, Line1?
    Y, enough space?
    N, send to line 2
  4. Double?
    Y, cursor on line1?
  5. Double, Line1?
    Y, enough space?
    N, send to line 2

but there's still a problem when I display 2digits in line1

Post your full code

arduinoTime:
Then I cannot display all numbers :frowning:

There are 12 sensors, if I do that I can only display up to

how about

char sensors[12] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'};

Why are you manually setting the cursor, let the code do the work.

Set the cursor to display on line 1 and/or line 2, at column 4, and let the code take over.

HazardsMind:
Why are you manually setting the cursor, let the code do the work.

Set the cursor to display on line 1 and/or line 2, at column 4, and let the code take over.

it appears he is overflowing the available space on the display, no matter how he gets there.

He doesn't need the blank spaces and the commas, he can use one or the other to separate them if needed, but not both. I like to use '|' as a separator.

"nos. 1|2|3|4|5|6" full, 16 columns used
"7|8|9|10|11|12" 13 column used

Here is an idea to try - count the number of characters currently on line.

void  TestLCD(void)
{
  lcd.clear();
#define SENSORS 120 
 
  int iLine1Count = 0;
  bool bLine2 = false; 


  int x=0;
  int y=0; 
  for(int i = 0; i <= SENSORS; i++){ 

    if(i < 10)                          // single digit 
      iLine1Count++;
    else
      iLine1Count += 2;    // double 

// add spaces etc. here 

    if(iLine1Count < 17)     //line 1 visible length 
    {
      // print line 1  
      ;
      /*   
       lcd.print(i); //fan[i][0]);
       delay(DELAY_LCD/2);
       */

    }
    else
      // set cursor to line 2 once 
    {
      if (!bLine2) 
      {
        lcd.setCursor(0,1);
        bLine2++; 
      }
    }

    lcd.print(i); //fan[i][0]);
    delay(DELAY_LCD/2);

}

BulldogLowell:

HazardsMind:
Why are you manually setting the cursor, let the code do the work.

Set the cursor to display on line 1 and/or line 2, at column 4, and let the code take over.

it appears he is overflowing the available space on the display, no matter how he gets there.

Sir BulldogLowell is right, I need overflow.

Thank you for all the help!

The code is now working :slight_smile:

      for(int i = 0; i <= 11; i++){        
        if(fan[i] != 0){
          if(i == 0){
            dPos=4;
          }
          else{            
            if(fan[i-1] < 10){
              dPos+=2;
            }
            else{
              dPos+=3;
            }
          }          
         
          if(dLine == 0){
            int d;
            if(fan[i] < 10){
              d=2;
            }
            else{
              d=3;
            }
            
            if(dPos+d > 16){
              dPos=1;
              dLine=1;
            }
          }          
          lcd.setCursor(dPos,dLine);
          lcd.print(fan[i]);
          
          if(fan[i+1] != 0){
            lcd.print(",");
          }
          else{
            lcd.print("?");
          }                  
        }
      }

I determined if the number of digit the previous number has then I compute if I need to go to the next line by adding the number of digit of the current number.

Please give comments regarding the code :slight_smile: Thank you!

if it works, and you are happy than the code is wonderful!

good to see you worked it out