[text] AAAAAAAAAA

Viewer

copydownloadembedprintName: AAAAAAAAAA
  1. int led = 13;                // the pin that the LED is atteched to
  2. int sensor = 2;              // the pin that the sensor is atteched to
  3. int state = LOW;             // by default, no motion detected
  4. int val = 0;                 // variable to store the sensor status (value)
  5.  
  6. void setup() {
  7.   pinMode(led, OUTPUT);      // initalize LED as an output
  8.   pinMode(sensor, INPUT);    // initialize sensor as an input
  9.   Serial.begin(9600);        // initialize serial
  10. }
  11.  
  12. void loop(){
  13.   val = digitalRead(sensor);   // read sensor value
  14.   if (val == HIGH) {           // check if the sensor is HIGH
  15.     digitalWrite(led, HIGH);   // turn LED ON
  16.     delay(100);                // delay 100 milliseconds 
  17.     
  18.     if (state == LOW) {
  19.       Serial.println("Motion detected!"); 
  20.       state = HIGH;       // update variable state to HIGH
  21.     }
  22.   } 
  23.   else {
  24.       digitalWrite(led, LOW); // turn LED OFF
  25.       delay(200);             // delay 200 milliseconds 
  26.       
  27.       if (state == HIGH){
  28.         Serial.println("Motion stopped!");
  29.         state = LOW;       // update variable state to LOW
  30.     }
  31.   }
  32. }

Editor

You can edit this paste and save as new:


File Description
  • AAAAAAAAAA
  • Paste Code
  • 18 Apr-2024
  • 1.1 Kb
You can Share it: