Good day ALL,
I am a beginner Arduino user and would appreciate your assistance with the following:
- I have an Arduino MEGA with a PIR sensor connected to DIO pin 2 and a 5v solid state relay connected to DIO pin 6.
- When motion is detected by the PIR I wish to turn on the relay and start to countdown for a pre-determined period....
- Each instance motion is detected during the countdown I would like to restart the countdown timer and
- After the period has elapsed the relay would be returned off.
I have attempted this with the code attached, however the relay does not turn off based on my condition used.
Could someone please assist??
Thank You
PIR2.ino (936 Bytes)
Rather than post a file with your code (which means that someone must have the Arduino software ready to run on their computer, rather than just reading this with a browser on whatever device) post with code tags.
Type the following: [code]
paste your code [/code]
That way, everyone will be able to see (and help debug if possible) your code.
Apologies, see code below:
int PIR1=2;
int PIR1state;
int lastPIR1state;
boolean PIR1DT;
int relay1=6;
unsigned int lastMillis=0;
unsigned int previousMillis = 0;
void setup() {
pinMode(PIR1, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(9600);
Serial.println("LDR Light Sensor Example Sketch");
}
void loop()
{
PIR1state = digitalRead(PIR1);
//Serial.println(PIR1state);
Serial.println(lastMillis - previousMillis);
if ((PIR1state != lastPIR1state) and (PIR1state == 1))
{
lastMillis = millis();
//Serial.println("Entered");
//delay(1);
PIR1DT=true;
}
if (PIR1DT == true) //and (lastMillis - previousMillis <= lastMillis - previousMillis + 3000))
{
while ((lastMillis - previousMillis <= lastMillis - previousMillis + 300))
{
digitalWrite(relay1,PIR1DT);
}
PIR1DT=false;
digitalWrite(relay1,PIR1DT);
previousMillis=lastMillis;
}
lastPIR1state = PIR1state;
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
while ((lastMillis - previousMillis <= lastMillis - previousMillis + 300))
{
digitalWrite(relay1, PIR1DT);
}
How can this while loop ever end as none of the variables in the loop condition are changed within it ? An if may be more appropriate
See Using millis() for timing. A beginners guide for advice on the use of millis() for timing.
Save the millis() value at the time that the start action happens. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly and maybe save the start time for the next activity. If not, then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().
NOTE : All timing variables when using millis() should be unsigned long to avoid problems at rollover to zero
@UKHeliBob
Thanks for your help, I had a look at the beginners guide for using millis() as a timer and it looks like ive done a few things wrong. I will update and let you guys know if I come into another issue.
THANKS!
I am glad that you found the guide useful. Whether or not you get the program working can I suggest that you post it here for further advice as there may be ways to optimise it or prevent future problems even if it works.