const float ADC_VREF_V = 5.0; // ADC reference voltage in volts (5 V)
const int ADC_RESOLUTION = 4095; // ADC resolution (2^12 - 1)
const int analogPin = 2; // Pin for reading the analog input (e.g., potentiometer)
int previousValue = 0; // Variable to store the previous analog value
const byte pinRed = 18; // Pin connected to the red LED
const byte pinGreen = 19; // Pin connected to the green LED
const byte pinYellow = 5; // Pin connected to the green LED
void setup() {
pinMode(pinRed, OUTPUT); // Set the red LED pin as an output
pinMode(pinGreen, OUTPUT); // Set the green LED pin as an output
pinMode(pinYellow, OUTPUT); // Set the green LED pin as an output
digitalWrite(pinRed, LOW); // Initialize the red LED to be OFF
digitalWrite(pinGreen, LOW); // Initialize the green LED to be OFF
digitalWrite(pinYellow, LOW); // Initialize the green LED to be OFF
Serial.begin(115200); // Start serial communication at 115200 baud rate
delay(1000); // Wait for 1 second to allow system initialization
pinMode(analogPin, INPUT); // Set the analog pin as an input
}
void loop() {
int analogValue = analogRead(analogPin); // Read the analog value from the analog pin
Serial.print("Analog Value: "); // Print the analog value on the serial monitor
Serial.println(analogValue);
float voltage = analogValue * (ADC_VREF_V / ADC_RESOLUTION); // Calculate the voltage (scale of 5V)
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.println(" V");
if (voltage >= 3.0) {
digitalWrite(pinRed, HIGH);
digitalWrite(pinGreen, LOW);
digitalWrite(pinYellow, LOW);
} else if (voltage < 3.0 and voltage >2) {
digitalWrite(pinRed, LOW);
digitalWrite(pinYellow, LOW);
digitalWrite(pinGreen, HIGH);
} else if (voltage < 2.0) {
digitalWrite(pinRed, LOW);
digitalWrite(pinYellow, HIGH);
digitalWrite(pinGreen, LOW);
}
if (analogValue != previousValue) {
previousValue = analogValue;
}
delay(250);
}