Delay Before and After Buzzer Turns On or Off

Hi all,

I am working on a very simple project that when you press a button a buzzer sounds until you let go of the button.

It is working well, but there is a slight delay from when you press the button and when the buzzer sounds, and there is about 1 seconds delay from when you let go of the the button and when the buzzer stops.

Code:

  // constants won't change. They're used here to set pin numbers:
  const int buttonPin = 9;     // the number of the pushbutton pin
  int piezoPin = 8;

  // variables will change:
  int buttonState = 0;         // variable for reading the pushbutton status

  void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  }

  void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
   tone(piezoPin, 1000);
   } 
   if (buttonState == LOW) {
  noTone (piezoPin);
   } 
}

Is it possible to get rid of these delays?

Thanks very much,

Zeb

OOOPS! Sorry Posted to Soon!

I uploaded the same code again and it works!

No idea why this happened, perhaps upload error?

Thanks very much,

Zeb

  1. How is your button wired? Do you have an external pull-up or pull-down? Which? Why don't you use the internal built in pull-up and wire the button the normal way where it reads LOW when it's pushed?

  2. Yeah you can get rid of the 1000ms delay. Go look at the doc for the tone function that you are using. What does that second parameter mean? The one where you put 1000? Was there another form of that function that did something different?

Yes, it is important to know how you wired your switch.

FYI

Hi all,

Thanks for the link Delta_G!

I followed this tutorial : >>>> Click Here <<<<

I reloaded the program and it now works, not sure what went wrong!

Thanks

Zeb