Strange count effect.

So I have been playing with some sensors, and have had some errors, code wise. I narrowed it down to a single field… what is happening is my count climbs by one or lowers by one , however if I reach 11 and count down it goes from 10 to 90 to 80 to 70 and back up by tens until it hits 90 then it goes to 10 11 12 etc… anyways I will post my code. Thanks in advance.

#include <LiquidCrystal.h>
#include <DHT.h>

int tempval;

#define DHTPIN 11
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);
  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  return btnNONE;
}




void setup()
{
  lcd.begin(16, 2);
}

void loop()
{
 lcd_key = read_LCD_buttons();
 
  switch (lcd_key)
  {
   case btnUP:
      {
        tempval++;
        lcd.setCursor(0, 1);
        lcd.print(tempval);
        delay(250);
        break;
      } 
      
      case btnDOWN:
      {
        tempval--;
        lcd.setCursor(0, 1);
        lcd.print(tempval);
        delay(250);
        break;
      }
  }
}

So you print a 10, and then you print a 9 over it. The 9 only prints over the 1 and the 0 is left there because you didn't clear the screen. If you don't want to clear the whole screen then print spaces over it or format your number with leading spaces or leading 0's.

For starters (as Delta_G stated), you need to clear the lcd screen before writing any new numbers to the screen. This is because if you write number 11 and then write 2 without clearing the screen first, you might see 21 or 12 instead of just 2.

Also, you might want to git rid of the delay() calls. Rarely is delay() needed. See the tutorial "Blink Without Delay" if you need to do anything in terms of timing.

Well ya that is what it is

lcd.clear();

Thanks everyone...
The smarter I get the dummer mistakes I make...LOL

Hi,

lcd.clear();

May eventually cause your display to flicker as you get it to display more characters.

 switch (lcd_key)
  {
   case btnUP:
      {
        tempval++;
        lcd.setCursor(0, 1);
        lcd.print("   ");
        lcd.setCursor(0, 1);
        lcd.print(tempval);
        delay(250);
        break;
      } 
      
      case btnDOWN:
      {
        tempval--;
        lcd.setCursor(0, 1);
        lcd.print("   ");
        lcd.setCursor(0, 1);
        lcd.print(tempval);
        delay(250);
        break;
      }
  }

Will be better as it overwrites the old digits with spaces before you write the new data, and leaves the rest of the display alone.

Tom… :slight_smile:

        lcd.setCursor(0, 1);
        lcd.print("   ");
        lcd.setCursor(0, 1);
        lcd.print(tempval);

Looks like a candidate to put into a function to be called when required. If you need to use the technique to print variables in different places on the display then the function could be passed row, column and variable name to be printed to make it more generally useful.

If you have the spare room on the display it is more efficient to always print spaces after value to avoid the need to position the cursor twice. A variation would be to print only the number of spaces required after the variable depending on its length, thus avoiding the possibility of printing over other data or text already on the screen.