// Libraries
#include <DHT.h>;

//Constants
#define DHT_PIN 4     // The pin to which the DHT22 is connected
#define DHT_TYPE DHT22   // Type of the DHT connected
#define TEMP_THREASHOLD 40 // Thereshold temperature
#define BUZZER_PIN 9 // Pin number of the buzzer connected
#define BUZZER_FREQUENCY 300  // Frequency of the buzzer
#define STOP_BUTTON_PIN 7 // Pin number to which the button connect

DHT dht(DHT_PIN, DHT_TYPE);

float temp; // Variable to assign temperature read
boolean isAlarming = false; // Flag to indicate whether alarming or not
int stopButtonPressed = 0; // to store the button pressed state

void setup() {
  Serial.begin(9600);
  pinMode(STOP_BUTTON_PIN, INPUT);
  dht.begin();
}

void loop() {
  //Read the temperature
  temp = dht.readTemperature();

  if (temp > TEMP_THREASHOLD && !isAlarming) { // Check whether the temperature exeeds the threashold and then alarm is on
    tone(BUZZER_PIN, BUZZER_FREQUENCY); // Set the alarm/buzzer on
    isAlarming = true; // Set flag as alrming
  }
  // Read the button state
  stopButtonPressed = digitalRead(STOP_BUTTON_PIN);
  if (stopButtonPressed == 1 && isAlarming) {
    // Reset alarm
    noTone(BUZZER_PIN); // Stop the buzzer/alarm
    isAlarming = false; // Set the flag as not-alarming
  }
}