initial condition trigger

I'm playing with a little gas detector..

How can I stay indefinitely in an if statement or until an external button or reset button is pressed? So, once it detects gas, I'd like to sound the buzzer continuously until a button or reset button is pressed.

Right now it is only buzzing so long as it's detecting gas, MQ135.

int buzzer = 8;
int smokeA0 = A0;
// Your threshold value
int sensorThres = 100;
 
void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(gasA0, INPUT);
  Serial.begin(9600);
 
}
 
void loop() {
  int analogSensor = analogRead(gasA0);
 
  Serial.print("Pin A0: ");
  Serial.println(analogSensor);
   Serial.print("Gas Level:");
  Serial.println(analogSensor);

  // Checks if it has reached the threshold value
  if (analogSensor > sensorThres)
  {
  
    tone(buzzer, 494, 400);

     
  }
  else
  {
   
    noTone(buzzer);
  }
  delay(500);
  
}

Just remove the noTone() and remove the time period from tone().

const int buzzerPin = 8;
const int smokePin = A0;
// Your threshold value
const int sensorThres = 100;


void setup()
{
  Serial.begin(9600);
}


void loop()
{
  int analogSensor = analogRead(smokePin);


  // Checks if it has reached the threshold value
  if (analogSensor > sensorThres)
  {
    Serial.print("Pin A0: ");
    Serial.println(analogSensor);
    tone(buzzerPin, 494);
  }
}

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