Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1071

PIR motion sensor delay

$
0
0

I'm having some trouble with setting up a PIR motion sensor. Basically, I want every time a trigger happens to add to a count that is saved on a text file. This is to track a small animal entering and exiting a pe The problem I'm having is the delay between triggers. I want a delay of about 2 seconds between triggers, however the lowest I can get is about 5 seconds. The first set of sensors I bought turned out to have an internal limit of 5 seconds, so I bought a second one that can go down to .5 seconds. Again, I'm having the same issue where the delay is too long. This time it can range from about 4-6 seconds, even with the potentiometers at a minimum. It is set on a single trigger mode. There is no delay in my code either. Anyone have any tips? Am I just testing it wrong (waving my hand by it) or should I try to get a different sensor? If so, any recommendations? Here is my code if anything helps:

#include <SD.h>
#include <SPI.h>

File myFile;
const int chipSelect = 4; // Pin for SD module

// pin for the PIR sensor
const int sensorPin = 3;

// Counter for trigger
unsigned long count = 0;

void printFormattedTime(unsigned long currentTime) {
    unsigned long seconds = currentTime / 1000;
    unsigned long minutes = seconds / 60;
    unsigned long hours = minutes / 60;
    seconds %= 60;
    minutes %= 60;
    
    myFile.print(hours);
    myFile.print(":");
    if (minutes < 10) myFile.print("0");
    myFile.print(minutes);
    myFile.print(":");
    if (seconds < 10) myFile.print("0");
    myFile.println(seconds);
}

void setup() {
    Serial.begin(9600);
    pinMode(sensorPin, INPUT);

    if (!SD.begin(chipSelect)) {
        Serial.println("SD Card initialization failed!");
        return;
    }
    Serial.println("SD Card initialized.");
}

void loop() {
    if (digitalRead(sensorPin) == HIGH) {
        count++;
        Serial.println(count);
        logEvent(count);
        delay(1500);
        
        //while (digitalRead(sensorPin) == HIGH) {
            //delay(100); 
        //}
        

    }

}

void logEvent(unsigned long count) {
    myFile = SD.open("log.txt", FILE_WRITE);
    if (myFile) {
        myFile.print("PIR Sensor Triggered - Count: ");
        myFile.print(count);
        myFile.print(" - Time: ");
        printFormattedTime(millis());
        myFile.close();
    } else {
        Serial.println("Error opening log file");
    }
}

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1071

Trending Articles