It is best to look at when a switch changes state than the level.
You should scan your switches every ~50ms to remove debounce problems.
unsigned long lastMillis;
unsigned long startMillis;
const unsigned long period = 10000ul; //10 second time period
const byte Relay1 = 10; //Relay pin
const int inputPin = 33; // switch input
byte lastInputStatus;
boolean timingFlag = false;
//****************************************************************************
void setup()
{
Serial.begin(115200);
pinMode(Relay1, OUTPUT);
pinMode(inputPin, INPUT_PULLUP);
lastInputStatus = digitalRead(inputPin);
}
//****************************************************************************
void loop()
{
//***************************
//is timing enabled and has the period expired?
if (timingFlag == true && millis() - startMillis > period)
{
digitalWrite(Relay1, HIGH);
//disable timing
timingFlag = false;
}
//***************************
//time to check the switches?
if (millis() - lastMillis >= 50)
{
lastMillis = millis();
checkSwitches();
}
} //END of loop()
//****************************************************************************
void checkSwitches()
{
int inputStatus = digitalRead (inputPin);
//Has switch changed state?
if (lastInputStatus != inputStatus)
{
//Yes it has, update to the state
lastInputStatus = inputStatus;
//is the switch LOW?
if (inputStatus == LOW)
{
//time the switch went LOW
startMillis = millis();
//enabling timing
timingFlag = true;
}
//the switch must have gone HIGH then
else
{
digitalWrite(Relay1, LOW); // <-----<<<< remove this line for relay to stay as is
//disable timing
timingFlag = false;
}
}
} //END of checkSwitches()
//****************************************************************************