LCD 5110 - How to keep the text from moving at each loop

Hi,

I’ve created a sketch that reads a temperature sensor, and prints the values to the Serial Monitor and to a 5110 LCD screen.

I’ve basically managed to get it to work, but I believe some of my parameters are wrong. However not sure which.

The error: For each loop the bitmap and string that I print on the screen are offset 2-3 pixels to the right. So the first loop the print look good (center of the LCD screen), the second loop the print moves a bit (2-3 pixels) to the right.

I’ve attached two photos. One showing the first loop (when it looks good) and one of the eight loop (moved quite a bit to the right).

I hope someone can help me spot the error… please also help if you find other errors in the code or comments.

// Created: March 2016
// Last updated: XX
// Purpose: Read analog temperature sensor and print information to Nokia 5110 LCD Screen

// TUTORIALS USED: 
// LCD5110 start tutorial - https://www.youtube.com/watch?v=ga_1dqGjsbo
// LCD5110 and temperature sensor - https://www.youtube.com/watch?v=ga_1dqGjsbo

// HARDWARE USED:
// Created for the Arduino Uno R3 Board - Datasheet and schematics - https://www.arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf
// Temperature sensor (TMP36) - Datasheet and schematics - https://www.arduino.cc/en/uploads/Main/TemperatureSensor.pdf
// Nokia 5110 LCD Screen - Datasheet and schematics N/A

// LIBRARIES USED: 
// LCD5110_Graph.h - Used to write to the LCD - created by RinkyDink - Manual - http://www.rinkydinkelectronics.com/library.php?id=47

#include <LCD5110_Graph.h> // The library needed to write to the LCD 5110 

// It is assumed that the LCD module is connected to
// the following pins 

//      SCK  - Pin 3 (LCD module name is CIK)
//      MOSI - Pin 4 (LCD module name is DIN)
//      DC   - Pin 5 (LCD module name is DC)
//      RST  - Pin 7 (LCD module name is RST)
//      CS   - Pin 6 (LCD module name is CE)

LCD5110 lcd(3,4,5,7,6); // Set the PINs used on the Arduino board for the LCD module

extern unsigned char SmallFont[];
extern unsigned char BigNumbers[];
extern uint8_t temperatureIcon[];
extern uint8_t arduino_logo[];

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

  // LCD SCREEN
  lcd.InitLCD(); // Initialising the LCD
  lcd.setFont(BigNumbers); // Sets the font to use

  // SENSORS
  pinMode(5, INPUT); // Set the input PIN for the temperature sensor
 
  // SERIAL MONITOR
  Serial.begin(9600);

}

void loop() {
  // main code here, to run repeatedly:
  int TMP_sensor = analogRead(A5); // Get the raw data from the TMP sensor

  // Setting up the LCD screen
  lcd.clrScr(); // Clear the screen buffer
  lcd.drawBitmap(0,0, temperatureIcon, 84, 48); // Drawing the Temperature icon
  
  // DEBUG - For writing to serial the raw temperature value from the sensor. 
  // Serial.print("Temperature value: ");
  // Serial.print(TMP_sensor);
  // Serial.println();

  // convert the ADC (temperature sensor reading) reading to voltage
 
  float voltage = (TMP_sensor/1024.0) * 5.0; // The sensor give a value between 0-1024, which equate to a volate between 0 - 5

  // DEBUG - write the converted voltage 
  // Serial.print(", Volts: ");
   //Serial.print(voltage);

  // convert the voltage to temperature in degrees
  float temperature = (voltage - 0.5 ) * 100; // Depends on sensor - see DataSheet - This sensor: 10 millivolts = 1 degree Celsius  - As the sensor can indicate negative temperatures, we have to substract 0.5 and multiply with 100 to get the accurate temperature in Celsius

  // Print temperature in degrees C to the Serial Monitor 
  Serial.print("Degrees C: ");
  Serial.println(temperature);
  Serial.println();

  // Convert the float (eg. 1.54321) to string (letters)
  double floatVal=temperature; // set the double (eg. same as float, but used in the code below
  char charVal[4];               //temporarily holds data from vals - the number 4 decides the length
  String stringVal = "";     //data on buff is copied to this string
  dtostrf(floatVal, 4, 1, charVal); // FloatVal, minimum width (unsure of the use), precision = number of decimal points, string

  //convert chararray to string
  for(int i=0;i<sizeof(charVal);i++) // goes to the temperature (eg. 20.5) character by character and constructs the string
  {
    stringVal+=charVal[i];
  }
  
  Serial.print("stringVal: ");Serial.println(stringVal); //display the string in the Serial Monitor

  // Print temperature in degrees Celsium to the LCD
   lcd.print(stringVal,25,11); // The last two figures tell where to start writing on the screen
   lcd.update();

   
   delay(1000);


}

This is seriously weird, as the icon moves and the screen is cleared every time round. I'm guessing a bit but try:

Comment out the lcd.print(stringval) to see if the icon moves

Comment out the lcd.drawBitmap, to see if the stringval moves.

move the code

  // Setting up the LCD screen
  lcd.clrScr(); // Clear the screen buffer
  lcd.drawBitmap(0,0, temperatureIcon, 84, 48); // Drawing the Temperature icon

to the setup section. It is not animated and dsoesn't need to be printed every time round.

I don't know why you have to make a string anyway. Maybe it has something to do with decimal point with this font.