Advertisement
belrey10

Day 6: The Guardians of Cogsworth

Nov 3rd, 2024 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int photoPin1 = A0;     // Pin for the first photoresistor
  2. const int photoPin2 = A1;     // Pin for the second photoresistor
  3. const int ledPin1 = 8;        // Pin for the first LED
  4. const int ledPin2 = 9;        // Pin for the second LED
  5. const int buttonPin1 = 2;     // Pin for the first button
  6. const int buttonPin2 = 3;     // Pin for the second button
  7. const int lightThreshold = 500;  // Light threshold for LED activation
  8.  
  9. int lightLevel1 = 0;          // Variable for the first photoresistor reading
  10. int lightLevel2 = 0;          // Variable for the second photoresistor reading
  11. int buttonState1 = 0;         // Variable for the first button state
  12. int buttonState2 = 0;         // Variable for the second button state
  13.  
  14. void setup() {
  15.   pinMode(ledPin1, OUTPUT);     // Initialize the first LED pin as an output
  16.   pinMode(ledPin2, OUTPUT);     // Initialize the second LED pin as an output
  17.   pinMode(buttonPin1, INPUT);   // Initialize the first button pin as an input
  18.   pinMode(buttonPin2, INPUT);   // Initialize the second button pin as an input
  19.   Serial.begin(9600);           // Start serial communication for debugging
  20. }
  21.  
  22. void loop() {
  23.   // Read the light levels from the photoresistors
  24.   lightLevel1 = analogRead(photoPin1);
  25.   lightLevel2 = analogRead(photoPin2);
  26.  
  27.   // Read the states of the buttons
  28.   buttonState1 = digitalRead(buttonPin1);
  29.   buttonState2 = digitalRead(buttonPin2);
  30.  
  31.   // Print the light levels for debugging
  32.   Serial.print("Light level 1: ");
  33.   Serial.println(lightLevel1);
  34.   Serial.print("Light level 2: ");
  35.   Serial.println(lightLevel2);
  36.  
  37.   // Control the first LED based on the photoresistor and button
  38.   if (lightLevel1 < lightThreshold || buttonState1 == HIGH) {
  39.     digitalWrite(ledPin1, HIGH);  // Turn on the first LED if light is below threshold or button is pressed
  40.   } else {
  41.     digitalWrite(ledPin1, LOW);   // Turn off the LED if light is above threshold and button isn't pressed
  42.   }
  43.  
  44.   // Control the second LED based on the second photoresistor and button
  45.   if (lightLevel2 < lightThreshold || buttonState2 == HIGH) {
  46.     digitalWrite(ledPin2, HIGH);  // Turn on the second LED if light is below threshold or button is pressed
  47.   } else {
  48.     digitalWrite(ledPin2, LOW);   // Turn off the LED if light is above threshold and button isn't pressed
  49.   }
  50.  
  51.   delay(100);  // Short delay to stabilize readings
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement