Enter the time (variable or string) in LCD display

I'm trying to do a university project I need to insert a time on the LCD display via a keyboard like the example in the video below.

Can someone help me?

char key = kpd.waitForKey();
if (key)
{
switch (state) {    
case 1:           
switch (key){
case 'U':
//case bUp:
lcd.clear(); Set_state(8);
break;
case 'D':
//case bDown:
lcd.clear(); Set_state(2);
break;
default:
Set_state(1);
}
break;

case 2:          
//switch (CheckButton()) {
//case bChange:  
switch (key){
case 'U':
lcd.clear(); Set_state(1);
break;
case 'D':
lcd.clear(); Set_state(3);
break;
default:
Set_state(2);
}
break;

case 3:          
switch (key){
case 'R':
lcd.clear();Hora_ON++;
lcd.setCursor(1,0);
lcd.print ("-- Hora ON --");
lcd.setCursor(7,1);
lcd.print (Hora_ON);
break;
case 'E':
lcd.clear(); Set_state(2);
break;
case 'D':
lcd.clear(); Set_state(4);
break;
default:
Set_state(3);
}
break;

case 4:          
switch (key){  
case 'N':
lcd.clear(); Min_ON=kpd.waitForKey();
lcd.setCursor(1,0);
lcd.print ("--- MIN ON ---");
lcd.setCursor(7,1);
lcd.print (Min_ON);
break;
case 'E':
lcd.clear(); Set_state(3);
break;
case 'D':
lcd.clear(); Set_state(5);
break;
default:
Set_state(4);
}
break;

case 5:          
switch (key){ 
case 'R':  
lcd.clear();Hora_OFF++;
lcd.setCursor(1,0);
lcd.print ("-- Hora OFF --");
lcd.setCursor(7,1);
lcd.print (Hora_OFF);
break;
case 'E':
lcd.clear(); Set_state(4);
break;
case 'D':
lcd.clear(); Set_state(6);
break;
default:
Set_state(5);
}
break;

case 6:         
switch (key){ 
case 'N':  
lcd.clear();Min_OFF=kpd.waitForKey();
lcd.setCursor(1,0);
lcd.print ("--- MIN OFF ---");
lcd.setCursor(7,1);
lcd.print (Min_OFF);
break;
case 'E':
lcd.clear(); Set_state(5);
break;
case 'D':
lcd.clear(); Set_state(7);
break;
default:
Set_state(6);
}
break;

case 7:           
switch (key){
case 'U':
//case bUp:
lcd.clear(); Set_state(6);
break;
case 'D':
//case bDown:
lcd.clear(); Set_state(8);
break;
Set_state(7);
}
break;

case 8:          
//switch (CheckButton()) {
//case bChange:  
switch (key){
case 'U':
lcd.clear(); Set_state(1);
break;
case 'D':
lcd.clear(); Set_state(7);
break;
Set_state(8);
}
break;
default:;
}
}

You really should have read How to use this forum before posting. (Especially item #7.)

ie Your code and any error messages should always be placed between [code]code tags[/code]. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis, and often also corrupts the code with italics or smilies. (As you can see in your posted code. :frowning: )

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :slight_smile:

Sorry, friend! and thanks for yours informations! :slight_smile:

Neilon10:
Sorry, friend! and thanks for yours informations! :slight_smile:

Thank you for fixing that. Much better. :slight_smile:

Here is your code with the indentation fixed with the Autoformat tool of the IDE (ctrl-t or autoformat in the tools menu). I it not easier to follow?

If you tell us what happens when you run the code and what you expect to happen it would be easier to help you. As it is we have to guess and that never works out well.

You should post all of your code. Helpers need to see which libraries are used, what is in the setup() function, the variables, their data types and initial values, what the called functions do, etc.

char key = kpd.waitForKey();
if (key)
{
  switch (state) 
  {
  case 1:
    switch (key)
    {
    case 'U':
      //case bUp:
      lcd.clear();
      Set_state(8);
      break;
    case 'D':
      //case bDown:
      lcd.clear();
      Set_state(2);
      break;
    default:
      Set_state(1);
    }
    break;

  case 2:
    //switch (CheckButton()) {
    //case bChange:
    switch (key)
    {
    case 'U':
      lcd.clear();
      Set_state(1);
      break;
    case 'D':
      lcd.clear();
      Set_state(3);
      break;
    default:
      Set_state(2);
    }
    break;

  case 3:
    switch (key)
    {
    case 'R':
      lcd.clear();
      Hora_ON++;
      lcd.setCursor(1,0);
      lcd.print ("-- Hora ON --");
      lcd.setCursor(7,1);
      lcd.print (Hora_ON);
      break;
    case 'E':
      lcd.clear();
      Set_state(2);
      break;
    case 'D':
      lcd.clear();
      Set_state(4);
      break;
    default:
      Set_state(3);
    }
    break;

  case 4:
    switch (key)
    {
    case 'N':
      lcd.clear();
      Min_ON=kpd.waitForKey();
      lcd.setCursor(1,0);
      lcd.print ("--- MIN ON ---");
      lcd.setCursor(7,1);
      lcd.print (Min_ON);
      break;
    case 'E':
      lcd.clear();
      Set_state(3);
      break;
    case 'D':
      lcd.clear();
      Set_state(5);
      break;
    default:
      Set_state(4);
    }
    break;

  case 5:
    switch (key)
    {
    case 'R':
      lcd.clear();
      Hora_OFF++;
      lcd.setCursor(1,0);
      lcd.print ("-- Hora OFF --");
      lcd.setCursor(7,1);
      lcd.print (Hora_OFF);
      break;
    case 'E':
      lcd.clear();
      Set_state(4);
      break;
    case 'D':
      lcd.clear();
      Set_state(6);
      break;
    default:
      Set_state(5);
    }
    break;

  case 6:
    switch (key)
    {
    case 'N':
      lcd.clear();
      Min_OFF=kpd.waitForKey();
      lcd.setCursor(1,0);
      lcd.print ("--- MIN OFF ---");
      lcd.setCursor(7,1);
      lcd.print (Min_OFF);
      break;
    case 'E':
      lcd.clear();
      Set_state(5);
      break;
    case 'D':
      lcd.clear();
      Set_state(7);
      break;
    default:
      Set_state(6);
    }
    break;

  case 7:
    switch (key)
    {
    case 'U':
      //case bUp:
      lcd.clear();
      Set_state(6);
      break;
    case 'D':
      //case bDown:
      lcd.clear();
      Set_state(8);
      break;
      Set_state(7);
    }
    break;

  case 8:
    //switch (CheckButton()) {
    //case bChange:
    switch (key)
    {
    case 'U':
      lcd.clear();
      Set_state(1);
      break;
    case 'D':
      lcd.clear();
      Set_state(7);
      break;
      Set_state(8);
    }
    break;
  default:;
  }
}

You only show part of your code.

Anyway, you need to set up some sort of timer, which is initialised by the value you enter on the keypad. Then you print the countdown to the LCD every second.

So you press a keypad key, store the value and print it to the LCD, then wait for an "Enter" keypress, (or maybe a "Cancel" or "Delete", in case you hit the wrong key).
Then, when "Enter" is pressed, you start the timer and countdown display.

You could use 'millis()'-based timing for your timer and LCD updates. Based on:-

unsigned long currentMillis = millis();
if(currentMillis - prevTimerMillis >= 1000)
{
    // Do the timer update and display here.

    prevTimerMillis += 1000;
}

'prevTimerMillis' must be declared globally, as 'unsigned long'.
If you want more help, you need to show all of your code, not just a part of it. (As 'groundfungus' said while I was typing.)

Thank you one more friend. I constructing a timer, which once inserted in the time display, the arduino1 sends the signal to the arduino2 to activate a relay.

But when I use the '1' (case 6 for exemple) key of the keypad display shows the number 52

My objective My code this moment

LCD display LCD display


Time ON / OFF Time ON / OFF
00:00 hh:mm 54:54 -----> 54:54


#include <EEPROM.h>
#include <virtuabotixRTC.h>
#include <Keypad.h>
// myRTC(clock, data, rst)
virtuabotixRTC myRTC(6, 7, 8);

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define CE_PIN   9
#define CSN_PIN 10

const byte FILAS = 5; // Cinco filas
const byte COLS = 4; // Cuatro columnas
char keys[FILAS][COLS] = {
  {'N', 'M', '#', '*'},
  {'1', '2', '3', 'U'},
  {'4', '5', '6', 'D'},
  {'7', '8', '9', 'C'},
  {'L', '0', 'R', 'E'},
};
byte filasPins[FILAS] = {5, 4, 3, 2, 14}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {15, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad kpd = Keypad( makeKeymap(keys), filasPins, colPins, FILAS, COLS );
/*
const byte rows=4;
const byte cols=4;
char keys[rows][cols]=

{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowpins[rows]={9,8,7,6};
byte colpins[cols]={5,4,3,2};

Keypad kpd=Keypad(makeKeymap(keys),rowpins,colpins,rows,cols);
*/

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

char state = 1;
//char stat;
char letra;

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataToSend[10] = "1";
char txNum = '0';

int addr = 0;

int Hora_ON = 0;
int Hora_OFF = 0;
int Min_ON = 0;
int Min_OFF = 0;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  Wire.begin();
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.openWritingPipe(slaveAddress);
  myRTC.setDS1302Time(00, 58, 23, 2, 17, 11, 2014);

}

void loop()
{
  myRTC.updateTime();

    //char key = kpd.getKey();
  char key = kpd.waitForKey();
  if (key)
  {
    switch (state) {
      case 1:
        switch (key) {
          case 'U':
            //case bUp:
            lcd.clear(); Set_state(8);
            break;
          case 'D':
            //case bDown:
            lcd.clear(); Set_state(2);
            break;
          default:
            Set_state(1);
        }
        break;

      case 2:
        //switch (CheckButton()) {
        //case bChange:
        switch (key) {
          case 'U':
            lcd.clear(); Set_state(1);
            break;
          case 'D':
            lcd.clear(); Set_state(3);
            break;
          default:
            Set_state(2);
        }
        break;

      case 3:
        switch (key) {
          case 'R':
            lcd.clear(); Hora_ON++;
            lcd.setCursor(1, 0);
            lcd.print ("-- Hora ON --");
            lcd.setCursor(7, 1);
            lcd.print (Hora_ON);
            break;
          case 'E':
            lcd.clear(); Set_state(2);
            break;
          case 'D':
            lcd.clear(); Set_state(4);
            break;
          default:
            Set_state(3);
        }
        break;

      case 4:
        switch (key) {
          case 'N':
            lcd.clear(); Min_ON = kpd.waitForKey();
            lcd.setCursor(1, 0);
            lcd.print ("--- MIN ON ---");
            lcd.setCursor(7, 1);
            lcd.print (Min_ON);
            break;
          case 'E':
            lcd.clear(); Set_state(3);
            break;
          case 'D':
            lcd.clear(); Set_state(5);
            break;
          default:
            Set_state(4);
        }
        break;

      case 5:
        switch (key) {
          case 'R':
            lcd.clear(); Hora_OFF++;
            lcd.setCursor(1, 0);
            lcd.print ("-- Hora OFF --");
            lcd.setCursor(7, 1);
            lcd.print (Hora_OFF);
            break;
          case 'E':
            lcd.clear(); Set_state(4);
            break;
          case 'D':
            lcd.clear(); Set_state(6);
            break;
          default:
            Set_state(5);
        }
        break;

      case 6:
        switch (key) {
          case 'N':
            lcd.clear(); Min_OFF = kpd.waitForKey();
            lcd.setCursor(1, 0);
            lcd.print ("--- MIN OFF ---");
            lcd.setCursor(7, 1);
            lcd.print (Min_OFF);
            break;
          case 'E':
            lcd.clear(); Set_state(5);
            break;
          case 'D':
            lcd.clear(); Set_state(7);
            break;
          default:
            Set_state(6);
        }
        break;

      case 7:
        switch (key) {
          case 'U':
            //case bUp:
            lcd.clear(); Set_state(6);
            break;
          case 'D':
            //case bDown:
            lcd.clear(); Set_state(8);
            break;
            Set_state(7);
        }
        break;

      case 8:
        //switch (CheckButton()) {
        //case bChange:
        switch (key) {
          case 'U':
            lcd.clear(); Set_state(1);
            break;
          case 'D':
            lcd.clear(); Set_state(7);
            break;
            Set_state(8);
        }
        break;
      default:;
    }
  }
}

void Set_state(char index) {
  state = index;
  switch (state) {
    case 1:
      lcd.setCursor(0, 0);
      lcd.print ("Irrigation TIMER");
      lcd.setCursor(0, 1);
      lcd.print ("  By N. Duarte");

      break;

    case 3:

      lcd.setCursor(0, 0);
      lcd.print("----TIMER-ON----");
      lcd.setCursor(0, 1);
      lcd.print(    Hora_ON, DEC);
      lcd.print("   horas");
      break;

    case 4:

      lcd.setCursor(0, 0);
      lcd.print("----TIMER-ON----");
      lcd.setCursor(0, 1);
      lcd.print(    Min_ON, DEC);
      lcd.print("   minutos");
      break;

    case 5:

      lcd.setCursor(0, 0);
      lcd.print("---TIMER-OFF---");
      lcd.setCursor(0, 1);
      lcd.print(    Hora_OFF, DEC);
      lcd.print("   horas");
      break;

    case 6:

      lcd.setCursor(0, 0);
      lcd.print("---TIMER-OFF---");
      lcd.setCursor(0, 1);
      lcd.print(    Min_OFF, DEC);
      lcd.print("   minutos");
      break;

    case 2:

      lcd.setCursor(0, 0);
      lcd.print("----PROGRAMA----");
      lcd.setCursor(1, 1);
      lcd.print(Hora_ON, DEC);
      lcd.setCursor(3, 1);
      lcd.print(":");
      lcd.setCursor(4, 1);
      lcd.print(Min_ON, DEC);
      lcd.setCursor(7, 1);
      lcd.print("-->");
      lcd.setCursor(11, 1);
      lcd.print(Hora_OFF, DEC);
      lcd.setCursor(13, 1);
      lcd.print(":");
      lcd.setCursor(14, 1);
      lcd.print(Min_OFF, DEC);
      break;

    case 7:

      lcd.setCursor(0, 0);
      lcd.print("---TIMER-001---");
      if (myRTC.hours < Hora_ON && myRTC.hours > Hora_OFF && myRTC.minutes < Min_ON  && myRTC.minutes > Min_OFF) {
        lcd.setCursor(0, 1);
        lcd.print("Setor-01-ON");
      }
      else
      {
        lcd.setCursor(2, 1);
        lcd.print("Setor-01-OFF");
      }
      break;

    case 8:
      lcd.setCursor(0, 0);
      lcd.print("Modo de operacao");
      break;
    default:;
  }
}
//====================

void send() {
  if (myRTC.hours < Hora_ON && myRTC.hours > Hora_OFF && myRTC.minutes < Min_ON  && myRTC.minutes > Min_OFF) {
    bool rslt;
    rslt = radio.write( &dataToSend, sizeof(dataToSend) );
  }
}