Advertisement
belrey10

Day 7: The Lost Beacon

Nov 3rd, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int buttonPin1 = 2;   // the number of the first button pin
  2. const int buttonPin2 = 3;   // the number of the second button pin
  3. const int buttonPin3 = 4;   // the number of the third button pin
  4. const int ledPin1 = 8;      // the number of the first LED pin
  5. const int ledPin2 = 9;      // the number of the second LED pin
  6. const int ledPin3 = 10;     // the number of the third LED pin
  7. const int photoPin = A0;    // the pin where the photoresistor is connected
  8.  
  9. int buttonState1 = 0;       // variable for reading the first button status
  10. int buttonState2 = 0;       // variable for reading the second button status
  11. int buttonState3 = 0;       // variable for reading the third button status
  12. int ledBrightness = 0;      // variable for LED brightness based on photoresistor
  13.  
  14. void setup() {
  15.   pinMode(buttonPin1, INPUT);  // initialize the first button pin as an input
  16.   pinMode(buttonPin2, INPUT);  // initialize the second button pin as an input
  17.   pinMode(buttonPin3, INPUT);  // initialize the third button pin as an input
  18.   pinMode(ledPin1, OUTPUT);    // initialize the first LED pin as an output
  19.   pinMode(ledPin2, OUTPUT);    // initialize the second LED pin as an output
  20.   pinMode(ledPin3, OUTPUT);    // initialize the third LED pin as an output
  21.   Serial.begin(9600);          // begin serial communication for debugging
  22. }
  23.  
  24. void loop() {
  25.   // read the state of the pushbutton values:
  26.   buttonState1 = digitalRead(buttonPin1);
  27.   buttonState2 = digitalRead(buttonPin2);
  28.   buttonState3 = digitalRead(buttonPin3);
  29.  
  30.   // read the photoresistor value and map it to LED brightness:
  31.   int sensorValue = analogRead(photoPin);
  32.   ledBrightness = map(sensorValue, 0, 1023, 0, 255);
  33.  
  34.   // check if the pushbuttons are pressed. If they are, the buttonState is HIGH:
  35.   if (buttonState1 == HIGH) {
  36.     analogWrite(ledPin1, ledBrightness);  // adjust LED brightness based on light level
  37.   } else {
  38.     analogWrite(ledPin1, 0);   // turn LED off
  39.   }
  40.  
  41.   if (buttonState2 == HIGH) {
  42.     analogWrite(ledPin2, ledBrightness);  // adjust LED brightness based on light level
  43.   } else {
  44.     analogWrite(ledPin2, 0);   // turn LED off
  45.   }
  46.  
  47.   if (buttonState3 == HIGH) {
  48.     analogWrite(ledPin3, ledBrightness);  // adjust LED brightness based on light level
  49.   } else {
  50.     analogWrite(ledPin3, 0);   // turn LED off
  51.   }
  52.  
  53.   // print the sensor values to the serial monitor for debugging
  54.   Serial.print("Light level: ");
  55.   Serial.println(sensorValue);
  56.   Serial.print("LED brightness: ");
  57.   Serial.println(ledBrightness);
  58.  
  59.   delay(100);  // wait for a short period before repeating the loop
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement