Need help adding a delay/duration to a digitalWrite function

Hello, first off thanks for helping,

Introduction: I have a simple program I need help with. This is my first arduino project so I am a very novice. I have tried to find a solution to my problem online first but I cant quite find someone doing what I am or at least I dont know the right "buzz" words to make a effective search on the topic.

Problem: I have a flame effect that I would like to power the ignition source and electric solenoid valve with one button. I am using a taser as my ignition source but it only needs to run for 1 -2 seconds before it should turn off BUT I want my electronic solenoid valve to stay open as long as I push my momentary switch. I cant find out how to accomplish this.

Here is my code so far to just turn on both the Taser and electronic solenoid on when I push my momentary switch. I tried messing with delay and millis stuff a little but could not get it to work how I wanted.

[u]Fire Effects Program[/u]

// pin definitions int tazPin = 2; int solPin = 3; int buttonPin = 4; int toggleState; unsigned long time_since_last_reset = 0; int duration = 2000;

void setup() { pinMode(tazPin, OUTPUT); pinMode(solPin, OUTPUT); pinMode(4,INPUT_PULLUP); Serial.begin(9600); delay(150); }

void loop() { toggleState = digitalRead(buttonPin); if (toggleState == LOW) { Serial.println("Button is pushed."); digitalWrite(solPin, LOW); digitalWrite(tazPin, LOW);

}else{ Serial.println("Button is NOT pushed."); digitalWrite(tazPin, HIGH); digitalWrite(solPin, HIGH); }


I know this is a simple problem so please excuse my lack of knowledge. I am excited that I am now diving into the world of arduinos! the scopes of my projects have now expanded greatly

Welcome to the forums! Please read the sticky post at the top of the forum about how to properly post code (using code tags) so that people can more easily help you.

Based on your problem description, I would recommend looking at two examples:

BlinkWithoutDelay (File->Examples->02 Digital ->BlinkWithoutDelay) StateChangeDetection (File->Examples->02 Digital ->StateChangeDetection)

The first one will show you how to measure elapsed time without your programming stopping everything else

The second one will show you how to react to a button state change (OFF->ON = start flame, ON->OFF = turn off)

Good Luck!

Take a look at Using millis() for timing. A beginners guide for a full explanation of how to use millis() for timing

Consider counting down within the loop as your timer for the delay. It may help to organize your application logic using a diagram and then write your code based on the diagram too. For an example please check out Flame Effect Control Logic, Simulation and Sketch (at or Flame Effect Control Logic, Simulation and Sketch (at