Simplifying Function (combine string and int)

Hi All,

I’m trying to find a more elegant way to update my LCD and cursor position on the LCD.

The function here is just to illustrate something that I use to set the current cursor location, which I would like to simplify. Obviously this function is not useful as is (I have set up a switch case for each LCD.print), but I just want to move forward with a strategy and then apply it to my current code as needed.

inception:

cursor() {
  LCD.print("?x00?y0");
  LCD.print("?x01?y0");
  LCD.print("?x02?y0");
  LCD.print("?x03?y0");
  LCD.print("?x04?y0");
  LCD.print("?x05?y0");
  LCD.print("?x06?y0");
  LCD.print("?x07?y0");
  LCD.print("?x08?y0");
  LCD.print("?x09?y0");
  LCD.print("?x10?y0");
  LCD.print("?x11?y0");
  LCD.print("?x12?y0");
  LCD.print("?x13?y0");
  LCD.print("?x14?y0");
  LCD.print("?x15?y0");
  LCD.print("?x00?y1");
  LCD.print("?x01?y1");
  LCD.print("?x02?y1");
  LCD.print("?x03?y1");
  LCD.print("?x04?y1");
  LCD.print("?x05?y1");
  LCD.print("?x06?y1");
  LCD.print("?x07?y1");
  LCD.print("?x08?y1");
  LCD.print("?x09?y1");
  LCD.print("?x10?y1");
  LCD.print("?x11?y1");
  LCD.print("?x12?y1");
  LCD.print("?x13?y1");
  LCD.print("?x14?y1");
  LCD.print("?x15?y1");
}

1st revalation:

cursor() {
  char pos1 = "?x00?y0";
  char pos2 = "?x01?y0";
  char pos3 = "?x02?y0";
  ...

  LCD.print(pos1);
  LCD.print(pos2);
  LCD.print(pos3);
  ...

1st evolution (which I do not think will compile or work as I expect):

cursor() {
  char pos[33] = {"?x00?y0", "?x01?y0", "?x02?y0", ..., "?x15?y1"};
  for (int i = 0, i < 33, i++) {
    LCD.print(pos[i]);
  }
}

Untested:

for (int y=0; y<=1; y++)
{
  for (int i=0; i<=15; i++)
  {
    LCD.print("?x");
    if (i < 10)
      LCD.print('0');
    LCD.print(i);
    LCD.print("?y");
    LCD.print(y);
  }
}

You could also use sprintf() if you wanted to do a single LCD.print() call.

lcd.write() is useful sometimes, others not, just have to experiment. Here is something I threw together.

void cursor() {
  lcd.print("?x00?y0");
  for (int i = 0; i < 2; i++) {
    lcd.setCursor(6,0);
    lcd.print(i, DEC);
    for (int j = 0; j < 16; j++) {
      lcd.setCursor(2,0);
      if (j < 10) {
        lcd.write('0');
      } 
      lcd.print(j, DEC);
    }
  }
}

Slightly redundant as it reprints the 0’s in for x and y both, but it gets the job done.

Note though, there is no delay in between each segment so it is pretty instantaneous. If you add any sort of delay in between each iteration of the for loop, it works like a gem.

Yup, looks like what Arrch posted would also get the job done, pretty much the same thing.

Somehow my last piece did not get copied to my post:

I think there is a way to do something with having an x and y variable, and somehow concatenate the string, but I’m not seeing it yet.

cursor() {
  for (int y = 0, y < 2, y++) {
    for (int x = 0, x < 16, x++) {
      LCD.print("?x" && char(x) && "?y" && char(y));
    }
  }
}

Note that I am not using a parallel LCD nor the liquid crystal library.

Arrch, I will try your proposal tonight. I suspected there wold be an issue with the line endings when splitting up the command.

BTW, this is a modern device LCD with a serial interface (pic chip).