If button 1 is pressed, exit the loop and set the relay to a normally open state. void loop() { button1State = digitalRead(button1_Gtech); button2State = digitalRead(button2_In);
if (button2State == LOW && !loopActive) { loopActive = true; isPaused = true; startTime = millis(); duration = random(0, 5000); } } If button 2 is pressed, start the random run and pause loop, with the duration ranging from 0 to 5 s. if (loopActive) { if (isPaused) { if (millis() - startTime >= duration) { isPaused = false; digitalWrite(relayPin, HIGH); startTime = millis(); duration = random(0, 5000); } } else { if (millis() - startTime >= duration) { isPaused = true; digitalWrite(relayPin, LOW); startTime = millis(); duration = random(0, 5000);} } }