Tone Generation

Hi Guys,

I'm new to Arduino, rusty at programming and busting my bottom reading tutorials and trying stuff out. I love the Arduino! I haven't had this much fun in a long time!

A question: I learn best by figuring out stuff on my own with just a little push in the right direction. I'm wanting to use the tone library (which I downloaded) to figure some stuff out.

Basically what I wanna do is write code that I press a momentary switch on a pin and then have a tone play on an output pin....I'll hook up a little speaker.

I've read Tom Igoe's tutorial, tried the code, read other's code and it's all way fancier than I want for my learning exercise....

Can anyone help me? I know I need to create an instance of the library, setup digitalRead on a pin, assign a tone to that pin to play as output. Once I can get that little basic code figured out, I know I can then add a bunch of input pins and associate them to different notes....I'm just not sure what code to write....

Any help is greatly appreciated!

Annie in Idaho!

:slight_smile:

Welcome to the forum, Annie in Idaho.

You can take the code example that you have already seen on the tone library page -

#include <Tone.h>

Tone tone1;

void setup()
{
  tone1.begin(13);
  tone1.play(NOTE_A4);
}

void loop()
{
}

and integrate it with this snippet of code from the button tutorial at http://arduino.cc/en/Tutorial/Button -

 const int buttonPin = 2;     // the number of the pushbutton pin

 int buttonState = 0;         // variable for reading the pushbutton status

 void setup() {
   // 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 your tone here    
   } 
 }

The circuit diagram for the button is at the link above.

Hope that helps.

Thank you so much, TBar, that will get me started!!!

One question…I know the #include <Tone.h> command references the tone library…what do you call the next line you wrote? The line that says: Tone tone1;

What is it called when you do that? For example I know that Serial.begin(9600) starts the serial monitor session…initializes it so to speak…I’m just curious what you call the Tone tone1 line…

Thanks again!

Annie in Idaho!

Tone tone1;

Tone is the name of a class. tone1 is an object whose type is that of the class.

Thanks, Paul!

So a class is like an 'object' in object oriented programming...is that correct?

Annie in Idaho

A class defines an object. An object is an instance of a class.

Thanks, again Paul!!!

Hey, you're in Seattle...I bet that's the closest anyone with an Arduino is to me in Idaho, hah!

Annie

Hi Guys,

Sadly my code isn’t working. I still don’t quite have the ‘hang’ of using instances and methods and libraries…I’m working on it, though!!!

Here is the code that isn’t working…but, once again, remember I’m just trying to have one note play with one button press…once I get that running, I’ll add more buttons and more notes to better understand what is going on…here’s the code:

#include <Tone.h>

#define speaker 9
#define button 4

Tone tone1;

int buttonState = 0//store value of button pin

void setup(){
  pinMode(speaker, OUTPUT);
  pinMode(button, INPUT);
  tone1.begin(13);
  
}

void loop(){
  buttonState = digitalRead(button);
  speaker = tone1.play(NOTE_A4);
   if (button == HIGH){
    tone1.play(NOTE_A4);
  }
}

I’m getting an error message that says: error: expected unqualified-id before numeric constant In function ‘void loop()’:

Thanks for the help!

Annie in Idaho

Here's a ; to put after the "int buttonState = 0" statement.

Try this and check the differences with what you have:

#include <Tone.h>

#define SPEAKER 9
#define BUTTON 4

Tone tone1;

int buttonState = 0; //store value of button pin

void setup()
{
  pinMode(BUTTON, INPUT);
  tone1.begin(SPEAKER);
}

void loop()
{
  buttonState = digitalRead(BUTTON);
  if (buttonState == HIGH)
  {
    // play a tone, then wait for a bit before checking the button again
    tone1.play(NOTE_A4);
    delay(500);  // 500 = 500 ms or 1/2 second
    tone1.stop();
  }
}

I put the delay in there, but you don’t necessarily need it. Without it, the tone1.play() method will just be called really often when the button is pressed.

(Note: I’ve made some coding style differences - these are just my own personal preferences.)

b

Thank you, Bhagman and PaulS!

I've got it working and I've added more buttons to more pins to play different notes...all is well! You guys are my heroes :slight_smile:

I still do have another question about libraries, objects and stuff....

In the beginning of the sketch we include the tone library:

#include <Tone.h>

Then after the pin definitions there is this:

Tone tone1;

Is tone1 a random name? Could it just as easily been

Tone Fred???

Or is tone1 a necessary part of the library? Does my question make sense?

Thanks again to all!

Annie in Idaho

yea it could be pretty much anything

your initing a class with the name Fred, bob, Tone1, whatever best suits the readability of your script

ie tone1 is on button1 makes since vs cat is on dog

#include <Tone.h>

#define SPEAKER 9
#define BUTTON 4

Tone tone1;

int buttonState = 0; //store value of button pin

void setup()
{
  pinMode(BUTTON, INPUT);
  tone1.begin(SPEAKER);
}

void loop()
{
  buttonState = digitalRead(BUTTON);
  if (buttonState == HIGH)
  {
    // play a tone, then wait for a bit before checking the button again
    tone1.play(NOTE_A4);
    delay(500);  // 500 = 500 ms or 1/2 second
    tone1.stop();
  }
}

I unloaded this code to kit. the result is tone keeps playing= =
SWITCH is attached from pin4 to 5V, and speaker is attached pin9 to 5V.

Do you have any type of pull-up resistor? (or pull-down, i guess.)
When the switch pin is not connected to anything, it is floating, and can randomly report as being either high or low erratically.
Try doing digitalwrite(switchpin, HIGH) to use th built in pullups, and connect the switch to ground instead of 5v. You may have to change a HIGH to a LOW in the code somewhere.