I've been creating a project that involves the Mq-3 sensor. When the sensor gets an increase of value of 51%, then the RedLED will blink. As for this, I have created used a formula based on ratios and the data I collected from respondents.
sensorVal=analogRead(sensorPin); //read SensorPin
sensorCalc51=(322./150.)*sensorVal; //This is the 51% value that the arduino makes that is dependent on the sensorVal
if (sensorVal >= sensorCalc51) { //the condition involves both the sensorVal and sensorCalc51
for (int i=0; i<=20; i=i+1) {
analogWrite(redPin,255);
delay (500);
analogWrite(redPin,000);
delay (500);
}
As you can see in the code, the condition will NEVER be true as the formula always makes the sensorCalc51 higher than the sensorVal.
I need to make the sensorCalc51 lock it's latest value through a button so that it
won't go higher when the person breathes on the sensor, and it actually makes the condition TRUE.
Although I have an idea to use a pushbutton to somewhat stop the never-ending calculation, is there a code to atleast lock the most recent value?
sensorVal = analogRead(sensorPin); //read SensorPin
if (sensorVal >= sensorCalc51) { //the condition involves both the sensorVal and sensorCalc51, sensorCalc51 updates when condition is true
sensorCalc51 = (322.0 / 150.0) * sensorVal; //This is the 51% value that the arduino makes that is dependent on the sensorVal
for (int i = 0; i <= 20; i = i + 1) {
analogWrite(redPin, 255);
delay (500);
analogWrite(redPin, 0);
delay (500);
}
if (!digitalRead(clearValButton)) sensorVal = 0; // clear sensorVal when clearValButton is pressed (pin goes LOW)
Ohh, that is a good idea too but I am trying to lock the "recent" value given by my sensorCalc51 (fully dependent on sensorVal) in order to make the condition true
I am actually not quite sure if that's possible but I really do hope it is
But now my problem is that it shows a value of "0" in the sensorCalc51 and loops forever in the
if (sensorVal >= sensorCalc51) { //If value is goes above the threshold, the LCD will print a text,RED pin, and buzzer will TURN ON
lcd.clear();
for (int i=0; i<=20; i=i+1) {
lcd.print("DRUNK DRIVER");
lcd.setCursor (8,1);
lcd.print("ON-BOARD");
analogWrite(redPin,255);
analogWrite(bluePin,000);
analogWrite(greenPin,000);
digitalWrite(buzzPin, HIGH);
delay (500);
digitalWrite(buzzPin, LOW);
delay (500);
}
}
this is because after uploading the code, it immediately goes on to the IF statements. Is there a code that waits for a pushbutton input before it goes into the IF statements?
There's if(<expression>) But you have to figure out what to put as <expression>. If you created a variable like bool anyButtonsEverPressed = false; then you could wrap code with if (anyButtonsEverPressed){.....} and it wouldn't execute until you set anyButtonsEverPressed = true;.