Ok, I've been googling this for a while now and can't seem to find an answer to my question. I have a push button and when you push that button, the piezo speaker will play a song. I can't seem to figure out the electronics, nor the coding (cause I'm a n00b).
I've already got that part ;-). And I also know how to connect a LED with a pushbutton. But I specifically don't know how to do it with the piezo speaker.
Ok this is fairly simple. Build up the circuit like in this Button Tutorial: http://www.arduino.cc/en/Tutorial/Button
Then connect the positive wire of the piezo to digital pin 9 and the black wire one piezo the ground pin.
Then upload this code to the Arduino ( modified version of Button Tutorial):
/*
Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 7.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 17 Jun 2009
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
int Buzzer1 = 9;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the piezo as output:
pinMode(Buzzer1, OUTPUT);
// 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) {
// play th Music
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,450,225);
delay(300);
tone(Buzzer1,450,225);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,450,200);
delay(300);
tone(Buzzer1,600,300);
delay(300);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,700,300);
delay(300);
tone(Buzzer1,700,300);
delay(500);
tone(Buzzer1,600,300);
delay(300);
tone(Buzzer1,400,200);
delay(1000);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,650,200);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,650,200);
delay(300);
tone(Buzzer1,650,200);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,400,200);
delay(1000);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,600,300);
delay(500);
tone(Buzzer1,600,300);
delay(500);
tone(Buzzer1,800,300);
delay(500);
tone(Buzzer1,800,300);
delay(500);
tone(Buzzer1,400,200);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,400,200);
delay(500);
tone(Buzzer1,300,150);
delay(500);
tone(Buzzer1,300,150);
delay(500);
tone(Buzzer1,300,150);
delay(500);
tone(Buzzer1,300,150);
delay(500);
tone(Buzzer1,300,150);
tone(Buzzer1,300,150);
tone(Buzzer1,300,150);
tone(Buzzer1,300,150);
}
}
It looks quite long but it is simple. If i´m wright the buzzer will play a frequency between the two values (Buzzer1,300,150)
If it does not work tell me and i´ll check the code again tommorow.
This works perfectly! Thanks so much! ^^
Sorry if this is a really old topic, but I hope someone responds.
I have setup the circuit exactly [I think] and it just starts playing a bunch of tones. The button does nothing.
Setup picture:
sorry if that's not a good picture.