Help with delay in loop

I'll do my best to explain my issue, here goes..
A simple closed switch on D2, sets D13 HIGH using the PULLUP method and fires a Solenoid. I NEED a delay of about 3000ms from the time the switch is closed before the Solenoid kicks on (I got that part working) My problem is safety, I need the Solenoid to shut off Immediately and as soon as the switch opens. It's not doing that now because of the built in delay. Is there a different way to Code this to make the Solenoid shut down as soon as the switch opens?
**(I simplified the code using the LED for testing purposes. I'm using a MOSFET on that pin before the Solenoid)

const int BUTTON_PIN = 2;  // the number of the switch pin
const int SOL_PIN =  13;   // the number of the Solenoid pin


int switchState = 0;   // variable for reading the switch status

void setup() {
  // initialize the Solenoid pin as an output:
  pinMode(SOL_PIN, OUTPUT);
  // initialize the JB4 pin as a pull-up input:
  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  // read the state of the switch value:
  switchState = digitalRead(BUTTON_PIN);

  // control the Solenoid according to the state of button
  if(switchState == LOW) { // If switch is closed
    delay (3000);
    digitalWrite(SOL_PIN, HIGH); // turn on Solenoid
    }
  else                          { // otherwise, switch is not closed
    digitalWrite(SOL_PIN, LOW); } // turn off Solenoid
       
    
    }

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

What a phenomenal tutorial! I was able to wrap my head around the example for your specific task. I just have to brain storm and apply the millis() to what I need it to do.

The basic flow will be
if switch is closed and flag reset, record start time and energize solenoid, set flag
if switch is open, de-energize solenoid, reset flag
if flag is set then check current time - start time and if exceeded, energize solenoid and set flag

Do each of these tests every time through loop.

Lets see if I got this right..

You want to click a button, wait 3 seconds then turn on something.
Then, when the button is released, turn it off. - Even if it hasn't yet been turned on.

If so.. Here's the silver bullet approach...

#include <mechButton.h>
#include <idlers.h>
#include <timeObj.h>


#define BUTTON_PIN   2           // Pin we'll hook the button to. The other side hooks to ground.
#define SOL_PIN      13          // Usual pin number for built in LED.

timeObj vTimer(3000,false);      // Setup a 3 sec timer.
mechButton button(BUTTON_PIN);   // Set button to pin 2.


// Your standard sketch setup()
void setup() {
   
   Serial.begin(9600);              // Fire up our serial monitor thing.
   pinMode(SOL_PIN,OUTPUT);         // Set up the LED pin for output.
   button.setCallback(myCallback);  // Set up our callback. (Also calls hookup() for idling.)
}


// This is the guy that's called when the button changes state.
void myCallback(void) {

   Serial.print("Button just became ");
   if (button.trueFalse()) {
      Serial.println("true!");
      digitalWrite(SOL_PIN, LOW);
   } else {
      Serial.println("false!");
      vTimer.start();
   }
}


// Your standard sketch loop()
void loop() {
      
   idle();                             // Let all the idlers have time to do their thing.
   if (!button.trueFalse() && vTimer.ding()) { digitalWrite(SOL_PIN, HIGH); }
}

If you are interested in trying this, you will need to install LC_baseTools from the library manager.

-jim lee

@jimlee Yes, exactly! micro switch is pressed full throttle.. 3 sec delay then solenoid fires... If switch is opened, cut off solenoid immediately. I will try that code, thanks for your help!!

Darrin

There are two issues in your code:

sir-william:
@jimlee Yes, exactly! micro switch is pressed full throttle.. 3 sec delay then solenoid fires... If switch is opened, cut off solenoid immediately. I will try that code, thanks for your help!!

Darrin

Is this a NOX system?

-jim lee

if closing the valve is essential I would think about circuitry that switches off the solenoid even in case of a failure of the microcontroller.

I mean something like how emergency-buttons are wired. Normally closed. Any opening at any place initiates emergency-cut-off. In your case this could be something like a watchdog-circuitry that must get an impulse every second or even more often to keep a relay-contact closed (normally opened). As soon as the pulses don't come anymore relay opens solenoid cutoff.

best regards Stefan

@jimLee Yes it is

Ha! Thought so. Car or boat?

-jim lee

@jimLee it's a twin turbo car I'm adding Nitrous to :slight_smile:
ps, that code worked like a champ!