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