Help programming a 'safety button' to stop circuit input and output


I'm very new to using Arduino, so i'm aware this might be an easy solve, but I can't seem to get my head around it.

I'm using the Arduino Uno R3.

I currently have a circuit with x2 push buttons (one of which is programmed to be a switch) and an LED.

The aim is for the LED to only turn on when the push button (safetyButtonPin) is held down and then the push 'switch' (onButtonPin) is clicked. This then runs a 'timer' (delay) until the LED turns off (as long as the push button (safetyButtonPin) is still being held down).

I've managed to programme the majority of this, however, I am not sure how to programme that the LED turns off if the push button (safetyButtonPin) is release before the delay time is over.

If anyone can help i'd really appreciate it! (code below)

const int safetyButtonPin = 2;       // the number of the safetybutton pin
const int onButtonPin = 3;           // the number of the onbutton pin
const int uvLEDPin1 =  4;            // the number of the UV LED pin

int safetyButtonState = 0;           // variable for reading the pushbutton status
int LEDState = 0;
int onButtonNew;
int onButtonOld = 1;
int dt = 100;

void setup() {
  pinMode(uvLEDPin1, OUTPUT);
  pinMode(safetyButtonPin, INPUT_PULLUP);
  pinMode(onButtonPin, INPUT);

void loop() {
  if (digitalRead(safetyButtonPin) == LOW){
  digitalWrite (uvLEDPin1, LOW);
  LEDState = 0;
} else {
  if (digitalRead(safetyButtonPin) == HIGH){
    onButtonNew = digitalRead(onButtonPin);
    if (onButtonOld == 0 && onButtonNew == 1){
    digitalWrite(uvLEDPin1, HIGH);
    delay (3000);
    LEDState = 1;
    if (digitalRead(safetyButtonPin) == LOW){
onButtonOld = onButtonNew;

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:

Also, you will find that the delay(...) function is not helpful for what you want to do. Learn to use the millis() function. There is a tutorial if you want help.

Once you use delay(3000), you are stuck for three seconds. There are exceptions (such as using an interrupt) that have their own problems. millis() may be harder to use initially but is well worth learning.


"Using millis() for timing. A beginners guide"

for more information.