mirror of
https://github.com/meshtastic/meshtastic.git
synced 2024-12-28 23:19:47 -08:00
Merge pull request #736 from htcheroportugal/projectopir
update pir code
This commit is contained in:
commit
79bba3290e
|
@ -265,26 +265,31 @@ while True:
|
||||||
|
|
||||||
#### Arduino Mini Pro Code
|
#### Arduino Mini Pro Code
|
||||||
|
|
||||||
```c++
|
|
||||||
int LED = 13; // the pin that the LED is atteched to
|
int LED = 13; // the pin to which the LED is connected
|
||||||
int PIR = 2; // the pin that the sensor is atteched to
|
int PIR = 2; // the pin to which the sensor is connected
|
||||||
|
int previousState = LOW; // previous state of the sensor
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(LED, OUTPUT); // initialize LED as an output
|
pinMode(LED, OUTPUT); // initialize the LED as an output
|
||||||
pinMode(PIR, INPUT); // initialize sensor as an input
|
pinMode(PIR, INPUT); // initialize the sensor as an input
|
||||||
Serial.begin(9600); // initialize serial
|
Serial.begin(9600); // initialize serial communication
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
if (digitalRead(PIR) == HIGH) { // check if the sensor is HIGH
|
int currentState = digitalRead(PIR); // read the current state of the sensor
|
||||||
digitalWrite(LED, HIGH); // turn LED ON
|
if (currentState != previousState) { // check if the state has changed
|
||||||
Serial.write(":Motion!:");
|
if (currentState == HIGH) { // check if there is motion
|
||||||
delay(10000); // delay 100 milliseconds
|
digitalWrite(LED, HIGH); // turn the LED on
|
||||||
}
|
Serial.println("Motion Detected");
|
||||||
else {
|
|
||||||
digitalWrite(LED, LOW); // turn LED OFF
|
|
||||||
Serial.write("Motion stopped!");
|
|
||||||
delay(10000); // delay 100 milliseconds
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
else {
|
||||||
|
digitalWrite(LED, LOW); // turn the LED off
|
||||||
|
Serial.println("No Motion");
|
||||||
|
}
|
||||||
|
previousState = currentState; // update the previous state
|
||||||
|
}
|
||||||
|
delay(100); // small delay to avoid false sensor readings
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue