temperature control

hi , could anyone help me, please?
i hv some school project .
a temperature control with fan, heater, and buzzer.
the buzzer will beep once when the fan start up.
but i dont know the program to make the buzzer beep once.
help me pleasee
here's the code

tempsensor.ino (1.66 KB)

Don't use delay().

Look at the "Blink without Delay" sketch in the Arduino IDE under File > examples > digital.

In principle, you set a timer with the current value of millis() and when the timer expires after X milliseconds, you stop the buzzer.

See comments with *****.
Create a flag to control whether the tone can be played or not. I called it toneEnable and initialized to true (tone can be played). If the temperature is over 25, the state of the flag is checked and if toneEnable is true the tone plays one time and the toneEnable is set false. If the temperature is still over 25 the next iteration of loop() the flag is false so no tone will play. If the temperature drops below 25 the toneEnable is set back to true so the tone can play the next time that the temperature goes over 25.

#include <LiquidCrystal.h> //memanggil library LCD  
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  // pin LCD
int tempPin = A0;   // output pin sensor suhu
int fan = 9;       // output pin kipas
int heater = 8;        // output pin pemanas
int temp; //inisialisasi
int buzzer = 6; //alarm pin

boolean toneEnable = true;  // *****add a flag variable

int readTemp() {  // membaca suhu  
  temp = analogRead(tempPin); //membaca suhu
  return temp * 0.48828125; //mengubah suhu ke celcius
}

void setup() {
  pinMode(fan, OUTPUT); //pin kipas sebagai output
  pinMode(heater, OUTPUT);//pin pemanas sebagai output
  pinMode(tempPin, INPUT);//pin sensor suhu sebagai output
  pinMode(buzzer, OUTPUT);
  lcd.begin(16, 2); //sintaks lcd yang digunakan 16x2
}

void loop() {
  temp = readTemp();     // membaca suhu
  lcd.setCursor(0, 0); // penulisan karakter lcd kolom 0 baris 0
  lcd.print("Temperature="); //menampilkan karakter di lcd
  lcd.setCursor(0, 1); // penulisan karakter lcd kolom 0 baris 1
  lcd.print(temp); //menampilkan hasil suhu
  lcd.println("\337C");//menampilkan pada baris baru
  
  if (temp <= 10) { // jika suhu  <= 10 derajatC
    digitalWrite(fan, LOW); //kipas mati
    digitalWrite(heater, HIGH); //pemanas menyala

    toneEnable = true;  // *****enable the tone
  }
  else if ((temp >= 11) && (temp <= 25)) { // jika suhu diatas >=11 derajatC dan <=25derajatC
    digitalWrite(fan, LOW);  //kipas mati
    digitalWrite(heater, LOW) ; // pemanas mati

    toneEnable = true;  // *****enable the tone
  }
  else {                   // jika diatas 25 derajat C
    digitalWrite(fan, HIGH);  //kipas menyala
    digitalWrite(heater, LOW) ;//pemanas mati

    if(toneEnable == true)  // *****only sound tone 1 time
    {
    tone(buzzer, 250);
    // Waits some time to turn off
    delay(200);
    //Turns the buzzer off
    noTone(buzzer);

    toneEnable = false;  // *****disable further tones until temperature drops below 25
    }
  }

  delay(10);// mengirimkan data tiap 10milliseconds
}

Please post code using code tags so more people can see it.

(deleted)

groundFungus:
See comments with *****.
Create a flag to control whether the tone can be played or not. I called it toneEnable and initialized to true (tone can be played). If the temperature is over 25, the state of the flag is checked and if toneEnable is true the tone plays one time and the toneEnable is set false. If the temperature is still over 25 the next iteration of loop() the flag is false so no tone will play. If the temperature drops below 25 the toneEnable is set back to true so the tone can play the next time that the temperature goes over 25.

#include <LiquidCrystal.h> //memanggil library LCD  

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  // pin LCD
int tempPin = A0;  // output pin sensor suhu
int fan = 9;      // output pin kipas
int heater = 8;        // output pin pemanas
int temp; //inisialisasi
int buzzer = 6; //alarm pin

boolean toneEnable = true;  // *****add a flag variable

int readTemp() {  // membaca suhu 
  temp = analogRead(tempPin); //membaca suhu
  return temp * 0.48828125; //mengubah suhu ke celcius
}

void setup() {
  pinMode(fan, OUTPUT); //pin kipas sebagai output
  pinMode(heater, OUTPUT);//pin pemanas sebagai output
  pinMode(tempPin, INPUT);//pin sensor suhu sebagai output
  pinMode(buzzer, OUTPUT);
  lcd.begin(16, 2); //sintaks lcd yang digunakan 16x2
}

void loop() {
  temp = readTemp();    // membaca suhu
  lcd.setCursor(0, 0); // penulisan karakter lcd kolom 0 baris 0
  lcd.print("Temperature="); //menampilkan karakter di lcd
  lcd.setCursor(0, 1); // penulisan karakter lcd kolom 0 baris 1
  lcd.print(temp); //menampilkan hasil suhu
  lcd.println("\337C");//menampilkan pada baris baru
 
  if (temp <= 10) { // jika suhu  <= 10 derajatC
    digitalWrite(fan, LOW); //kipas mati
    digitalWrite(heater, HIGH); //pemanas menyala

toneEnable = true;  // *****enable the tone
  }
  else if ((temp >= 11) && (temp <= 25)) { // jika suhu diatas >=11 derajatC dan <=25derajatC
    digitalWrite(fan, LOW);  //kipas mati
    digitalWrite(heater, LOW) ; // pemanas mati

toneEnable = true;  // *****enable the tone
  }
  else {                  // jika diatas 25 derajat C
    digitalWrite(fan, HIGH);  //kipas menyala
    digitalWrite(heater, LOW) ;//pemanas mati

if(toneEnable == true)  // *****only sound tone 1 time
    {
    tone(buzzer, 250);
    // Waits some time to turn off
    delay(200);
    //Turns the buzzer off
    noTone(buzzer);

toneEnable = false;  // *****disable further tones until temperature drops below 25
    }
  }

delay(10);// mengirimkan data tiap 10milliseconds
}




Please post code using code tags so more people can see it.

its work! thankyou :slight_smile: