Currently my project requires me to turn on and off buzzer at certain time. For example, after 15 seconds, the buzzer will turn on and then turn off after a while.
int piezoPin = 6;
void setup() {
}//close setup
void loop() {
/*Tone needs 2 arguments, but can take three
1) Pin#
2) Frequency - this is in hertz (cycles per second) which determines the pitch of the noise made
3) Duration - how long teh tone plays
*/
tone(piezoPin, 10000, 500);
delay (10000);
//tone(piezoPin, 1000, 500);
//delay(1000);
}
This is the code that I am currently using. From what I understand is that the delay and the buzzer is turned on at the same time and the long delay will start first and the followed by short buzzer. Is there any way to improve this?
horace:
it looks like your tone plays for 500mSec
when the tone starts you delay for 10000mSec
so every 10 seconds you play a tone for half a second?
Yes that is currently happening. However, my objective is to turn it on after 10 seconds where it will around for say 5 seconds before turning off immediatly.
My project is basically an alarm, and I want to turn on the buzzer to act as an alarm. My aim is after around 10 seconds, the buzzer will activate only once and produce noise for around 5 seconds before turning off completely.
when the alarm goes off after ten seconds the buzzer sounds for 5 seconds
what is your 'alarm' condition
I think I'm beginning to see your point. However, as of now, the alarm condition is the 10 seconds after uploading the code into the Arduino. Is that possible?
Basically this only a trial for my project. What I am hoping for is after uploading the code into Arduino, 10 seconds will pass by. Only then will buzzer turn on for 5 seconds before turning off.
Currently what my code does is it will delay 10 seconds after uploading before turning buzzer on for 5s and then repeat. Upon reading your replies I understand that this is due to void loop. However, is there anyway for it to be turned off and no longer loop, even in void loop?
s there anyway for it to be turned off and no longer loop, even in void loop?
Make running the "once only" code dependant on a boolean variable which is initially set to true. In the "once only" code set the boolean to false so that next time through loop() the "once only" code will not be executed.
A boolean variable can have only one of two values, true or false so you can do something like this pseudo code
set boolean variable okToRun to true
start of loop()
if okToRun equals true
//code here will only execute if the boolean is true
//execute the code
set okToRun to false //stop it running next time through loop()
end if
end of loop()