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!
Thanks again!
Andrew