programing a 16 x 2 lcd

Problem Solved! Thanks to all who helped! I used '=' insted of '==' I thought that I was programming in free pascal

I'm creating a program do print alphabet on the lcd. (arduino 1602 lcd keypad shield)

The lcd has 5 buttons: select, left, right, up, down and reset.

my variable is = 0 when i push the button UP, the program add 1 to a variable, (variable = 1) and the program should print on the lcd "A" when i push again the button UP: variable = 2, and the program should print on the lcd "B" when i push again the button UP: variable = 3, and the program should print on the lcd "C"

and so on

but the program always print "A" and the variable returns always 1

does anyone know why?

here's part of the program

void letras()
  {
    if (contanome = 1)
      {carater = 'A';
      lcd.print("A");
    else if (contanome = 2)
      {carater = 'B';
      lcd.print("B");
    else if (contanome = 3)
      {carater = 'C';
      lcd.print("C");
    else if (contanome = 4)
      {carater = 'D';
      lcd.print("D");}
    else if (contanome = 5)
      {carater = 'E';
      lcd.print("E");}
    else if (contanome = 6)
      {carater = 'F';
      lcd.print("F");}
    else if (contanome = 7)
      {carater = 'G';
      lcd.print("G");}
    else if (contanome = 8)
      {carater = 'H';
      lcd.print("H");}
    else if (contanome = 9)
      {carater = 'I';
      lcd.print("I");}
    else if (contanome = 10)
      {carater = 'J';
      lcd.print("J");}
    else if (contanome = 11)
      {carater = 'K';
      lcd.print("K");}
    else if (contanome = 12)
      {carater = 'L';
      lcd.print("L");}
    else if (contanome = 13)
      {carater = 'M';
      lcd.print("M");}
    else if (contanome = 14)
      {carater = 'N';
      lcd.print("N");}
    else if (contanome = 15)
      {carater = 'O';
      lcd.print("O");}
    else if (contanome = 16)
      {carater = 'P';
      lcd.print("P");}
    else if (contanome = 17)
      {carater = 'Q';
      lcd.print("Q");}
    else if (contanome = 18)
      {carater = 'R';
      lcd.print("R");}
    else if (contanome = 19)
      {carater = 'S';
      lcd.print("S");}
    else if (contanome = 20)
      {carater = 'T';
      lcd.print("T");}
    else if (contanome = 21)
      {carater = 'U';
      lcd.print("U");}
    else if (contanome = 22)
      {carater = 'V';
      lcd.print("V");}
    else if (contanome = 23)
      {carater = 'W';
      lcd.print("W");}
    else if (contanome = 24)
      {carater = 'X';
      lcd.print("X");}
    else if (contanome = 25)
      {carater = 'Y';
      lcd.print("Y");}
    else if (contanome = 26)
      {carater = 'Z';
      lcd.print("Z");}
    else if (contanome = 27)
      {carater = ' ';
      lcd.print(" ");}
  }

and inside of the loop i have this:

  do{
        lcd_key = read_LCD_buttons();
      
      if (read_LCD_buttons() == btnUP) // se o botão carregado for para cima
          {
            Serial.println(contanome);Serial.println("inicio"); // always contanome = 1
            contanome++;
            Serial.println(contanome);Serial.println("após"); // always contanome = 2
            if (contanome > 27)        // o alfabeto tem 26 e adicionei o espaço
              {                        // quando chegar ao final dos carateres possiveis 
                contanome = 1;         // regressar ao primeiro carater
              } 
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print(nome);          // faz print do que já foi escrito e guardado
            lcd.setCursor(X,Y);       // posiciona o cursor
            letras();                 // fiz um procedimento para os 27 if
            delay(225);
          }

here's part of the program

Here's part of the solution

  if (contanome == 1)

https://www.arduino.cc/en/Reference/If

Instead of continuous “else if”, use “switch case”.

Post the complete code using code tags </>.

You also LCD.print(nome) but there appears to be nothing to change it and should it be contanome?

Weedpharma

As weedpharma says, you should post all of the code, not just part of it. And it should be between code tags.

Please read item #7 here:- How to use this forum

A 'do while' statement usually has a 'while'.

Are you sure this is correct:-

do{
        lcd_key = read_LCD_buttons();
      
      if (read_LCD_buttons() == btnUP) // se o botão carregado for para cima
          {

Or did you mean:-

do{
        lcd_key = read_LCD_buttons();
      
      if (lcd_key == btnUP) // se o botão carregado for para cima
          {
void letras()
  {
    if (contanome >= 1 and contanome <= 26)
      {
       carater = 'A' + contanome -1;
        lcd.print(carater);
       }
    else if (contanome = 27)
      {
       carater = ' ';
       lcd.print(" ");
      }
  }

Your letras() function does not even compile because it’s missing a number of closing curly braces.

  if (contanome = 1)
  { carater = 'A';
    lcd.print("A");
    else if (contanome = 2)
    { carater = 'B';
      lcd.print("B");

The else if is inside the if and that does not work.

if (contanome = 1)
{
    carater = 'A';
    lcd.print("A");
} // <<<----
else if (contanome = 2)
{
    carater = 'B';
    lcd.print("B");

There is a quick way to convert a number to a character. You can add 64 (40 hex) to the number to get the matching character.

carater = contanome + 0x40;

The below uses the above; it adds some checks (display ‘?’ if the number is not between 1 and 27 (both included) and adds the option for 27 (the space)). It also uses a 2 byte text buffer to store the character string for display purposes.

void letras()
{

  // declare buffer for text to be displayed; one character and terminating NULL  
  char lcdtext[2];
  lcdtext[1]='\0';

  if(contanome > 0 || contanome <= 27)
  {
    // if 27, it's a space
    if(contanome == 27)
      lcdtext[0] = carater = ' ';
    // else it's A..Z
    else
      lcdtext[0] = carater = contanome + 0x40;
  }
  else
  {
    // error condition, display question mark
    lcdtext[0] = carater = '?';
  }
  lcd.print(lcdtext);
}

Tested on a computer using printf instead of lcd.print.

I see aarg beat me to it :wink:

http://www.gammon.com.au/tips#trap3

Thanks Nick. A great refresher for my old brain. Is now bookmarked.

Weedpharma

cattledog: Here's part of the solution

  if (contanome == 1)

https://www.arduino.cc/en/Reference/If

If I had a dollar for every time I mistakenly used "=" when I wanted "=="... well I'd have lots of dollars.