Go Down

Topic: LCD not displaying int variables properly (Read 659 times) previous topic - next topic

Avamander

Oct 02, 2014, 06:56 pm Last Edit: Oct 02, 2014, 07:02 pm by Avamander Reason: 1
My problem is that the LCD is not displaying a variable as a number, it's instead displaying a character.
Depending on the value it displays a different character. It displays variable named "highscore" properly but it is not displaying "score" and "raund" properly. Help is much appreciated.

EDIT: If I create a custom character(aka a proper number) to the position 0, 1, 2, 3 it displays those numbers correctly.

My code:
Code: [Select]

    #include <LiquidCrystal.h>
#include <EEPROM.h>

LiquidCrystal lcd(12, 11, 5, 15, 3, 2);

int raund = 0;
int z;
int y;
int x;
int playsequence = 0;
int sequence[30] = {
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int neu = 1;
int gameend = 0;
int receivesequence = 0;
int buttonpresscount = 0;
int score = 1;                 //String
int highscore;
                                                 //LED ports
int sinineled = 4;
int punaneled = 9;
int kollaneled = 10;
int rohelineled = 14;
                                                 //Switches
int sinineluliti = 8;
int punaneluliti = 7;
int kollaneluliti = 13;
int rohelineluliti = 6;
int luliti[4] =  {
 sinineluliti, punaneluliti, kollaneluliti, rohelineluliti};
int led[4] = {
 sinineled, punaneled, kollaneled, rohelineled};

void setup() {
 lcd.begin(16, 2);
 lcd.setCursor(0, 0);
 lcd.print("Maluvillak");
 lcd.setCursor(0, 1);
 highscore = EEPROM.read(1);
 lcd.print("Parim skoor:");
 lcd.setCursor(14, 1);
 lcd.print(highscore);

 pinMode(luliti[0], INPUT);
 pinMode(led[0], OUTPUT);
 pinMode(luliti[1], INPUT);
 pinMode(led[1], OUTPUT);
 pinMode(luliti[2], INPUT);
 pinMode(led[2], OUTPUT);
 pinMode(luliti[3], INPUT);
 pinMode(led[3], OUTPUT);

 digitalWrite(led[0], HIGH);                   //Random test to indicate it restarted
 delay(55);
 digitalWrite(led[1], HIGH);
 digitalWrite(led[0], LOW);
 delay(55);
 digitalWrite(led[2], HIGH);
 digitalWrite(led[1], LOW);
 delay(55);
 digitalWrite(led[3], HIGH);
 digitalWrite(led[2], LOW);
 delay(55);
 digitalWrite(led[3], LOW);
 Serial.begin(9600);                           //Serial communication
 delay(2000);
}

void loop(){

 if(neu==1){                                   //Generate a new number to the sequence
   sequence[raund]= random(4);
   playsequence = 1;
   neu= 0;
   lcd.setCursor(9, 0);
   lcd.write("Lvl:");
   lcd.setCursor(13, 0);
   lcd.write(raund);                           //BUG
   delay(250);
 }



 if(playsequence==1){
   neu = 0;
   Serial.println(raund);
   if(raund > 0){
     for(x = 0;x < raund+1; x++){
       lcd.setCursor(15, 0);
       Serial.print("Iks:");
       Serial.println(x);
       digitalWrite(led[sequence[x]], HIGH);
       delay(200);
       digitalWrite(led[sequence[x]], LOW);
       delay(200);
       Serial.println(x);
       if(x < raund){                               //End sequence playing if played enough
         receivesequence = 1;
         playsequence = 0;
       }
       buttonpresscount=0;
     }
     delay(250);
   }

   if(raund == 0){                                  //Rough patch to make it play the sequence when it's the first round
     digitalWrite(led[sequence[0]], HIGH);
     delay(200);
     lcd.setCursor(0, 0);
     lcd.write("Skoor:            ");
     digitalWrite(led[sequence[0]], LOW);
     lcd.setCursor(7, 0);
     lcd.write(score);
     delay(200);
     playsequence = 0;
     receivesequence = 1;
     buttonpresscount=0;
   }
 }



 if(receivesequence==1 && playsequence == 0){
   for(z = 0; z < 4; z++){
     if(z == 4){
       z=0;
     }
     if(digitalRead(luliti[z]) == HIGH){
       Serial.println(z);
       digitalWrite(led[z], HIGH);
       if(round!=0){
         if(sequence[buttonpresscount]==z){
           buttonpresscount++;
           score++;
           delay(300);
         }
         else{
           receivesequence=0;
           lcd.clear();
           lcd.setCursor(0, 0);
           lcd.setCursor(0, 1);
           lcd.setCursor(7, 1);
           lcd.write(score);
         }

       }
       else{
         if(sequence[0] == z){
           score++;
           receivesequence=0;
           delay(200);
         }
       }
       digitalWrite(led[z], LOW);
     }
     if(buttonpresscount == raund+1 && raund != 0){             //Right sequence was entered
       receivesequence=0;
       neu=1;
       raund++;
       buttonpresscount=0;
     }
     else if(buttonpresscount == 1 && raund == 0){              //Right button was pressed
       receivesequence=0;
       raund++;
       neu=1;
       Serial.print(buttonpresscount);
       buttonpresscount==0;
     }
   }
 }




 if(gameend==1){
   if(score > highscore){
     EEPROM.write(1, score);
   }
 }
}

groundFungus

You have lcd.write in many places.  lcd.write sends bytes (the binary code) not characters.  So lcd.write(48) prints '0' where lcd.print(48) prints  48.  Try replacing lcd.write with lcd.print.

Avamander

Oh, thanks! I made a stupid mistake, it seems it works now.

Go Up