Arduino DoorBell.

Hi guys I'm trying to make a simple doorbell with my arduino a speaker and a button. I need to be able to have the arduino play a tone when the button is pressed. and create no tone when it isn't pressed. I tried creating the code myself but when I go to verify/compile it gives me errors.

I tried creating the code myself but when I go to verify/compile it gives me errors.

That code was?
The errors were?

This is what I think will work. For the hardware, just plug the positive of the speaker or buzzer into the pin you want, and the negative in the GND of the Arduino. Wire the button like you would a regular button. You just have to change some variables in the code I gave you below. :slight_smile:

int buttonPin=pin number here;
int speakerPin=pin number here;
int pitch=pitch number here;
int button;

void setup(){
pinMode(buttonPin,INPUT);
}

void loop(){
button=digitalRead(buttonPin);
if (button==HIGH){
Tone(speakerPin,pitch,100);
}else{
noTone(speakerPin);
}
delay(1);
}

here is my original code.

#include <Tone.h>

int buttonPin = 13;
int buttonState = 0;
Tone tone1;

void setup(){

tone1.begin(12);
}

void loop(){

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
tone1.play(NOTE_A4);
delay(100);
tone1.play(NOTE_D8);

if (buttonState == LOW)
noTone(12);

}

This should work then:

int buttonPin=13;
int speakerPin=12;
int button;

void setup(){
pinMode(buttonPin,INPUT);
}

void loop(){
button=digitalRead(buttonPin);
if (button==HIGH){
tone(speakerPin,440,100);
delay(100);
tone(speakerPin,4699,100);
delay(100);
}else{
noTone(speakerPin);
}
delay(1);
}

Hi,

  1. How have you connected the button? Normally, you want to connect a push button between an Arduino input pin and ground, and enable the internal pullup on that input. The input will read HIGH when the buttion is not pressed, and LOW when it is pressed. This is the opposite way round to your code.

  2. Pin 13 is not a good choice for the button pin because on many Arduinos it is connected to a LED.

  3. In setup() you need to call pinMode(buttonPin, INPUT) to make it an input, then digitalOutputdigitalWrite(buttonPin, HIGH) to enable the pullup.

I thought it was digitalWrite instead of digitalOutput.

the errors are as follows. it says something about tone.ccp being first defined here and multiple definition of 'timer0_pin_mask'
and multiple definition of 'timer1_pin_port'
and multiple definition of 'timer0_toggle_count'
and multiple definition of 'timer1_toggle_count'
and multiple definition of 'timer1_pin_mask'
and multiple definition of 'timer1_pin_port'
and multiple definition of 'timer2_pin_port'
and multiple definition of 'timer2_pin_mask'
and multiple definition of 'timer2_toggle_count'
and multiple definition of '_vector_7'
those are the errors.

Try changing the button to pin 11?

dc42:
Hi,

  1. How have you connected the button? Normally, you want to connect a push button between an Arduino input pin and ground, and enable the internal pullup on that input. The input will read HIGH when the buttion is not pressed, and LOW when it is pressed. This is the opposite way round to your code.

  2. Pin 13 is not a good choice for the button pin because on many Arduinos it is connected to a LED.

  3. In setup() you need to call pinMode(buttonPin, INPUT) to make it an input, then digitalOutput(buttonPin, HIGH) to enable the pullup.

I connected the button via ground and a resistor

Soapy29:
I thought it was digitalWrite instead of digitalOutput.

Quite right - I've edited my post.

Ok.

Arduino02:
the errors are as follows. it says something about tone.ccp being first defined here and multiple definition of 'timer0_pin_mask'
and multiple definition of 'timer1_pin_port'
and multiple definition of 'timer0_toggle_count'
and multiple definition of 'timer1_toggle_count'
and multiple definition of 'timer1_pin_mask'
and multiple definition of 'timer1_pin_port'
and multiple definition of 'timer2_pin_port'
and multiple definition of 'timer2_pin_mask'
and multiple definition of 'timer2_toggle_count'
and multiple definition of '_vector_7'
those are the errors.

In my Arduino software there is no Tone.h file and no Tone class. You just use tone() and noTone() to generate tones. See http://arduino.cc/en/Reference/Tone.

Like I said before.

even when I just get near the wire it goes off. It would make a good security alarm.

Live video chat rooms, simple and easy. - Tinychat. there's a free chat room setup for us arduino users.

Kind of misses the point of forum communication - others can search and use the information discussed without having to ask the same old questions over and over (I know - I didn't say it was perfect)

yes it isn't perfect but it works. I set it up so the people who are online on the forum could go on there and get answers a bit quicker.

when i tried doing it the speaker would constantly go off