diff --git a/docs/configuration/module-config/serial.mdx b/docs/configuration/module-config/serial.mdx index 1c27bd76..7d1e0a3f 100644 --- a/docs/configuration/module-config/serial.mdx +++ b/docs/configuration/module-config/serial.mdx @@ -253,7 +253,7 @@ while True: #### Arduino Mini Pro Wiring -![image](/img/modules/Serial/arduino-mini-pro-pir-wiring.png) +![image](https://i.ibb.co/g3ZffzT/pir.png) - T-BEAM RX PIN 13 to TX PIN on the ARDUINO MINI - T-BEAM TX PIN 14 to RX PINon the ARDUINO MINI @@ -265,26 +265,32 @@ while True: #### Arduino Mini Pro Code -```c++ -int LED = 13; // the pin that the LED is atteched to -int PIR = 2; // the pin that the sensor is atteched to +```cpp +int LED = 13; // the pin to which the LED is connected +int PIR = 2; // the pin to which the sensor is connected +int previousState = LOW; // previous state of the sensor + void setup() { - pinMode(LED, OUTPUT); // initialize LED as an output - pinMode(PIR, INPUT); // initialize sensor as an input - Serial.begin(9600); // initialize serial +pinMode(LED, OUTPUT); // initialize the LED as an output +pinMode(PIR, INPUT); // initialize the sensor as an input +Serial.begin(9600); // initialize serial communication } void loop(){ - if (digitalRead(PIR) == HIGH) { // check if the sensor is HIGH - digitalWrite(LED, HIGH); // turn LED ON - Serial.write(":Motion!:"); - delay(10000); // delay 100 milliseconds - } - else { - digitalWrite(LED, LOW); // turn LED OFF - Serial.write("Motion stopped!"); - delay(10000); // delay 100 milliseconds - } + int currentState = digitalRead(PIR); // read the current state of the sensor + if (currentState != previousState) { // check if the state has changed + if (currentState == HIGH) { // check if there is motion + digitalWrite(LED, HIGH); // turn the LED on + Serial.println("Motion Detected"); } -``` \ No newline at end of file + 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 + +} +```