*SOLVED* Help with programming a timer

Hi everyone, I’m hoping someone can shed some light on some difficulties I am having programming my Arduino.

I have written a program for my project at university. Basically it is designed to turn on or off an output (pin 12) dependant on 3 variables.

The first variable is a wireless receiver (pin 2), and the other two are switches (pin 3 and pin 4 - set high).

So, I want to make pin 12 high when pin 2 AND pin 3 are high. (Pin 12 latched on, pin 2 is a momentary press of a button and will become low again after button is released.)

I then want a timer to run for 10 seconds while pin 12 is high.

I want one of three scenarios to make pin 12 low.

Either the timer runs out OR pin 3 is made low OR pin 4 is made low.

At present I have this all working, except the timer. I have been working on the timer for nearly two days now.

I tried using millis, however the timer is erratic. I believe this is because pin 12 isn’t always high, whilst the Arduino is constantly checking to see if the current time - previous time is more than 10 seconds. So, it cycles every 10 seconds and not when pin 12 is high.

I also attempted to use the for function, with no success.

I also tried to do a simple count loop. N=N-1 - loops until N=0 at which point it would make pin 12 low. Didn’t work.

I am sure I am missing something really simple here and I have done many searches to try and help.

I hope this makes sense!

Why don't post your code so we can see what you've done?

Yeah, sure.

/* Turns on screen resistance wire by using transmitter/receiver, battery monitor and ice sensor input */

//constants //set pin numbers int screen = 12; //the number of the screen resistance wires pin int receiver = 2; //the number of the receiver pin int battmon = 3; //the number of the battery monitor pin int icesensor = 4; //the number of the ice sensor pin

//variable int receiverstate = 0; //variable for reading the receiver int battmonstate = 0; //varibale for reading the battery monitor int icesensorstate = 0; //varibale for reading the ice sensor long previousMillis = 0; long screenoff = 10000;

void setup() {

pinMode(screen, OUTPUT); //initialise the LED pin as an output pinMode(receiver, INPUT); //initialise the receiver pin as an input pinMode(battmon, INPUT); //initialise the battmon pin as an input pinMode(icesensor, INPUT); //initialise the icesenor pin as an input }

void loop(){ receiverstate = digitalRead(receiver); //read the state of the receiver battmonstate = digitalRead(battmon); //read the state of the battery monitor icesensorstate = digitalRead(icesensor);//read the state of the ice sensor unsigned long currentMillis = millis();

//check if the receiver has been activated, if it has, the receiver state is HIGH //check if the battery has enough power, if it does, the battmon state is LOW if ((receiverstate == HIGH) && (battmonstate == LOW)) { //turn LED on digitalWrite(screen, HIGH);

}

else if ((battmonstate == HIGH) || (icesensorstate == HIGH)) { digitalWrite(screen, LOW);

}

if (currentMillis - previousMillis > screenoff) { previousMillis = currentMillis; digitalWrite(screen, LOW); }

}

Pin 12 seems to be set low at the end of the 10 second time cycle no matter what point it enters.

I used a stopwatch, reset the Arduino, started the timer and left it running for 6 seconds. I then made pin 12 high and it went off after 4 seconds. I need a way of starting the cycle or resetting previous time to current time when pin 12 is made high.

I've solved my problem! Thanks anyway.