I'm having a hard time with using millis in my sketch. I have read UKHeliBob's "Using millis() for timing. A beginners guide" Using millis() for timing. A beginners guide - Introductory Tutorials - Arduino Forum but it's not registering in my head for how I want to use it, I need some extra guidance. I want to have LED strips come on in my hallway at night for 15 seconds then go off until next detection. I am using an Arduino Nano with a PIR sensor to activate a relay. Later I will add the LED strips and two more PIRs to my project. Can someone help with the use of millis in my code? Thank you in advance.
(Side note: I originally wanted to use IR sensors for smart cars in my project instead of the PIR sensors but the range was too short. If anyone has any suggestions on something other than the PIR sensors, that would be greatly appreciated also.)
int relayPin = 12; // choose the pin for the LED
int pir_Pin = 11; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
boolean relayState = true; // relayState used to set relay
unsigned long lastLedTime = 0; // will store last time relay was updated (the last time LED turned on or off)
unsigned long ledOn = 15000; // milliseconds of on-time for relay (LED on 15 seconds)
void setup() {
pinMode(relayPin, OUTPUT); // declare LED as output
pinMode(pir_Pin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop() {
unsigned long currentTime = millis();
val = digitalRead(pir_Pin); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// // we have just turned on
Serial.println("Motion detected!");
// // We only want to print on the output change, not state
pirState = HIGH;
}
relayState = true;
digitalWrite(relayPin, LOW);// turn LED ON
lastLedTime = currentTime;// Save the time
} else {
digitalWrite(relayPin, HIGH); // turn LED OFF
if (pirState == HIGH) {
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}