// Constants for pins
const int LDR_PIN = A0; // LDR sensor pin
const int TRIGGER_PIN = 9; // Ultrasonic sensor trigger pin
const int ECHO_PIN = 10; // Ultrasonic sensor echo pin
const int LED_PIN = 13; // LED pin
// Constants for array values
const int LDR_ARRAY_VALUE = 1;
const int ULTRASONIC_ARRAY_VALUE = 2;
const int DONE_ARRAY_VALUE = 3;
// Function prototypes
void readLDR();
void readUltrasonic();
void printMessage(const char* message);
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(LDR_PIN, INPUT); // Set LDR pin as input
pinMode(TRIGGER_PIN, OUTPUT); // Set trigger pin as output
pinMode(ECHO_PIN, INPUT); // Set echo pin as input
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
}
void loop() {
int dataArray[] = {1, 2, 3}; // Array of values {1, 2, 3}
for (int i = 0; i < sizeof(dataArray) / sizeof(dataArray[0]); i++) {
Serial.println(i);
int dataValue = dataArray[i]; // Get the current value from the array
if (dataValue == LDR_ARRAY_VALUE) {
readLDR();
}
else if (dataValue == ULTRASONIC_ARRAY_VALUE) {
readUltrasonic();
}
else if (dataValue == DONE_ARRAY_VALUE) {
printMessage("Done");
}
digitalWrite(LED_PIN, HIGH); // Turn on LED to indicate data read
delay(500); // Wait for 0.5 seconds
digitalWrite(LED_PIN, LOW); // Turn off LED
delay(500); // Wait for 0.5 seconds
}
}
void readLDR() {
int ldrValue = analogRead(LDR_PIN); // Read LDR sensor value
if (ldrValue < 100) { // Assuming low intensity if LDR value is less than 100
printMessage("LIGHT LOW");
}
}
void readUltrasonic() {
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 * 0.034 / 2; // Convert time to distance
if (distance < 20) { // Assuming near if distance is less than 20 cm
printMessage("Distance Near");
}
}
void printMessage(const char* message) {
Serial.println(message);
}