How to combine 2 coding?

first coding i want for the lcd display timer countdown in minutes and second

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//Counter to change positions of pages
long minutes=0, second =120;
long countdown = (minutes*60)+ second;
long start;

int button = 5;        //Enter button
int selectstate = 0;   //Set the select to 0, 1 when user press the button. 
int led = 13;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  pinMode(button, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  lcd.setCursor(0,0);
  lcd.print("HELLO WORLD");
  long start = millis();
}

void loop() {
  // put your main code here, to run repeatedly:

//count the time has passed
      long countdowntime_seconds = 60 - (millis() - start)/1000;

  if (countdowntime_seconds >= 0) {
    
    //conversion to min and sec
    long countdown_minute = ((countdowntime_seconds / 60) % 60);
    long countdown_sec = countdowntime_seconds % 60;
 
    lcd.setCursor(5, 1);
    if (countdown_minute < 2) {
      lcd.print("0");
    }
    lcd.print(countdown_minute);
    lcd.print(":");
    if (countdown_sec < 120) {
      lcd.print("0");
    }
    lcd.print(countdown_sec);

    //times up
    if (countdowntime_seconds == 0) {
      digitalWrite(led, LOW);
    }
  }
  
 
}

second coding i want the part where 2 button with different timer countdown for example button 1 = 1 min and button 2 = 2min

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
int pinButton1 = 5;
int pinButton2 = 4;
int LED = 13;
int timer1 = 5000;
int timer2 = 10000;

void setup()
{
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("UV LED SYSTEM");
  lcd.setCursor(2,1);
  lcd.print("DISINFECTION");
  delay (2000);
  lcd.clear ();
  pinMode( LED, OUTPUT );
  pinMode( pinButton1, INPUT_PULLUP );
  pinMode( pinButton2, INPUT_PULLUP );
  long start = millis();
}

void loop()
{
  static uint32_t timerStart = 0;
  static uint32_t timerDuration = 0;

  if ( timerDuration != 0 && millis() - timerStart >= timerDuration )
  {
      digitalWrite( LED, LOW );
      timerDuration = 0;
      lcd.print( "LED off" );
  }

  if ( timerDuration == 0 )
  {
    if( digitalRead( pinButton1 ) == LOW )
    {
      timerDuration = timer1;
    }
  }
    else if( digitalRead( pinButton2 ) == LOW )
    {
      timerDuration = timer2;
     
    }

    if ( timerDuration != 0 )
    {
      digitalWrite( LED, HIGH );
      timerStart = millis();
     lcd.setCursor(3,0);
     lcd.print( "LED on for " );
      lcd.print( timerDuration / 1000 );
      lcd.setCursor(3,0);
      lcd.print( " seconds" );
    }
  }


i try to combine it but cannot get the result and become more confused.

So show us your best effort, and explain why it fell short of your expectations.

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
int pinButton1 = 5;
int pinButton2 = 4;
int LED = 13;
long minutes=0, second =60;
long countdown = (minutes*60)+ second;
int timer1 = 60;
int timer2 = 120;
long start;

void setup()
{
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("UV LED SYSTEM");
  lcd.setCursor(2,1);
  lcd.print("DISINFECTION");
  delay (2000);
  lcd.clear ();
  pinMode( LED, OUTPUT );
  pinMode( pinButton1, INPUT_PULLUP );
  pinMode( pinButton2, INPUT_PULLUP );
  long start = millis();
}

void loop()
{
static uint32_t timerStart = 0;
static uint32_t timerDuration = 0;
 long countdowntime_seconds = (millis() - start)/1000;

  if ( timerDuration != 0 && millis() - timerStart >= timerDuration )
  {
      digitalWrite( LED, LOW );
      timerDuration = 0;
      lcd.print( "LED off" );
  }

  if ( timerDuration == 0 )
  {
    if( digitalRead( pinButton1 ) == LOW )
    {
      timerDuration = timer1;
      long countdowntime_seconds = timer1 - (millis() - start)/1000;

    }
  }
    else if( digitalRead( pinButton2 ) == LOW )
    {
      timerDuration = timer2;
      long countdowntime_seconds = timer2 - (millis() - start)/1000;
    }

     if (countdowntime_seconds >= 0) {
    
    //conversion to min and sec
    long countdown_minute = ((countdowntime_seconds / 60) % 60);
    long countdown_sec = countdowntime_seconds % 60;
 
    lcd.setCursor(5, 1);
    if (countdown_minute < 2) {
      lcd.print("0");
    }
    lcd.print(countdown_minute);
    lcd.print(":");
    if (countdown_sec < 120) {
      lcd.print("0");
    }
    lcd.print(countdown_sec);
     }
}

previously already done many time so my previous coding is gone. so when i simulate nothing come out. this is the new one that i try.

Does the two different codes work compiled and downloaded one at the time?

im sorry i dont understant, what do you mean?

If You compile, download and run the first code, does it work as You want?
The same for the second code....

for the first code it have problem when it simulate the timer countdown is working but led is not on. i though its ok because i just need the countdown coding and the lcd display the countdown coding.

for second code is working and have no problem.

Making the first code working would be the best. Don't mix a faulty code into another code.

owhhhh because i try to fixed it but can only use simulation because my lcd has problem. someone suggest me to do it with real lcd first but i cannot do it. so i stuck

How do You perform the simulation?
Code, working in the reality is what You need? Post that code and schematics so the fault can be corrected.

Do the right thing, if you have enough time. Go back to first principles. Study and understand the display and display code, and the other hardware and code. Then write your own, just use the software you found for hints.


i simulate using proteus using this coding

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//Counter to change positions of pages
long minutes=0, second =60;
long countdown = (minutes*60)+ second;
long start;

int button = 5;        //Enter button
int selectstate = 0;   //Set the select to 0, 1 when user press the button. 
int led = 13;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  pinMode(button, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  lcd.setCursor(0,0);
  lcd.print("HELLO WORLD");
  long start = millis();
}

void loop() {
  // put your main code here, to run repeatedly:

//count the time has passed
      long countdowntime_seconds = (millis() - start)/1000;

  if (countdowntime_seconds >= 0) {
    
    //conversion to min and sec
    long countdown_minute = ((countdowntime_seconds / 60) % 60);
    long countdown_sec = countdowntime_seconds % 60;
 
    lcd.setCursor(5, 1);
    if (countdown_minute < 2) {
      lcd.print("0");
    }
    lcd.print(countdown_minute);
    lcd.print(":");
    if (countdown_sec < 120) {
      lcd.print("0");
    }
    lcd.print(countdown_sec);

    //times up
    if (countdowntime_seconds == 0) {
      digitalWrite(led, LOW);
    }
  }
  
 
}

its because i still dont understand that why i ask here. I will try to study again thank you for your advised

Simulation is not always useful because it does not use the actual powering and wiring You have.

owhhh i see. alright thank you

Why use the I2C I/O expander? It complicates things. Connect the LCD directly to the UNO. You still have plenty of I/O left. Know that the analog pins A0 - A3 can be used as digital I/O.

Hi, im having a problem because i confuse how to edit the coding. I found coding that can on the led for 5 second after pressing a push button. but when i want to add another pushbutton to on led for 10 second i become lost. can anyone help me.

int pinButton1 = 5;
int pinButton2 = 4;
int LED = 13;
int timer1 = 5000;
int timer2 = 10000;


void setup(){
  pinMode(13,OUTPUT); // LED output
  pinMode(5,INPUT); // Button input
  pinMode(4,INPUT); // Button input
}

void loop()
{
  static unsigned char ledState = LOW;
  static unsigned char buttonState1 = LOW;
  static unsigned char buttonState2 = LOW;
  static unsigned char lastButtonState1 = LOW;
  static unsigned char lastButtonState2 = LOW;
  static unsigned long ledCameOn = 0;
  
  // If the LED has been on for at least 5 seconds then turn it off.
  if(ledState == HIGH)
  {
    if(millis()-ledCameOn > 5000)
    {
      digitalWrite(13,LOW);
      ledState = LOW;
    }
  }

  // If the button's state has changed, then turn the LED on IF it is not on already.
  buttonState = digitalRead(5);
  if(buttonState != lastButtonState)
  {
    lastButtonState = buttonState;
    if((buttonState == HIGH) && (ledState == LOW))
    {
      digitalWrite(13,HIGH);
      ledState = HIGH;
      ledCameOn = millis();
    }
  }
}

Welcome

Here is an example timer_2_buttons.ino - Wokwi Arduino and ESP32 Simulator (updated)

thank you i will try it now

thank you its working.