// Name of ADC channel
int Pot1;
float Pot1_volt;
// Name of LED Channel
int LED1 = 22; // Active Low
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, welcome to test ADC ");
pinMode(LED1, OUTPUT);
}
void loop() {
// Read potentiometer value and calculate voltage
Pot1 = analogRead(26);
Pot1_volt = (float) Pot1 * 3.3 / 4095; // using 12 bit & Vref 3.3 Volt
Serial.print("ADC value : ");
Serial.print(Pot1); // show ADC Value
Serial.print("\t");
Serial.print("ADC Voltage: ");
Serial.println(Pot1_volt); // Show Voltage of ADC
delay(300);
// call alarm fuction to control the LED based on voltage
alarm(Pot1_volt, LED1, 1000, 1000, 500, 500, 200, 200);
}
void blink(int channel, int on, int off) {
digitalWrite(channel, LOW); // Turn in the LED
delay(on);
digitalWrite(channel, HIGH); // Turn off the LED
delay(off);
}
/*
safe 0 - 1.1
warn 1.1 - 2.2
danger 2.2 - 3.3
*/
void alarm(float ch_adc,
int ch_led,
int safe_on,
int safe_off,
int warn_on,
int warn_off,
int danger_on,
int danger_off) {
// safe condition
if (ch_adc <= 1.1) {
blink(ch_led, safe_on, safe_off);
Serial.println("Condition: Safe");
}
// warning condition
else if (ch_adc > 1.1 & ch_adc < 2.2) {
blink(ch_led, warn_on, warn_off);
Serial.println("Condition: Warning");
}
// danger condition
else if (ch_adc >= 2,2) {
blink(ch_led, danger_on, danger_off);
Serial.println("Condition: Danger");
}
}