Rookie needs help with a simple delay, and inputs

I am new to this, and so far, i have made a LED turn on with a 12v input one shot signal, and turn of again after 5 second

is it possible, (and how to) if a made another input, when that goes on the delay stops immediately, and turn off the LED, and the LED can´t be turned on before the new input is LOW again ??

int ledPin = 13; // choose the pin for the LED
int inPin = 8;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    delay(5000);                  // waits for 5 second
    digitalWrite(ledPin, LOW);  // turn LED OFF
  }  
}

I hope it was understandable

//Brian (denmark)

delay() is blocking and nothing else can be done until the full delay elapses. It cannot be interrupted mid-delay.

You could break it up into (very) short delays in a loop which also watches for the second input, or you could look into using millis() instead of delay, as shown in the example "BlinkWithoutDelay" sketch.

Also, you say "i have made a LED turn on with a 12v input one shot signal"

I hope you're not connecting the 12V directly to an Arduino pin.

study blink with out delay example and look at INPUT_PULLUP. you will need to change the code example to suit a input .

int ledPin = 13; // choose the pin for the LED
int inPin = 8;   // choose the input pin (for a pushbutton)
byte forceOff = 7;
unsigned long previousMillis = 0;
unsigned long interval = 5000;
byte lightled = 0;

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(forceOff, INPUT_PULLUP);  // declare LED as output
  pinMode(inPin, INPUT_PULLUP);    // declare pushbutton as input
}

void loop() {
  unsigned long currentMillis = millis();
  byte val = digitalRead(inPin);  // read input value
  byte offbutton = digitalRead(forceOff);  // read input value

  if ((val == LOW) && (offbutton == HIGH)) {
    lightled = 1;
  }
  if (offbutton == LOW) {
    lightled = 0;
  }

  if (lightled == 1) {
    if (currentMillis - previousMillis <= interval) {
      digitalWrite(ledPin, HIGH);  // turn LED ON
    } else {
      lightled = 0;
    }
  } else {//lighted not equal 1
    digitalWrite(ledPin, LOW);  // turn LED OFF
    previousMillis = currentMillis;
  }
}

gpop1:
study blink with out delay example and look at INPUT_PULLUP. you will need to change the code example to suit a input .

int ledPin = 13; // choose the pin for the LED

int inPin = 8;   // choose the input pin (for a pushbutton)
byte forceOff = 7;
unsigned long previousMillis = 0;
unsigned long interval = 5000;
byte lightled = 0;

void setup() {
 pinMode(ledPin, OUTPUT);  // declare LED as output
 pinMode(forceOff, INPUT_PULLUP);  // declare LED as output
 pinMode(inPin, INPUT_PULLUP);    // declare pushbutton as input
}

void loop() {
 unsigned long currentMillis = millis();
 byte val = digitalRead(inPin);  // read input value
 byte offbutton = digitalRead(forceOff);  // read input value

if ((val == LOW) && (offbutton == HIGH)) {
   lightled = 1;
 }
 if (offbutton == LOW) {
   lightled = 0;
 }

if (lightled == 1) {
   if (currentMillis - previousMillis <= interval) {
     digitalWrite(ledPin, HIGH);  // turn LED ON
   } else {
     lightled = 0;
   }
 } else {//lighted not equal 1
   digitalWrite(ledPin, LOW);  // turn LED OFF
   previousMillis = currentMillis;
 }
}

Most of those brackets are unnecessary and only serve to make the code harder to read.
It can be done more simply, too.

Since Brian's original version used active-high inputs, I've kept true to that, since I thought there might have been a good reason, but they're easily converted to active-low with input pullups and 'hard' resistors removed if necessary:-

// Connect pushbuttons between input pins and +5V. (Not 12V)
// and resistors from input pins to ground.

const byte pLED = 13;                       // LED on pin 13, active-high.
const byte inSig1 = 8;                      // First input signal on pin 8, active-high.
const byte inSig2 = 7;                      // Second input signal on pin 7, active-high.
unsigned long prevMillis;                   // Will hold millis() count.

void setup()
{
    pinMode(pLED, OUTPUT);                  // Make LED pin an output.
    pinMode(inSig1, INPUT);                 // Make signal pins inputs.
    pinMode(inSig2, INPUT);                 //  "     "     "     "
}

void loop()
{
    while (digitalRead(inSig1) == LOW){}    // Wait for first signal.
    digitalWrite(pLED, HIGH);               // Got first signal - turn LED on.
    prevMillis = millis();                  // Save current 'millis' value.
    while (millis() - prevMillis < 5000)    // Keep LED on for 5 seconds.
    {
      if (digitalRead(inSig2) == HIGH)      // Check if second signal has arrived and if so
        break;                              // break out of 'while' loop.
    }
    digitalWrite(pLED, LOW);                // Turn LED off.
    while (digitalRead(inSig2) == HIGH){}   // Wait for second signal to end before continuing.
}

Either way, now Brian's seen two different ways of doing it.
There's more than one way to skin a cat. (39, according to the robot in 'Lost in Space'. :smiley: )

And Brian, note that the second pushbutton/input signal is connected to pin 7.

Brian, this is the active-low version, with input pullups:-

// Connect pushbuttons between input pins and ground.
// No additional resistors needed.

const byte pLED = 13;                       // LED on pin 13, active-high.
const byte inSig1 = 8;                      // First input signal on pin 8, active-low.
const byte inSig2 = 7;                      // Second input signal on pin 7, active-low.
unsigned long prevMillis;                   // Will hold millis() count.

void setup()
{
    pinMode(pLED, OUTPUT);                  // Make LED pin an output.
    pinMode(inSig1, INPUT_PULLUP);          // Make signal pins inputs.
    pinMode(inSig2, INPUT_PULLUP);          //  "     "     "     "
}

void loop()
{
    while (digitalRead(inSig1) == HIGH) {}   // Wait for first signal.
    digitalWrite(pLED, HIGH);               // Got first signal - turn LED on.
    prevMillis = millis();                  // Save current 'millis' value.
    while (millis() - prevMillis < 5000)    // Keep LED on for 5 seconds.
    {
        if (digitalRead(inSig2) == LOW)    // Check if second signal has arrived and if so
            break;                          // break out of 'while' loop.
    }
    digitalWrite(pLED, LOW);                // Turn LED off.
    while (digitalRead(inSig2) == LOW) {}  // Wait for second signal to end before continuing.
}

So you have one input that turns on the output for 5 seconds, and another that turns off the output immediately.

What happens if the first input is triggered during the 5 second delay? Should it be ignored, or should it reset the time?

What should happen if the cancel button is pressed while the first input is being held down?

What should happen if the first input is pressed while the cancel button is being held down?

What should happen if someone holds down the first input for an appreciable amount of time? Should the 5 seconds be counted from the moment the button is pressed down, or from the moment it is released?

If the time is to be counted from the moment the button is pushed down, what happens if the first input is held down for more than 5 seconds? Should another 5 seconds be counted from the moment the previous 5 seconds finishes, or should the nput be ignored unless it is released and pressed again?

Just two buttons and a delay gives you 32 scenarios to think about.… nope: 48 come to think of it.

button A previous state (2)

  • button A new state (2)
  • button B previous state (2)
  • button B new state (2)
  • (
    timeout not active +
    timeout active, and has timed out +
    timeout active, and has not timed out
    ) (3)

PaulMurrayCbr:
So you have one input that turns on the output for 5 seconds, and another that turns off the output immediately.

What happens if the first input is triggered during the 5 second delay? Should it be ignored, or should it reset the time?

What should happen if the cancel button is pressed while the first input is being held down?

What should happen if the first input is pressed while the cancel button is being held down?

What should happen if someone holds down the first input for an appreciable amount of time? Should the 5 seconds be counted from the moment the button is pressed down, or from the moment it is released?

If the time is to be counted from the moment the button is pushed down, what happens if the first input is held down for more than 5 seconds? Should another 5 seconds be counted from the moment the previous 5 seconds finishes, or should the nput be ignored unless it is released and pressed again?

Just two buttons and a delay gives you 32 scenarios to think about.… nope: 48 come to think of it.

button A previous state (2)

  • button A new state (2)
  • button B previous state (2)
  • button B new state (2)
  • (
    timeout not active +
    timeout active, and has timed out +
    timeout active, and has not timed out
    ) (3)

As an absolute beginner, I don't think that Brian was thinking to that level, and just wanted a simple example to get him started. That's what his post indicates.
Adding too much complexity would just make it much harder for him to understand what's going on. I personally think that the above examples are enough for the moment.
We must learn to crawl before we can walk, then walk before we can run.

Of course, you could also write and post a version covering all of the possibilites that you mention, so he can see how it 'really' should be done.