Newbie having trouble displaying variable on lcd.

Hi Carl,

No problems :slight_smile: had fun doing it.

I revised the code to be a bit more time efficient, and to also have a more stable output. Fixed some compile issues as well:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potpin = 0;    // analog pin used to connect the potentiometer
int rawval;        // value from the analog pin 
int mapval;        // rawval remapped to be from 0-180 degrees
int oldval = 200;  // previous value of mapval, set out of range intentionally so it always updates first time through (range: 0-180)

/* ^^ separation of these variables can come in handy sometimes further down in
your program if you ever need to reuse the raw value..                       */


void setup() {
  // set up the LCDs number of columns and rows and init display
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Current angle:");
}

void loop() {
  rawval = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  mapval = map(rawval, 0, 1023, 0, 179);
  
  if (oldval = mapval)
  { 
    // we're already displaying the right value so we do nothing - nice and fast!
  }
  else
  {
    // this only runs when mapvap is different to the displayed value on the screen, making your display look more stable.
    lcd.setCursor(0,1);     // reset cursor to first character (0), second line (1)
    lcd.print("    ");      // write blank space over the previous value and degree symbol so there are no artefacts from the previous output
    lcd.setCursor(0,1);     // reset the cursor again so that we output cleanly over the fresh blank space
    lcd.print(mapval);      // output the value to the second line on the screen
    lcd.print("°");         // put a degrees symbol after it, hopefully this outputs correctly on the LCD - the character *is* present in the LCD's character map
    oldval = mapval;        // store the displayed value so we can check if it changes
  }
}

The code now stores the displayed value and when it changes, updates the LCD. This stops it continually refreshing and clearing over and over again. The code should be more efficient if you have to drop it into other projects, and there's no delays in sight :slight_smile: