My code:
// Pin 13 has a LED connected on most Arduino boards.
// give it a name:
int led = 13;
const int buttonPin = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
// the loop routine runs over and over again forever:
void loop()
{
bool buttonState = digitalRead(buttonPin);
while (buttonState == LOW)
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
tone(12,260);
{
}
}
}
What I wish to do, is that when the button is pressed, and the Led's start to blink, I want the piezo buzzer to make a sound. I have never worked with them before so I am having troubles getting results. Is it that the setup is wrong or the code needs extra lines than just the tone(12,260);
that I added?
buttonState
and start usingdigitalRead
everywhere instead. You need current state of pin, not the state loaded looooong time ago. ThebuttonState
won't magically change a state without any assigment into it!