I am programming the Arduino Uno Rev3 to wait for a 12v signal from my vehicle when it goes into reverse. Then power the motor to control my passenger side mirror to move down for 3 seconds, then when it is taken out of reverse, power it back up . I have found a signal source for the reverse gear and the 12v power source for up and a different source for down. I am having difficulty figuring out the coding. This is what I have so far.
setup(){
pinmode(2,INPUT);//reverse gear
pinmode(3,OUTPUT);//mirror down
pinmode(4,OUTPUT);//mirror up
}
loop(){
if ((2) == HIGH){
digitalWrite(3,HIGH);//control the mirror down for 3 seconds
delay(3000);
digitalWrite(3,LOW);
}
//having difficulty here
digitalWrite(4,HIGH);
I believe I have figured out how to put the mirror down, but I want the arduino to wait for the signal to power off again and then power pin 4 for 3 seconds to put the mirror back up.
You need to detect not the state of the input pin, you need to detect
a change in the state of the pin. Otherwise you'll continue to move
the mirror forever.
There are 3 relevant conditions: pin hasn't changed since you last looked,
pin has gone LOW->HIGH, pin has gone HIGH->LOW.
Two of those mean you do something with the mirror for 3 seconds,
the first means you don't do anything with the mirror.
So you need to remember what the pin was last time - a global variable
will do that.