How do I lock the values (with a button) 10 seconds before in a loop?

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)
1 Like

Hello, may ask what is the !digitalRead code?

Also, I would like to take this opportunity in uploading the whole code to fully understand it:



#include <Wire.h>                  
#include <LiquidCrystal_I2C.h>      //Arduino IC2 Library
LiquidCrystal_I2C lcd(0x27, 16, 2); 

int sensorPin = A3;
int sensorVal = 0;
float sensorCalc51 = 0; //51%
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int buzzPin = 13;     //Setting pins
int i; //loop



void setup()
{
  pinMode(sensorPin,INPUT);
  pinMode(redPin,OUTPUT);
  pinMode(bluePin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(buzzPin,OUTPUT);

  lcd.begin();
  lcd.backlight();        //LCD setup
  Serial.begin(9600);
}

void loop()
{
       
        sensorVal=analogRead(sensorPin);    
        sensorCalc51=(341./184.)*sensorVal;
        Serial.println("   ");
        Serial.print("0% :    ");
        Serial.print(sensorVal);        //prints the 0%
        Serial.print("  51% :   ");
        Serial.print(sensorCalc51);      //prints 51%
        delay(100);
               

 if (sensorVal <= sensorCalc51) {   //If value is goes between the given threshold, the LCD will print a text,GREEN pin, and buzzer will TURN OFF               
            lcd.clear();
              lcd.clear();
              lcd.print("YOU ARE TOO ");
              lcd.print(sensorVal);
              lcd.setCursor (3,1); 
              lcd.print("SOBER");
          analogWrite(redPin, 000);
          analogWrite(bluePin,000);
          analogWrite(greenPin,255);
          digitalWrite(buzzPin, LOW);               
               
          }
        
            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);
               }
            }
  
} //end of loop

same as ...

  if (digitalRead(clearValButton) == LOW) {
    sensorVal = 0;  // clear sensorVal when clearValButton is pressed (pin goes LOW)
  }

1 Like

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

Yes. It won't work to test if a value is 51% higher than itself. Maybe you mean 51% higher than the lowest it has been?

void loop()
{
  static int lowestReading = 1023;
  sensorVal = analogRead(sensorPin);
  if (sensorVal < lowestReading)
    lowestReading = sensorVal;

  if (sensorVal > lowestVal * (341./184.))
  {
    // Alarm
    analogWrite(redPin,255);
  }
1 Like

Maybe OP's looking for the other logic:

if (digitalRead(reset51Button)==LOW) sensorCalc51 = (322.0 / 150.0) * sensorVal;  // remember sensorCalc51 when reset51Button is pressed (pin goes LOW)

This does the calculation only when the button is pressed.

1 Like

I am quite confused, where does the lowestVal come from?

Sorry, typo. That should be "lowestReading".

perhaps something like this (START) ...

This worked!

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;.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.