Turning on and off buzzer after certain time

Hi there, pardon for my English.

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?

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?

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.

you wish every 10 seconds you play a tone for five seconds?
try

 tone(piezoPin, 10000, 5000);
  delay (10000);

Im sorry I think I havent made myself clear.

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.

something along the lines of

void loop()
{
   if(alarm)
    {
    delay (10000);
    tone(piezoPin, 10000, 5000);
}

when the alarm goes off after ten seconds the buzzer sounds for 5 seconds
what is your 'alarm' condition

horace:
something along the lines of

void loop()

{
  if(alarm)
    {
    delay (10000);
    tone(piezoPin, 10000, 5000);
}



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?

the code of post #5 is in a continuous loop (the loop() function does just that, it loops)
so each loop the alarm condition is tested

  1. if no alarm loop() exits and then executes again
  2. if alarm is true after ten seconds the tone starts for 5 seconds and loop executes again

do you wish to delay until the tone is finished before looping and testing alarm again, e.g.

void loop()
{
   if(alarm)
    {
    delay (10000);
    tone(piezoPin, 10000, 5000);
    delay(5000);
}

perhaps give use a detailed specification of what you are attempting to do?

edited

Im so sorry for my English, please have patience.

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?

I really appreciate everybody's help.

setup() only runs once so put your code there, e.g.

int piezoPin = 6;

void setup()
{
    delay (10000);
    tone(piezoPin, 10000, 5000);
}

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.

Can you explain a bit more about boolean?

Izzy13:
Can you explain a bit more about boolean?

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()