I need help fixing my code for Gas Leakage System with IR Remote Control

Hi, I'm working on a project, "Gas Leakage System with IR remote control". I am not a pro at coding C and I just know a little more than the basics. So, what I wanted the code to do is that, when I press a specific button on the IR remote, a function should be executed, for example turns red led on or off, switch the button state to on if it is already off and vice versa. While all of this happens, I wanted the code to check if there is any gas leakage (MQ-2 Sensor is used for that), after using the Switch case for the IR remote control, I used the While loop (inside the void loop() function) to check if any gas is detected by the sensor and compare it to two variables, lowAlert and highAlert. My purpose is to run this while loop every time when any button state is on. There is no error in the code yet the IR remote control works but everything in the while loop doesn't. I've added a few comments to the code also, so that whoever is reading it can have an idea about what I intended the code to do.

#include <IRremote.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int mq2 = A3;
int lowAlert = 60;
int highAlert = 75;
int RECV_PIN = 7;
int redLed = 13;
int grnLed = 12;
int bluLed = 11;
int grn2Led = 10;
int red2Led = 9;
int btnState[] = {0,0,0,0,0,0}; // This is to check the state of a button if its On(1) or Off(0)

#define Btn_1 0xFF30CF // code received from button 1 on the IR remote control
#define Btn_2 0xFF18E7 // code received from button 2 on the IR remote control
#define Btn_3 0xFF7A85 // code received from button 3 on the IR remote control
#define Btn_100 0xFF9867 // code received from button 100 on the IR remote control
#define Btn_0 0xFF6897 // code received from button 0 on the IR remote control

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode(mq2, INPUT);
  pinMode(redLed, OUTPUT);
  pinMode(grnLed, OUTPUT);
  pinMode(bluLed, OUTPUT);
  pinMode(grn2Led, OUTPUT);
  pinMode(red2Led, OUTPUT);
  lcd.begin(16,2);
  lcd.clear();
  lcd.print("Gas Leakage System!");
  delay(2000);
  lcd.clear();
}

void lowAlertOn() {
        int mq2Value = analogRead(mq2);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("LOW ALERT BLUE");
        lcd.setCursor(0,2);
        lcd.print("Gas Value: ");
        lcd.print(mq2Value);
        digitalWrite(red2Led, LOW);
        digitalWrite(grn2Led, HIGH);
        delay(250);
        digitalWrite(grn2Led, LOW);
        delay(250);
        lcd.clear();
}

void highAlertOn() {
        int mq2Value = analogRead(mq2);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("HIGH ALERT RED");
        lcd.setCursor(0,2);
        lcd.print("Value: ");
        lcd.print(mq2Value);
        digitalWrite(grn2Led, LOW);
        digitalWrite(red2Led, HIGH);
        delay(250);
        digitalWrite(red2Led, LOW);
        delay(250);
        lcd.clear();
}

void loop() {
  int mq2Value = analogRead(mq2);
  lcd.setCursor(0,0);
  lcd.print("MQ-2 Sensor");
  lcd.setCursor(0,2);
  lcd.print("Gas Value: ");
  lcd.print(mq2Value);
  Serial.print("Gas Value: ");
  Serial.println(mq2Value);
  if (irrecv.decode(&results)) {
    unsigned int irValue = results.value;
    switch(irValue) {
      case Btn_1: Btn1Pressed(); // when Btn_1 is pressed, it executes whatever Btn1Pressed() function says
                   delay(1000);
      break;

      case Btn_2: Btn2Pressed(); // when Btn_2 is pressed, it executes whatever Btn2Pressed() function says
                   delay(1000);
      break;

      case Btn_3: Btn3Pressed(); // when Btn_3 is pressed, it executes whatever Btn3Pressed() function says
                  delay(1000);
      break;

      case Btn_100: Btn100Pressed(); // when Btn_100 is pressed, it executes whatever Btn100Pressed() function says
                    delay(1000);
      break;

      case Btn_0: Btn0Pressed(); // when Btn_0 is pressed, it executes whatever Btn0Pressed() function says
                  delay(1000);
      break; 
    }
    irrecv.resume();
  }
  // Below, while loop is used. While Btn_1 or Btn_2 or Btn_3 or Btn_100 is On,
    while (btnState[1, 2, 3, 4] == 1) {
      if (mq2Value >= lowAlert && mq2Value < highAlert) // if mq2value is greater than or equal to lowAlert and less than highAlert
          {
            lowAlertOn(); // Executes whatever the lowAlertOn() funtion says
          }
        else if (mq2Value >= highAlert) // if mq2Value is greater than or equal to highAlert
          {
            highAlertOn(); // Executes whatever the highAlertOn() funtion says
          }
        else
          {
            digitalWrite(red2Led, LOW); // Or else the red2Led and grn2Led is turned off 
            digitalWrite(grn2Led, LOW);
          }
      delay(500);                  
    }
}
void Btn1Pressed() {
  int mq2Value = analogRead(mq2);
  if(btnState[1] == 1) // Checks if btnState of Btn_1 is On
   { // if it is on then
    digitalWrite(redLed, LOW); // turns redLed off when Btn_1 is pressed
    btnState[1] = 0; // and btnState is set as off
   }
  else if (btnState[1] == 0) // Checks if btnState of Btn_1 is On
   { // else if btnState of Btn_1 is already off
      digitalWrite(grnLed, HIGH); // turns grnLed on when the Btn_1 is pressed
      btnState[1] = 1; // and btnState is set as on
      digitalWrite(redLed, LOW);
      digitalWrite(bluLed, LOW);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Gas Value: ");
      lcd.print(mq2Value);
      lcd.setCursor(0,2);
      lcd.print("Gas Flow: 25%");
      Serial.println("Gas Flow: 25%");
   }
}

void Btn2Pressed() {
  int mq2Value = analogRead(mq2);
  if(btnState[2] == 1) 
   {
    digitalWrite(bluLed, LOW);
    btnState[2] = 0;
   }
  else if (btnState[2] == 0)
   {
      digitalWrite(bluLed, HIGH);
      btnState[2] = 1;
      digitalWrite(redLed, LOW);
      digitalWrite(grnLed, LOW);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Gas Value: ");
      lcd.print(mq2Value);
      lcd.setCursor(0,2);
      lcd.print("Gas Flow: 50%");
      Serial.println("Gas Flow: 50%");
   }
}

void Btn3Pressed() {
  int mq2Value = analogRead(mq2);
  if(btnState[3] == 1)
   {
    digitalWrite(redLed, LOW);
    btnState[3] = 0;
   }
  else if (btnState[3] == 0)
   {
      digitalWrite(redLed, HIGH);
      btnState[3] = 1;
      digitalWrite(grnLed, LOW);
      digitalWrite(bluLed, LOW);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Gas Value: ");
      lcd.print(mq2Value);
      lcd.setCursor(0,2);
      lcd.print("Gas Flow: 75%");
      Serial.println("Gas Flow: 75%");
   }
}

void Btn100Pressed() {
  int mq2Value = analogRead(mq2);
  if(btnState[4] == 1)
   {
    digitalWrite(redLed, LOW);
    digitalWrite(grnLed, LOW);
    digitalWrite(bluLed, LOW);
    btnState[4] = 0;
   }
  else if (btnState[4] == 0)
   {
      digitalWrite(redLed, HIGH);
      digitalWrite(grnLed, LOW);
      digitalWrite(bluLed, HIGH);
      btnState[4] = 1;
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Gas Value: ");
      lcd.print(mq2Value);
      lcd.setCursor(0,2);
      lcd.print("Gas Flow: 100%");
      Serial.println("Gas Flow: 100%");
   }
}

void Btn0Pressed() {
  if(btnState[5] == 1) 
   {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Press Btn 1,2,3");
    lcd.setCursor(0,2);
    lcd.print("Or 100 to Start");
    btnState[5] = 0;
   }
  else if (btnState[5] == 0)
   {
    digitalWrite(redLed, HIGH);
    digitalWrite(grnLed, HIGH);
    digitalWrite(bluLed, HIGH);
    btnState[1, 2, 3, 4] = 0;
    btnState[5] = 1;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("GAS FLOW BLOCKED");
    lcd.setCursor(0,2);
    lcd.print("Gas Flow: 0%");
    delay(1000);
   }
}

2_finalTest.ino (6.65 KB)

That code size does not exceed the 9K post limit. Please include your code inside code tags so it looks like this

#include <IRremote.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int mq2 = A3;
int lowAlert = 60;
int highAlert = 75;
int RECV_PIN = 7;
int redLed = 13;
int grnLed = 12;
int bluLed = 11;
int grn2Led = 10;
int red2Led = 9;
int btnState[] = {0, 0, 0, 0, 0, 0}; // This is to check the state of a button if its On(1) or Off(0)

#define Btn_1 0xFF30CF // code received from button 1 on the IR remote control
#define Btn_2 0xFF18E7 // code received from button 2 on the IR remote control
#define Btn_3 0xFF7A85 // code received from button 3 on the IR remote control
#define Btn_100 0xFF9867 // code received from button 100 on the IR remote control
#define Btn_0 0xFF6897 // code received from button 0 on the IR remote control

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode(mq2, INPUT);
  pinMode(redLed, OUTPUT);
  pinMode(grnLed, OUTPUT);
  pinMode(bluLed, OUTPUT);
  pinMode(grn2Led, OUTPUT);
  pinMode(red2Led, OUTPUT);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("Gas Leakage System!");
  delay(2000);
  lcd.clear();
}

void lowAlertOn() {
  int mq2Value = analogRead(mq2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("LOW ALERT BLUE");
  lcd.setCursor(0, 2);
  lcd.print("Gas Value: ");
  lcd.print(mq2Value);
  digitalWrite(red2Led, LOW);
  digitalWrite(grn2Led, HIGH);
  delay(250);
  digitalWrite(grn2Led, LOW);
  delay(250);
  lcd.clear();
}

void highAlertOn() {
  int mq2Value = analogRead(mq2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("HIGH ALERT RED");
  lcd.setCursor(0, 2);
  lcd.print("Value: ");
  lcd.print(mq2Value);
  digitalWrite(grn2Led, LOW);
  digitalWrite(red2Led, HIGH);
  delay(250);
  digitalWrite(red2Led, LOW);
  delay(250);
  lcd.clear();
}

void loop() {
  int mq2Value = analogRead(mq2);
  lcd.setCursor(0, 0);
  lcd.print("MQ-2 Sensor");
  lcd.setCursor(0, 2);
  lcd.print("Gas Value: ");
  lcd.print(mq2Value);
  Serial.print("Gas Value: ");
  Serial.println(mq2Value);
  if (irrecv.decode(&results)) {
    unsigned int irValue = results.value;
    switch (irValue) {
      case Btn_1: Btn1Pressed(); // when Btn_1 is pressed, it executes whatever Btn1Pressed() function says
        delay(1000);
        break;

      case Btn_2: Btn2Pressed(); // when Btn_2 is pressed, it executes whatever Btn2Pressed() function says
        delay(1000);
        break;

      case Btn_3: Btn3Pressed(); // when Btn_3 is pressed, it executes whatever Btn3Pressed() function says
        delay(1000);
        break;

      case Btn_100: Btn100Pressed(); // when Btn_100 is pressed, it executes whatever Btn100Pressed() function says
        delay(1000);
        break;

      case Btn_0: Btn0Pressed(); // when Btn_0 is pressed, it executes whatever Btn0Pressed() function says
        delay(1000);
        break;
    }
    irrecv.resume();
  }
  // Below, while loop is used. While Btn_1 or Btn_2 or Btn_3 or Btn_100 is On,
  while (btnState[1, 2, 3, 4] == 1) {
    if (mq2Value >= lowAlert && mq2Value < highAlert) // if mq2value is greater than or equal to lowAlert and less than highAlert
    {
      lowAlertOn(); // Executes whatever the lowAlertOn() funtion says
    }
    else if (mq2Value >= highAlert) // if mq2Value is greater than or equal to highAlert
    {
      highAlertOn(); // Executes whatever the highAlertOn() funtion says
    }
    else
    {
      digitalWrite(red2Led, LOW); // Or else the red2Led and grn2Led is turned off
      digitalWrite(grn2Led, LOW);
    }
    delay(500);
  }
}
void Btn1Pressed() {
  int mq2Value = analogRead(mq2);
  if (btnState[1] == 1) // Checks if btnState of Btn_1 is On
  { // if it is on then
    digitalWrite(redLed, LOW); // turns redLed off when Btn_1 is pressed
    btnState[1] = 0; // and btnState is set as off
  }
  else if (btnState[1] == 0) // Checks if btnState of Btn_1 is On
  { // else if btnState of Btn_1 is already off
    digitalWrite(grnLed, HIGH); // turns grnLed on when the Btn_1 is pressed
    btnState[1] = 1; // and btnState is set as on
    digitalWrite(redLed, LOW);
    digitalWrite(bluLed, LOW);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Gas Value: ");
    lcd.print(mq2Value);
    lcd.setCursor(0, 2);
    lcd.print("Gas Flow: 25%");
    Serial.println("Gas Flow: 25%");
  }
}

void Btn2Pressed() {
  int mq2Value = analogRead(mq2);
  if (btnState[2] == 1)
  {
    digitalWrite(bluLed, LOW);
    btnState[2] = 0;
  }
  else if (btnState[2] == 0)
  {
    digitalWrite(bluLed, HIGH);
    btnState[2] = 1;
    digitalWrite(redLed, LOW);
    digitalWrite(grnLed, LOW);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Gas Value: ");
    lcd.print(mq2Value);
    lcd.setCursor(0, 2);
    lcd.print("Gas Flow: 50%");
    Serial.println("Gas Flow: 50%");
  }
}

void Btn3Pressed() {
  int mq2Value = analogRead(mq2);
  if (btnState[3] == 1)
  {
    digitalWrite(redLed, LOW);
    btnState[3] = 0;
  }
  else if (btnState[3] == 0)
  {
    digitalWrite(redLed, HIGH);
    btnState[3] = 1;
    digitalWrite(grnLed, LOW);
    digitalWrite(bluLed, LOW);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Gas Value: ");
    lcd.print(mq2Value);
    lcd.setCursor(0, 2);
    lcd.print("Gas Flow: 75%");
    Serial.println("Gas Flow: 75%");
  }
}

void Btn100Pressed() {
  int mq2Value = analogRead(mq2);
  if (btnState[4] == 1)
  {
    digitalWrite(redLed, LOW);
    digitalWrite(grnLed, LOW);
    digitalWrite(bluLed, LOW);
    btnState[4] = 0;
  }
  else if (btnState[4] == 0)
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(grnLed, LOW);
    digitalWrite(bluLed, HIGH);
    btnState[4] = 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Gas Value: ");
    lcd.print(mq2Value);
    lcd.setCursor(0, 2);
    lcd.print("Gas Flow: 100%");
    Serial.println("Gas Flow: 100%");
  }
}

void Btn0Pressed() {
  if (btnState[5] == 1)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Press Btn 1,2,3");
    lcd.setCursor(0, 2);
    lcd.print("Or 100 to Start");
    btnState[5] = 0;
  }
  else if (btnState[5] == 0)
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(grnLed, HIGH);
    digitalWrite(bluLed, HIGH);
    btnState[1, 2, 3, 4] = 0;
    btnState[5] = 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("GAS FLOW BLOCKED");
    lcd.setCursor(0, 2);
    lcd.print("Gas Flow: 0%");
    delay(1000);
  }
}

This is not correct

  while (btnState[1, 2, 3, 4] == 1) {

you have to do it like this

  while (btnState[1] or btnState[2] or btnState[3] or btnState[4]) {

but that while() loop will run forever since nothing inside of it will ever change those variables. I believe you want to use an if() so you code can complete and go back to loop()

blh64:
This is not correct

  while (btnState[1, 2, 3, 4] == 1) {

you have to do it like this

  while (btnState[1] or btnState[2] or btnState[3] or btnState[4]) {

but that while() loop will run forever since nothing inside of it will ever change those variables. I believe you want to use an if() so you code can complete and go back to loop()

So you're suggesting that I should use if() instead of while() for it to loop back, right?
And please tell me if there are any more mistakes in the code.
Thanks a lot for helping :slight_smile: