const int potPinA0 = A0; // Potentiometer analog pin
const int potPinA1 = A1; // Potentiometer analog pin
const int relayPin8 = 8; // Relay control pin
const int relayPin7 = 7; // Relay control pin
//const int midpoint = 512; // Midpoint of 10-bit ADC
//const int tolerance = 20; // Acceptable range around midpoint
// Optional: smoothing filter
int smoothRead(int pin, int samples = 10) {
long total = 0;
for (int i = 0; i < samples; i++) {
total += analogRead(pin);
delay(8); // Small delay for stable readings
//Serial.print(i);
}
return total / samples;
}
void setup() {
pinMode(relayPin8, OUTPUT);
digitalWrite(relayPin8, LOW); // Relay OFF initially
pinMode(relayPin7, OUTPUT);
digitalWrite(relayPin7, LOW); // Relay OFF initially
Serial.begin(9600);
}
void loop() {
int potA0Value = smoothRead(potPinA0);
int potA1Value = smoothRead(potPinA1);
// Debug output
Serial.print("PotentiometerA0: "); //pot1
Serial.print(potA0Value);
Serial.print(" PotentiometerA1: "); // pot 2
Serial.println(potA1Value);
// 1st pot to relay 1 pin8 Check if within midpoint range pin 8
if (potA0Value >= (150) && potA0Value <= (850)) {// above and below centre
digitalWrite(relayPin8, HIGH); // Turn relay ON
} else {
digitalWrite(relayPin8, LOW); // Turn relay OFF
}
//2nd pot to relay2 pin7
if (potA1Value >= (450) && potA1Value <= (550)) { // above and below centre
digitalWrite(relayPin7, HIGH); // Turn relay ON
} else {
digitalWrite(relayPin7, LOW); // Turn relay OFF
}
delay(100); // Loop delay to reduce flicker
}