Advertisement
belrey10

Day 4: Movement in the Shadows

Nov 3rd, 2024 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int photoPin = A0;    // the pin where the photoresistor is connected
  2. const int ledPin = 13;      // the pin where the LED is connected
  3. const int buzzerPin = 12;   // the pin where the buzzer is connected (optional)
  4.  
  5. int lightLevel = 0;         // variable to store the light level
  6. int threshold = 100;        // threshold for light change detection
  7.  
  8. void setup() {
  9.   pinMode(ledPin, OUTPUT);      // initialize the LED pin as an output
  10.   pinMode(buzzerPin, OUTPUT);   // initialize the buzzer pin as an output (optional)
  11.   Serial.begin(9600);           // begin serial communication for debugging
  12. }
  13.  
  14. void loop() {
  15.   lightLevel = analogRead(photoPin);  // read the light level from the photoresistor
  16.   Serial.println(lightLevel);         // print the light level to the serial monitor for debugging
  17.  
  18.   if (lightLevel < threshold) {       // if the light level is below the threshold
  19.     digitalWrite(ledPin, HIGH);       // turn on the LED
  20.     digitalWrite(buzzerPin, HIGH);    // turn on the buzzer (optional)
  21.   } else {
  22.     digitalWrite(ledPin, LOW);        // turn off the LED
  23.     digitalWrite(buzzerPin, LOW);     // turn off the buzzer (optional)
  24.   }
  25.   delay(100);                         // small delay to stabilize the readings
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement