That's why I said that the project has some shortcomings. Regarding that commented question [it was not necessary to pay attention
] I was thinking of introducing a switch with 4 cases, something like this [ I haven't written a switch-case from a long time
] :
void Detect(){
flame_val = analogRead(flame);
gas_val = analogRead(gas);
int x;
switch(x){ // something like that, I have to experiment a lot with this instruction
case 1:
x = (gas_val <= gasThreshold) && (flame_val <= flameThershold)
digitalWrite(Buzzer, LOW);
digitalWrite(Relay, LOW);
break;
case 2:
x = (gas_val < gasThreshold) && (flame_val > flameThershold)
digitalWrite(Buzzer, HIGH);
delay(interval);
digitalWrite(Relay, HIGH);
break;
case 3:
x = (gas_val > gasThreshold) && (flame_val < flameThershold);
digitalWrite(Buzzer, HIGH);
delay(interval);
digitalWrite(Relay, HIGH);
break;
case 4:
x = (gas_val > gasThreshold) && (flame_val > flameThershold);
digitalWrite(Buzzer, HIGH);
delay(interval);
digitalWrite(Relay, HIGH);
break;
} // close switch case
} // Close Detect
Mainly it is enough for me to enter the lines of code for that period of data acquisition at the sensors in the function you saw [void Detect()], because I do not know if I have enough time to introduce switch-cases. I would also introduce a Possible Fire message on the LCD when the sensors exceed the thresholds, then when they go back down, the initial Alarm Activated message will appear.
And this is the complete code in which I want to introduce what you told me about LDR (if I have time I will do more for this project) :
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
int Buzzer = 7;
int Relay = 8;
int flame = A0;
int flameThershold = 900;
int flame_val = 0;
int gas = A1;
int gasThreshold = 800;
int gas_val = 0;
bool flag = false;
const long interval = 5000; // ms
void setup() {
Serial.begin(9600);
pinMode(flame,INPUT);
pinMode(gas, INPUT);
pinMode(Buzzer, OUTPUT);
pinMode(Relay, OUTPUT);
pinMode(buttonPin, INPUT);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Fire Alarm");
} // void setup
void loop() {
buttonState = digitalRead(buttonPin);
//filter out any noise by setting a time buffer
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
if (flag){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm ");
lcd.setCursor(0,1);
lcd.print("deactivated");
flag = false;
delay(500); // delay between transitions
}
else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm activated");
flag = true;
delay(500);
}
}
delay(50);
} //close if(time buffer) // save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if(flag){ //
Detect();
}
} // void loop
void Detect(){
flame_val = analogRead(flame);
gas_val = analogRead(gas);
if ( (gas_val >= gasThreshold)||(flame_val >= flameThershold) )
{
digitalWrite(Buzzer, HIGH);
delay(interval);
digitalWrite(Relay, HIGH);
}
else if ((gas_val < gasThreshold) && (flame_val < flameThershold)) {
digitalWrite(Buzzer, LOW);
digitalWrite(Relay, LOW);
}
} // Detect
Operation (currently): power the circuit; the message Fire Alarm appears; press the button once, the message Alarm Activated appears, the sensors record data; press the button second time, the message Alarm Deactivated appears, the sensors are off.
Robin2:
It is no good you asking a question in your code in Reply #12
// what if both values are greater than thresholds ??!
You need to tell us how you want it to behave in each of the 4 possible situations.
...R