const int analogPin = 4; // Analog input pin
const int LED1 = 18; // Digital output pin for LED1
const int LED2 = 19; // Digital output pin for LED2
const int LED3 = 21;
int val = 0; // Variable to store the raw analog value
int mappedVal = 0; // Variable to store the mapped value
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 bps
pinMode(LED1, OUTPUT); // Set LED1 pin as an output
pinMode(LED2, OUTPUT); // Set LED2 pin as an output
pinMode(LED3, OUTPUT);
}
void loop() {
val = analogRead(analogPin); // Read the analog input
mappedVal = map(val, 0, 4095, 0, 100); // Map the analog value to a range of 0 to 100
if (mappedVal >= 70) { // If the mapped value is 50 or greater
digitalWrite(LED1, HIGH); // Turn on LED1
digitalWrite(LED2, LOW); // Turn off LED2
digitalWrite(LED3, LOW);
} else if (mappedVal >= 31) { // If the mapped value is less than 50
digitalWrite(LED1, LOW); // Turn off LED1
digitalWrite(LED2, HIGH); // Turn on LED2
digitalWrite(LED3, LOW);
} else if (mappedVal <= 30) { // If the mapped value is less than 50
digitalWrite(LED1, LOW); // Turn off LED1
digitalWrite(LED2, LOW); // Turn on LED2
digitalWrite(LED3, HIGH);
} else { // This else condition is redundant since all cases are covered above
digitalWrite(LED1, LOW); // Turn off LED1
digitalWrite(LED2, LOW); // Turn off LED2
digitalWrite(LED3, LOW);
}
Serial.print("val = ");
Serial.println(mappedVal); // Print the mapped value to the serial monitor
delay(500); // Wait for 500 milliseconds
}