Hi again guys. I'm struggling here. I have cobbled together some code mainly from Nick's tutorial.
I'm trying to get the basics going for now, as I said I want to monitor two sensors but I'm just looking at one for now.
The aim of the code was to read a digital pin, and if that pin is high to set another digital pin high, and also store the time the event happened.
The i added another if statement, which would look at the millis since the event, and if they are equal or above 5000 to turn off the led.
It doesn't seem to work, the led just goes on and off when I change the pin state, but does not stay on for 5000 ms. I want it to work from the pulse output of a PIR so if the pin is low I don't want to turn off the led until the time has elapsed.
What am i doing wrong?
// Which pins are connected to which LED
const byte greenLED = 9;
const byte redLED = 10;
int const onTime = 5000; //constant on time)
int val = 0; // variable for reading the pin status
// Variable holding the timer value so far. One for each "Timer"
unsigned long left;
unsigned long right;
void setup ()
{
pinMode (greenLED, OUTPUT);
pinMode (redLED, OUTPUT);
pinMode(2, INPUT);
left = millis ();
right = millis ();
} // end of setup
void loop()
{
val = digitalRead(2); // read input value
if (val == HIGH)
{
digitalWrite(9, HIGH);
left = millis ();
}
if (left >= 5000);
{
digitalWrite(9, LOW);
}
}