Unit 15: Light Resistance

 

 

 

Code : 


const int LDR_PIN = A0;        // LDR connected to analog pin A0
const int BUZZER_PIN = 8;      // Buzzer connected to digital pin 8
const int RED_PIN = 9;          // Red RGB LED pin
const int GREEN_PIN = 10;       // Green RGB LED pin
const int BLUE_PIN = 11;        // Blue RGB LED pin

void setup() {
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void setColor(int red, int green, int blue) {
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

void loop() {
  int ldrValue = analogRead(LDR_PIN);

  if (ldrValue > 950) {
    // Bright light
    setColor(0, 0, 255);   // Blue
    noTone(BUZZER_PIN);    // Turn off buzzer
  } else if (ldrValue <= 950 && ldrValue > 900) {
    // Partial light
    setColor(255, 165, 0); // Amber (approximation)
    noTone(BUZZER_PIN);    // Turn off buzzer
  } else {
    // Low light
    setColor(255, 0, 0);   // Red
    tone(BUZZER_PIN, 1000); // Sound buzzer at 1000Hz
  }

  delay(100); // Small delay for stability
}

  

Notes: