Here's something from somebody smarter than a chatbot. I'll explain a little as I go.
First we move all the stuff in the for loop to the loop function. We're going to let that be our loop. We're going to also have to get rid of the delay calls. delay is cool in example code where you're just demonstrating what a function does, but in the real world it causes code to be non-responsive. You can't read buttons while in a delay state.
So at the top we add a few variables to help up track what's going on. I've tried to give them names that explain their function.
byte relayState = LOW;
unsigned long lastSwitchTime = 0;
unsigned long switchingInterval = 1000;
int count = 100;
I also added pins for the up and down buttons. Change these to suit your setup
const int upButton = 2;
const int downButton = 3;
Next we have setup and that's just going to setup our pins and Serial and the lcd.
void setup() {
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0, 0);
//-------------------------------------
delay(900);
Serial.begin(19200);
pinMode(RELAY1, OUTPUT);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
}
And lastly we have loop and I'll break it down a little.
We start by asking if the count variable is greater than 0. If it is then we have another if statement that asks if it has been longer than 1000ms since the last switching. If it has then we have another if statement that checks to see the current state of the relay, if we are at the start or end of a cycle. At the end of all this we call digitalWrite with the state we determine and set the last switched time so we can check next time. Then after all of those if statements are closed out we have some code to read the buttons.
Notice how much easier it is to read this code with the if statement blocks lines up. You can use the auto-format tool in the IDE and it will do this for you. It makes it so much easier to follow your code.
Here's the whole thing. It's untested as I don't have your hardware but it should be pretty close. I'm hoping you'll take the time to understand it. It's really pretty simple.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define RELAY1 10
const int upButton = 2;
const int downButton = 3;
//----------------------------------------------------------
byte relayState = LOW;
unsigned long lastSwitchTime = 0;
unsigned long switchingInterval = 1000;
int count = 100;
void setup() {
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0, 0);
//-------------------------------------
delay(900);
Serial.begin(19200);
pinMode(RELAY1, OUTPUT);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
}
void loop() {
unsigned long currentTime = millis();
if (count > 0) {
if (currentTime - lastSwitchTime >= switchingInterval) {
if (relayState == LOW) {
Serial.println(count);
lcd.clear();
lcd.print(count);
relayState = HIGH;
} else {
relayState = LOW;
count--;
}
digitalWrite(RELAY1, relayState);
lastSwitchTime = currentTime;
}
}
if(digitalRead(upButton) == LOW){
count++;
}
if(digitalRead(downButton) == LOW){
count--;
}
}