[go: up one dir, main page]

0% found this document useful (0 votes)
45 views2 pages

Int MotionSensor

Uploaded by

Sadap Nian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

Int MotionSensor

Uploaded by

Sadap Nian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

int motionSensor = D1;

void setup() {
// put your setup code here, to run once:

Serial.begin(115200);

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);

pinMode(motionSensor, INPUT);

delay(1500);

Serial.println("Motion sensor test.");


}

unsigned long currentTime;

const unsigned long motionCheckInterval = 150UL;


unsigned long previousMotionCheckTime = 0;

void loop() {
// put your main code here, to run repeatedly:

currentTime = millis();

if (currentTime - previousMotionCheckTime >=


motionCheckInterval) {

int isMotionDetected = digitalRead(motionSensor);

if (isMotionDetected == 0) {

Serial.println("Motion ended!");
digitalWrite(LED_BUILTIN, HIGH);
} else {

Serial.println("Motion detected!");
digitalWrite(LED_BUILTIN, LOW);
}

previousMotionCheckTime = currentTime;
}
}

You might also like