Issue with value input through an LCD 1602 with keypad

Hey everyone!

I need to input a temperature value through an LCD1602 with Keypad UI.
It seems like the UI is working well to decrease the temperature setpoint (using Button DOWN), bue when using the Button UP the temperature setpoint is always reseting to 0.

In the video below you will see that the Units decresing value is not working properly, that issue is already FIXED, please ignore that part of the video.

My code:

#include <Arduino.h>
#include <LiquidCrystal.h>

/*******************************************************
This program will test the LCD panel and the buttons
********************************************************/
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;

int K_plus1 = 0;
int K_minus1 = 0;
int K = 0;
int H_plus1 = 0;
int H_minus1 = 0;
int H = 0;
int D_plus1 = 0;
int D_minus1 = 0;
int D = 0;
int U_plus1 = 0;
int U_minus1 = 0;
int U = 0;
int TempTarget;

int position = 1;
int pos_plus1;
int pos_minus1;
int pos_keep;

// degree sign
byte degree_sign[] = {
    B00110,
    B01001,
    B01001,
    B00110,
    B00000,
    B00000,
    B00000,
    B00000};

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
  adc_key_in = analogRead(0); // read the value from the sensor
  // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  // we add approx 50 to those values and check to see if we are close
  if (adc_key_in > 1000)
    return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  // For V1.1 us this threshold
  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; // when all others fail, return this...
}
void setup()
{
  Serial.begin(9600);

  lcd.begin(16, 2);
  lcd.createChar(0, degree_sign);
  lcd.home();

  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(5, 0);
  lcd.print(TempTarget);

  // lcd.write(degree_sign);
  // lcd.write(0);
  // lcd.setCursor(8, 0);
  // lcd.print("C: ");

  lcd.setCursor(0, 1);
  lcd.print(position);
}
void loop()
{

  //############################  KEYS  ###################################
  //#######################################################################
  //
  lcd_key = read_LCD_buttons(); // read the buttons

  switch (lcd_key) // depending on which button was pushed, we perform an action
  {
  case btnRIGHT:
  {
    pos_plus1 = position + 1;
    position = pos_plus1;

    if (position > 4)
    {
      position = 4;
    }
    delay(300);

    lcd.setCursor(0, 1);
    lcd.print(position);
    break;
  }
  case btnLEFT:
  {
    pos_minus1 = position - 1;
    position = pos_minus1;
    if (position < 1)
    {
      position = 1;
    }
    delay(300);

    lcd.setCursor(0, 1);
    lcd.print(position);
    break;
  }
  case btnUP:
  {
    switch (position)
    {
    case 1:
    {
      K_plus1 = K;
      K = K_plus1 + 1;
      TempTarget = (K * 1000) + (H * 100) + (D * 10) + U;

      lcd.setCursor(0, 0);
      lcd.print("Temp:");
      lcd.setCursor(5, 0);
      lcd.print(TempTarget);

      delay(300);

      lcd.setCursor(0, 1);
      lcd.print(position);
      break;
    }

    case 2:
    {
      H_plus1 = H;
      H = H_plus1 + 1;
      TempTarget = (K * 1000) + (H * 100) + (D * 10) + U;

      lcd.setCursor(0, 0);
      lcd.print("Temp:");
      lcd.setCursor(5, 0);
      lcd.print(TempTarget);

      delay(300);

      lcd.setCursor(0, 1);
      lcd.print(position);

      break;
    }

    case 3:
    {
      D_plus1 = D;
      D = D_plus1 + 1;
      TempTarget = (K * 1000) + (H * 100) + (D * 10) + U;

      lcd.setCursor(0, 0);
      lcd.print("Temp:");
      lcd.setCursor(5, 0);
      lcd.print(TempTarget);

      delay(300);

      lcd.setCursor(0, 1);
      lcd.print(position);

      break;
    }

    case 4:
    {
      U_plus1 = U;
      U = U_plus1 + 1;
      TempTarget = (K * 1000) + (H * 100) + (D * 10) + U;

      lcd.setCursor(0, 0);
      lcd.print("Temp:");
      lcd.setCursor(5, 0);
      lcd.print(TempTarget);

      delay(300);

      lcd.setCursor(0, 1);
      lcd.print(position);

      break;
    }
    }
  }
  case btnDOWN:
  {

    switch (position)
    {
    case 1:
    {
      K_minus1 = K - 1;
      K = K_minus1;
      TempTarget = (K * 1000) + (H * 100) + (D * 10) + U;

      lcd.setCursor(0, 0);
      lcd.print("Temp:");
      lcd.setCursor(5, 0);
      lcd.print(TempTarget);

      delay(300);

      lcd.setCursor(0, 1);
      lcd.print(position);

      break;
    }

    case 2:
    {
      H_minus1 = H - 1;
      H = H_minus1;
      TempTarget = (K * 1000) + (H * 100) + (D * 10) + U;

      lcd.setCursor(0, 0);
      lcd.print("Temp:");
      lcd.setCursor(5, 0);
      lcd.print(TempTarget);

      delay(300);

      lcd.setCursor(0, 1);
      lcd.print(position);

      break;
    }

    case 3:
    {
      D_minus1 = D - 1;
      D = D_minus1;
      TempTarget = (K * 1000) + (H * 100) + (D * 10) + U;

      lcd.setCursor(0, 0);
      lcd.print("Temp:");
      lcd.setCursor(5, 0);
      lcd.print(TempTarget);

      delay(300);

      lcd.setCursor(0, 1);
      lcd.print(position);

      break;
    }

    case 4:
    {
      U_minus1 = U - 1;
      U = U_minus1;
      TempTarget = (K * 1000) + (H * 100) + (D * 10) + U;

      lcd.setCursor(0, 0);
      lcd.print("Temp:");
      lcd.setCursor(5, 0);
      lcd.print(TempTarget);

      delay(300);

      lcd.setCursor(0, 1);
      lcd.print(position);

      break;
    }
    }
  }
  case btnSELECT:
  {

    delay(300);
    break;
  }
  case btnNONE:
  {
    lcd.setCursor(0, 0);
    lcd.print("Temp:");
    lcd.setCursor(5, 0);
    lcd.print(TempTarget);

    break;
  }
  } //end switch key
}

Any ideia why is it working when decreasing the value, and not when incresing the temperature value?

The video (sorry for the vertical format):

The cases for button up and button down are missing a break statement.

1 Like

thank you!! @wildbill

How is the project going? Have you melted any metal yet?

Spent the last month finishing the communication between boards (ramps 1.4+arduino with another arduino which controls the laser and powder feeder)
Now i am finalizing the UI, then i Just have to solder the electronics. I hope to start testing in 1 or 2 weeks.
I will post a video on the previous thread as promised :sweat_smile:
But theoreticaly the machine is done :partying_face: