setting digitalWrite with a time limit for HIGH and LOW

Hi all, I am new to the arduino community and would greatly appreciate some help. I am using arduino as a controller in my car to control actuators and motors to power some electric exhaust cutouts and some spoilers(racing application)

I've written the base code, and it works exactly how I want it to as far as switching power on the digital outputs from HIGH to LOW.

My issue is the timing functions. I want the digtalWrite outputs on my code (HIGH only) to only stay HIGH for 2 seconds, and then switch to LOW. I've been playing with the millis command but it seems that Arduino keeps counting as the program runs, and since the program is for a car ( which will see some of the IF statements many many times), i need to have a simple way to either reset the counter as soon as a leg of IF statement finishes, or find some other way for the arduino to count to 2 sec and then switch states

Here is the code I have right now, I have tested it and is looking great so far

const int SPUP=2;
const int SPDOWN=4;
const int EXCLOSED=7;
const int EXOPEN=8;

void setup(){
pinMode(SPUP, OUTPUT);
pinMode(SPDOWN, OUTPUT);
pinMode(EXCLOSED, OUTPUT);
pinMode(EXOPEN, OUTPUT);
}
void loop(){
if ((analogRead(1)>=68) && (analogRead(1)<130)){
if(analogRead(0)>512){
digitalWrite(SPUP, HIGH); // every HIGH in this code must last for only 2 seconds then switch to LOW
digitalWrite(SPDOWN, LOW);
}else{
digitalWrite(SPUP, LOW);
digitalWrite(SPDOWN, HIGH);
}
}
if(analogRead(1)>=130){
digitalWrite(SPUP, HIGH);
digitalWrite(SPDOWN, LOW);
}if (analogRead(1)<68){
digitalWrite(SPUP, LOW);
digitalWrite(SPDOWN, HIGH);
}
if(analogRead(2)<255){
digitalWrite(EXCLOSED, HIGH);
digitalWrite(EXOPEN, LOW);
}
if (analogRead(2)>=255){
digitalWrite(EXCLOSED, LOW);
digitalWrite(EXOPEN, HIGH);
}
}

I would love if you guys could simply show me something I could copy and paste in, but if not, at least a helping hand in the general dirrection would be greatly appreciated.. i've bbeen racking my brain on this one for too long! :slight_smile:

Thanks again!

Andrew

right after

digitalWrite(SPUP, HIGH);

put:

delay(2000);

it is in milliseconds so thats why you use 2000.

I know that command, however, the loop will keep the high on for 2 secs, then off( for a fraction of a second and then repeat. I need the HIGH to stay off for the remainder of that if statement until that part of the code is once again found true.
im pretty sure i can not do this:

digitalWrite(SPUP, HIGH);
delay(2000);
digtalWrite(SPUP, LOW);
delay( a gazillion seconds);

if you understand what I mean.

{digitalWrite(SPUP, HIGH);delay(2000);}

delay(2);
}

That would work if i could get those sections of the IF statements to onyl do one iteration... but they keep looping so the light keeps turning on (so fast it looks like it never turns off, but theoretically speaking this is no different from the very basic loop for led flashing except the led time off is a mere fraction of a millisecond)

Create a flag variable. Only execute the if-statement when the flag is true. set the flag to false inside of the if-statement. When enough time has passed (you can use millis()) or some other event occurs, set the flag true again.

James,

Why didnt I think of that?! It worked perfectly
Here's my fixed code

Thanks!

const int SPUP=2;
const int SPDOWN=4;
const int EXCLOSED=7;
const int EXOPEN=8;
int A=0;
int B=0;
int C=0;
int D=0;
int X=0;
int Z=2;

void setup(){
pinMode(SPUP, OUTPUT);
pinMode(SPDOWN, OUTPUT);
pinMode(EXCLOSED, OUTPUT);
pinMode(EXOPEN, OUTPUT);
}
void loop(){
if ((analogRead(1)>=68) && (analogRead(1)<130)){
C=0;
D=0;
if(A==0){
if(analogRead(0)>512){
B=0;
digitalWrite(SPUP, HIGH);
delay(2000);
digitalWrite(SPUP, LOW);
digitalWrite(SPDOWN, LOW);
A=1;
delay(10);
}
}
if(B==0){
if(analogRead(0)<=512){
A=0;
digitalWrite(SPUP, LOW);
digitalWrite(SPDOWN, HIGH);
delay(2000);
digitalWrite(SPDOWN, LOW);
B=1;
delay(10);
}
}
}
if(D==0){
if(analogRead(1)>=130){
C=0;
digitalWrite(SPUP, HIGH);
delay(2000);
digitalWrite(SPUP, LOW);
digitalWrite(SPDOWN, LOW);
D=1;
delay(10);
}
}
if(C==0){
if (analogRead(1)<68){
D=0;
digitalWrite(SPDOWN, HIGH);
delay(2000);
digitalWrite(SPDOWN, LOW);
digitalWrite(SPUP, LOW);
C=1;
delay(10);
}
}
if(X==0){
if(analogRead(2)<255){
Z=2;
digitalWrite(EXCLOSED, HIGH);
delay(2000);
digitalWrite(EXCLOSED, LOW);
digitalWrite(EXOPEN, LOW);
X=1;
delay(10);
}

}
if(Z==2){
if (analogRead(2)>=255){
X=0;
digitalWrite(EXOPEN, HIGH);
delay(2000);
digitalWrite(EXCLOSED, LOW);
digitalWrite(EXOPEN, LOW);
Z=3;
delay(10);
}
}
}