// Pin Definitions
const int MQ137Pin = 34; // Analog pin for MQ137 sensor
const int MotorDrive = 25; // for Motor Drive
const int PumpDrive = 33; // for Pump Drive
const int OnOffButton = 26; // Digital pin for the button
const int OverrideButton = 27; // Digital pin for the override button
const int OverrideIndicator = 14; // Override Indicator Led
// Threshold for MQ137
const int threshold = 400; // Adjust this value based on your calibration
// Debounce variables for OnOffButton
bool buttonState = LOW;
bool lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 10;
const unsigned long debounceDelayOverride = 100;
// Debounce variables for OverrideButton
bool overrideState = LOW;
bool lastOverrideState = LOW;
unsigned long lastOverrideDebounceTime = 0;
// Motor and pump state
bool motorWaterPumpState = LOW;
bool overrideEnabled = false;
void setup() {
Serial.begin(115200);
pinMode(MQ137Pin, INPUT);
pinMode(MotorDrive, OUTPUT);
pinMode(PumpDrive, OUTPUT);
pinMode(OverrideIndicator, OUTPUT);
pinMode(OnOffButton, INPUT_PULLUP);
pinMode(OverrideButton, INPUT_PULLUP);
digitalWrite(MotorDrive, LOW);
digitalWrite(PumpDrive, LOW);
digitalWrite(OverrideIndicator, LOW);
}
void loop() {
// Handle OverrideButton debounce
handleOverrideButton();
if (!overrideEnabled) {
int sensorValue = analogRead(MQ137Pin);
Serial.print("MQ137 Value: ");
Serial.println(sensorValue);
// Check if the sensor value exceeds the threshold
if (sensorValue >= threshold) {
motorWaterPumpState = HIGH;
} else {
motorWaterPumpState = LOW;
}
delay(100); // Small delay to prevent rapid toggling
} else {
// Handle OnOffButton debounce
handleOnOffButton();
}
// Update the relay state
digitalWrite(MotorDrive, motorWaterPumpState);
digitalWrite(PumpDrive, motorWaterPumpState);
digitalWrite(OverrideIndicator, overrideEnabled);
}
void handleOnOffButton() {
bool readOnOff = digitalRead(OnOffButton);
// Debouncing the button
if (readOnOff != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (readOnOff != buttonState) {
buttonState = readOnOff;
if (buttonState == LOW) {
// Toggle motor and water pump state
motorWaterPumpState = !motorWaterPumpState;
}
}
}
// Save the readOnOff state
lastButtonState = readOnOff;
}
void handleOverrideButton() {
bool readOverride = digitalRead(OverrideButton);
// Debouncing the override button
if (readOverride != lastOverrideState) {
lastOverrideDebounceTime = millis();
}
if ((millis() - lastOverrideDebounceTime) > debounceDelayOverride) {
if (readOverride != overrideState) {
overrideState = readOverride;
if (overrideState == LOW) {
// Toggle override state
Serial.println("Overide eneble");
overrideEnabled = !overrideEnabled;
}
}
}
// Save the readOverride state
lastOverrideState = readOverride;
}