// Library: MQ2_LPG
// By: Devan Cakra M.W
#include <MQ2_LPG.h> // library declaration
#define MQ2_ANALOG_PIN A0
#define BUZZER_PIN 9
#define RED_PIN 10
#define GREEN_PIN 11
#define BLUE_PIN 12
#define MQ2PIN 5 // mq2 pin declaration
MQ2Sensor mq2(MQ2PIN); // create a new object with the name mq2 to hold the MQ2Sensor class
int sensorValue = 0;
// Defina aqui os limites de segurança
int limitYellow = 300; // Começa a ficar alerta
int limitRed = 600; // Perigo real
void setup() {
Serial.begin(9600);
mq2.begin(); // initiate mq2 sensor
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.println("Sistema de Monitoramento Iniciado...");
}
void loop() {
sensorValue = analogRead(MQ2_ANALOG_PIN);
Serial.print("Nível: ");
Serial.println(sensorValue);
if (sensorValue < limitYellow) {
// TUDO SEGURO - Verde
setColor(0, 255, 0);
noTone(BUZZER_PIN);
}
else if (sensorValue >= limitYellow && sensorValue < limitRed) {
// ATENÇÃO - Amarelo
setColor(255, 150, 0);
tone(BUZZER_PIN, 500); // Som constante mas grave
delay(100);
noTone(BUZZER_PIN);
}
else {
// PERIGO - Vermelho
setColor(255, 0, 0);
// Alarme desesperador: agudo e rápido
tone(BUZZER_PIN, 2000);
delay(100);
noTone(BUZZER_PIN);
delay(50);
}
mq2.readGas(); // reading mq2 sensor data
mq2.viewGasData(); // print to serial monitor: gas value & status
//delay(3000); // delay for 3 seconds
delay(200);
}
// Função auxiliar para facilitar a troca de cores
void setColor(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}