You are on page 1of 3

Print Page - temperature control https://forum.arduino.cc/index.php?action=printpage;topic=548932.

Arduino Forum
Using Arduino => Project Guidance => Topic started by: ping25 on May 22, 2018, 02:18 pm

Title: temperature control


Post by: ping25 on May 22, 2018, 02:18 pm

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

Title: Re: temperature control


Post by: 6v6gt on May 22, 2018, 03:05 pm

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.

Title: Re: temperature control


Post by: groundFungus on May 22, 2018, 03:06 pm

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.

Code: [Select]
#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

1 dari 3 07/05/2019 19:05


Print Page - temperature control https://forum.arduino.cc/index.php?action=printpage;topic=548932.0

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.

Title: Re: temperature control


Post by: spycatcher2k on May 22, 2018, 04:07 pm

Quote
i have try it. but it doesnt work.
the buzzer wont stop and the fan doesnt work

So post the new code you have tried, along with the schematic for the circuit you have built! We are
NOT mind readers.

Title: Re: temperature control


Post by: ping25 on May 22, 2018, 04:33 pm

Quote from: groundFungus on May 22, 2018, 03:06 pm


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.

Code: [Select]
#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

2 dari 3 07/05/2019 19:05


Print Page - temperature control https://forum.arduino.cc/index.php?action=printpage;topic=548932.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 :)

SMF 2.1 Beta 1 © 2014, Simple Machines

3 dari 3 07/05/2019 19:05

You might also like