Turn alarm buzzer temporarly off

Hi!
Thinking about a problem for days, but can’t solve it…

I made an AirQuality-System with Air Quality Sensor v1.3 Grove https://www.seeedstudio.com/Grove-Air-quality-sensor-v1.3-p-2439.html, something like an air-traffic-light.

When it is current_quality == 0 then “High Pollution! Force signal active”, the buzzer goes on.

I would like to turn off the buzzer with Tast on Pin 2.
It should do the following:

  • current_quality = = 0
    and
  • Tast = 1
    then tone(Sum, 2000)

when I press the pushbutton

  • Tast = 0
    then noTone(Sum);

the buzzer goes off

and stay, until current_quality has a value greater 0
then it should reset the buzzer until current_quality = = 0 again.

/*
  AirQuality Demo V1.0.
  connect to A0 to start testing. it will needs about 20s to start 
* By: http://www.seeedstudio.com
*/
#include"AirQuality.h"
#include"Arduino.h"
AirQuality airqualitysensor;
int Tast = 2;
#define LED 13
const int Sum = 3;
#define Red 5
#define Yellow 6
#define Green 7
int current_quality =-1;
void setup()
{
    Serial.begin(9600);
    airqualitysensor.init(14);
    pinMode(Tast, INPUT_PULLUP);
    pinMode(LED, OUTPUT);
    pinMode(Sum, OUTPUT);
    pinMode(Red, OUTPUT);
    pinMode(Yellow, OUTPUT);
    pinMode(Green, OUTPUT);
    digitalWrite(LED, LOW);
    digitalWrite (Sum, LOW);
    digitalWrite (Red, LOW);
    digitalWrite (Yellow, LOW);
    digitalWrite (Green, LOW);
    
}
void loop()
{
    current_quality=airqualitysensor.slope();
    if (current_quality >= 0)// if a valid data returned.
    {
        if (current_quality==0)
            {
            Serial.println("High pollution! Force signal active");
            digitalWrite(LED, LOW);
            digitalWrite (Green, LOW);
            digitalWrite (Yellow, LOW);
            tone (Sum, 2000);
            digitalWrite (Red, HIGH);
            }
        else if (current_quality==1)
            {
            Serial.println("High pollution!");
            digitalWrite(LED, LOW);
            digitalWrite(Green, LOW);
            digitalWrite(Yellow, LOW);
            noTone(Sum);
            digitalWrite(Red, HIGH);
            }
        else if (current_quality==2)
            {
            Serial.println("Low pollution!");
            digitalWrite(LED, LOW);
            digitalWrite(Green, LOW);
            digitalWrite(Red, LOW);
            noTone(Sum);
            digitalWrite(Yellow, HIGH);
            }
        else if (current_quality ==3)
            {
            Serial.println("Fresh air");
            digitalWrite(LED, LOW);
            digitalWrite(Red, LOW);
            digitalWrite(Yellow, LOW);
            noTone(Sum);
            digitalWrite(Green, HIGH);
            }
    }
}
ISR(TIMER1_OVF_vect)
{
 if(airqualitysensor.counter==61)//set 2 seconds as a detected duty
 {

 airqualitysensor.last_vol=airqualitysensor.first_vol;
 airqualitysensor.first_vol=analogRead(A0);
 airqualitysensor.counter=0;
 airqualitysensor.timer_index=1;
 PORTB=PORTB^0x20;
 }
 else
 {
 airqualitysensor.counter++;
 }
}

thanks for help!
Peter

Tast is the pin number. It is 2. So it will never equal 0 or 1. What you mean is when digitalRead(Tast) is equal to 0 or 1.

Once you realise that you've almost written it yourself. Give it a try.

Steve

Hi Steve, i have to write somethin after

tone (Sum, 2000);

but I don't get what… When the buzzer gos high, the Tast could be pressed, tone goes off and stays off until there will be the next time current_quality==0

I don't know if I have to do somethin with a flipflop, but this function I don't know well… Peter

Record the time when you stsrted the alarm.

Every time through the loop, look at the current time and turn off the alarm if the time is exceeded.