const int LDR_PIN = A0; // LDR pin
const int GREEN_LED_PIN = 2; // Green LED pin
const int RED_LED_PIN = 3; // Red LED pin
const int TRIGGER_PIN = 7; // Ultrasonic sensor trigger pin
const int ECHO_PIN = 6; // Ultrasonic sensor echo pin
void setup() {
pinMode(LDR_PIN, INPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
int sensorData = random(1, 4); // Simulate reading the array of {1, 2, 3}
if (sensorData == 1) {
int intensity = analogRead(LDR_PIN);
if (intensity < 100) { // Assuming low intensity if LDR reading is less than 100
digitalWrite(GREEN_LED_PIN, HIGH);
} else {
digitalWrite(GREEN_LED_PIN, LOW);
}
}
else if (sensorData == 2) {
long duration, distance;
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration / 2) / 29.1; // Calculate distance from duration
Serial.println(distance);
if (distance < 20) {
digitalWrite(RED_LED_PIN, HIGH);
} else {
digitalWrite(RED_LED_PIN, LOW);
}
}
else if (sensorData == 3) {
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, HIGH);
}
delay(1000); // Delay for stability
}