Tones and Buttons causing program to stop.

Hello!

This is my first post here. I searched around for a similar topic but, feel free to link me if this has been covered elsewhere.

I have a minorly complicated sketch that I am having trouble with but I was able to isolate the problem and create a simpler sketch to illustrate it for troubleshooting.

I can have my arduino output 4 tones simultaneously to one speaker (or a speaker for each if I wish) but as soon as I add a button press into the mix upon pressing the button the whole program stops. If it's playing a tone(s) the tone sticks and everything becomes unresponsive, if the tone is not playing the program sticks and it won’t play anything until the arduino is reset. This problem starts at 3 Tones.

I am using an Uno R3 (haven’t tried it on any of my duemilanove's now that I think about it but will try it when I get home). I am using IDE V1. I haven't tried any other versions of the IDE. The OS I am using is Ubuntu Linux.

Here is the simple sketch that clearly illustrates the problem.

#include <Tone.h>

Tone tone1;
Tone tone2;
Tone tone3;
Tone tone4;

int buttonPin (5);
int buttonValue = 0;

void setup()
{
  Serial.begin(9600);
  tone1.begin(A1);
  tone2.begin(A2);
  tone3.begin(A3);
  tone4.begin(A4);
  pinMode(buttonPin, INPUT);  
}

void loop()
{
  buttonValue = digitalRead(buttonPin);

  if(buttonValue == HIGH)
  {
    tone1.play(NOTE_B3);
    tone2.play(NOTE_G3);
    tone3.play(NOTE_E3);
    tone4.play(NOTE_C3);
  }
}

If you comment out tones 3 and 4 the sketch should play tones 1 and 2 when you press the button and stop when you release it. With Tone 3 and/or 4 not commented out the program freezes.

If you remove the button press all 4 tones play fine and the program does not freeze (I know this by setting up a serial readout that I can observe functioning. The serial read out stops with the button press issue.)

I have tried the tone output on digital pins as well as analog pins (as it's setup now).

Any ideas?

Thanks
Erik

How IS your switch wired?

And what have you got connected to the output pins?

PaulS -

The button is wired exactly like this:

I am using a 100 ohm resistor.

dxw00d -
The output pins are all wired to an 8 ohm speaker with 100 ohm resisters on each from the output pins, not the ground.

Thanks for your time!

I am using a 100 ohm resistor.

Using Ohm's law, V=IR. V =5, R = 100, so I = 0.05 A, or 50 milliamps. That's a lot of current to be wasting. A bigger resistor would surely be in order. Or, use the internal pullup resistors. They are around 20K ohms.

Thanks PaulS! I will put a bigger resistor on the button when I get home tonight and give that a try. Do you think that's causing the issue or just advising on best use?

I have put bigger resistors on the speaker but the volume is lower than I want it so I stuck with the 100 ohm.

Do you think that's causing the issue or just advising on best use?

Just advice.

I have put bigger resistors on the speaker but the volume is lower than I want it so I stuck with the 100 ohm.

Ohm's law comes into play here, too. When you activate a digital pin with a 108 ohm resistance, nearly 50 milliamps of current is being drawn. That exceeds the maximum 40 milliamps that can be pulled. The recommended maximum value for longevity is 20 milliamps.

You need an amp.

You need an amp.

I have a LM3875 I can put on it I think. Thanks for the advice! I get sloppy with my electronics wiring because I am better with the code but will be more diligent in the future!

Thanks again.