Advertisement
belrey10

Day 8: The Echoing Call of Safety

Nov 3rd, 2024 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int buttonPin1 = 2;  // the pin where the first button is connected
  2. const int buttonPin2 = 3;  // the pin where the second button is connected
  3. const int ledPin1 = 9;     // the pin where the first LED is connected
  4. const int ledPin2 = 10;     // the pin where the second LED is connected
  5. const int forkPin = 7;     // the pin where the fork is connected
  6. const int photoPin = A0;   // the pin where the photoresistor is connected
  7.  
  8. int buttonState1 = 0;      // variable for reading the first button status
  9. int buttonState2 = 0;      // variable for reading the second button status
  10. int forkState = 0;         // variable for reading the fork status
  11. int photoValue = 0;        // variable for reading the photoresistor value
  12.  
  13. void setup() {
  14.   pinMode(buttonPin1, INPUT);  // initialize the first button pin as an input
  15.   pinMode(buttonPin2, INPUT);  // initialize the second button pin as an input
  16.   pinMode(ledPin1, OUTPUT);    // initialize the first LED pin as an output
  17.   pinMode(ledPin2, OUTPUT);    // initialize the second LED pin as an output
  18.   pinMode(forkPin, INPUT);     // initialize the fork pin as an input
  19.   pinMode(photoPin, INPUT);    // initialize the photoresistor pin as an input
  20.   Serial.begin(9600);          // begin serial communication for debugging
  21. }
  22.  
  23. void loop() {
  24.   buttonState1 = digitalRead(buttonPin1);  // read the state of the first button
  25.   buttonState2 = digitalRead(buttonPin2);  // read the state of the second button
  26.   forkState = digitalRead(forkPin);        // read the state of the fork
  27.   photoValue = analogRead(photoPin);       // read the value from the photoresistor
  28.  
  29.   if (buttonState1 == HIGH && forkState == HIGH) {
  30.     int brightness = map(photoValue, 0, 1023, 0, 255);  // map the photoresistor value to a brightness level
  31.     analogWrite(ledPin1, brightness);  // adjust first LED brightness based on light level
  32.     Serial.println("First button pressed and fork touched! LED 1 on with brightness adjusted.");
  33.   } else {
  34.     digitalWrite(ledPin1, LOW);    // turn first LED off
  35.   }
  36.  
  37.   if (buttonState2 == HIGH && forkState == HIGH) {
  38.     int brightness = map(photoValue, 0, 1023, 0, 255);  // map the photoresistor value to a brightness level
  39.     analogWrite(ledPin2, brightness);  // adjust second LED brightness based on light level
  40.     Serial.println("Second button pressed and fork touched! LED 2 on with brightness adjusted.");
  41.   } else {
  42.     digitalWrite(ledPin2, LOW);    // turn second LED off
  43.   }
  44.  
  45.   delay(100);  // wait for a short period before repeating the loop
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement