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....
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.
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...
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()':
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.)
#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.