toneMelody...without a tone?

Hi guys - annoying newbie here again. This time, I’m working on a Portal (the game) Companion Cube. The cube will light up and play the credits theme as the input buttons are pressed. I’ve got a functioning code it seems… but no sound will come out of my buzzer.

I’m using a leonardo and bits of a 321 shield
Heres what I’m working with:
#include “pitches.h”
#define REST 0

// each pin corresponds to an LED color:
int led0 = 10;
int led1 = 11;
int led2 = 12;

//declare internal variables
int brightness = 200;
int red = 0;
int blue = 0;
int green = 0;

//declare buzzer and the buttons
const int buzzerPin = 5; //The buzzerPin is connected to pin 5 of the Arduino.
int button1Pin = 2; //The SW1 button is connect to pin 2 of the Arduino.
int button2Pin = 3; //The SW2 button is connected to pin d3 of the Arduino.

// this routine runs each time you hit the reset button
void setup() {
// declare the relevant pins to be output
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buzzerPin, OUTPUT); //Setup buzzer pin as an output pin.
pinMode(button1Pin, INPUT); //Setup button1 pin as an input pin.
pinMode(button2Pin, INPUT); //Setup button2 as an input pin.

}

// this routine loops indefinitely
void loop() {
if (digitalRead(button1Pin) == LOW) { //Check to see if button1 is pressed.
for (float x=0;x < PI; x = x + 0.000004){
red = brightness * abs(sin(x*(180/PI))); // calculate red brightness
green = brightness * abs(sin((x+PI/3)(180/PI))); // calculate green brightness
blue = brightness * abs(sin((x+(2
PI)/3)*(180/PI)));// calculate the blue brightness
analogWrite(led0, red); // send the value to the LED
analogWrite(led1, green); // send the value to the LED
analogWrite(led2, blue); // send the value to the LED

if (digitalRead(button2Pin) == LOW) { //Check to see if button2 is pressed.
// First line
double song = {

// First line
REST, 2.0, NOTE_G3, 0.5, NOTE_FS3, 0.5, NOTE_E3, 0.5, NOTE_E3, 0.5,
NOTE_FS3, 2.0, REST, 2.0,

REST, 1.5, NOTE_A2, 0.5,
NOTE_G3, 0.5, NOTE_FS3, 0.5, NOTE_E3, 0.5, NOTE_E3, 1.0,

NOTE_FS3, 1.5,
NOTE_D3, 1.0, NOTE_E3, 0.5, NOTE_A2, 2.5,

REST, 1.5, NOTE_A2, 0.5,

// Second line
NOTE_E3, 1.0, NOTE_FS3, 0.5, NOTE_G3, 1.5, NOTE_E3, 0.5, NOTE_CS3, 1.0,
NOTE_D3, 1.5, NOTE_E3, 1.0, NOTE_A2, 0.5, NOTE_A2, 1.0,
NOTE_FS3, 1.5, REST, 2.0,

REST, 2.0, NOTE_G3, 0.5, NOTE_FS3, 0.5, NOTE_E3, 0.5, NOTE_E3, 0.5,
NOTE_FS3, 2.0, REST, 2.0,

// Third line
REST, 1.0, REST, 0.5, NOTE_A2, 0.5, NOTE_G3, 0.5, NOTE_FS3, 0.5, NOTE_E3, 0.5, NOTE_E3, 1.5,
NOTE_FS3, 0.5, NOTE_D3, 1.5, NOTE_E3, 0.5, NOTE_A2, 2.5,
REST, 2.0,

NOTE_E3, 1.0, NOTE_FS3, 0.5, NOTE_G3, 1.5, NOTE_E3, 0.5, NOTE_CS3, 1.5,
NOTE_D3, 0.5, NOTE_E3, 1.0, NOTE_A2, 0.5, NOTE_D3, 0.5, NOTE_E3, 0.5,

// Fourth line
NOTE_F3, 0.5, NOTE_E3, 0.5, NOTE_D3, 0.5, NOTE_C3, 0.5, REST, 1.0, NOTE_A2, 0.5, NOTE_AS2, 0.5,
NOTE_C3, 1.0, NOTE_F3, 1.0, NOTE_E3, 0.5, NOTE_D3, 0.5, NOTE_D3, 0.5, NOTE_C3, 0.5,
NOTE_D3, 0.5, NOTE_C3, 0.5, NOTE_C3, 1.0, NOTE_C3, 1.0, NOTE_A2, 0.5, NOTE_AS2, 0.5,
NOTE_C3, 1.0, NOTE_F3, 1.0, NOTE_G3, 0.5, NOTE_F3, 0.5, NOTE_E3, 0.5, NOTE_D3, 0.5,
NOTE_D3, 0.5, NOTE_E3, 0.5, NOTE_F3, 1.0, NOTE_F3, 1.0, NOTE_G3, 0.5, NOTE_A3, 0.5,

// Fifth line
NOTE_AS3, 0.5, NOTE_AS3, 0.5, NOTE_A3, 1.0, NOTE_G3, 1.0, NOTE_F3, 0.5, NOTE_G3, 0.5,
NOTE_A3, 0.5, NOTE_A3, 0.5, NOTE_G3, 1.0, NOTE_F3, 1.0, NOTE_D3, 0.5, NOTE_C3, 0.5,
NOTE_D3, 0.5, NOTE_F3, 0.5, NOTE_F3, 0.5, NOTE_E3, 1.0, NOTE_E3, 0.5, NOTE_FS3, 0.5, NOTE_FS3, 1.5,

// and belive me…
REST, 2.0, NOTE_A3, 0.5, NOTE_A3, 0.5,
NOTE_B3, 0.5, NOTE_A3, 0.5, NOTE_FS3, 0.5, NOTE_D3, 1.0, NOTE_E3, 0.5, NOTE_FS3, 0.5, NOTE_FS3, 1.5,
REST, 1.5, NOTE_A3, 0.5, NOTE_A3, 0.5, NOTE_A3, 0.5,

// science and im still…
NOTE_B3, 0.5, NOTE_A3, 0.5, NOTE_FS3, 0.5, NOTE_D3, 1.0, NOTE_E3, 0.5, NOTE_FS3, 0.5, NOTE_FS3, 1.5,

REST, 1.5, NOTE_A3, 0.5, NOTE_A3, 0.5, NOTE_A3, 0.5,
NOTE_B3, 0.5, NOTE_A3, 0.5, NOTE_FS3, 0.5, NOTE_D3, 1.0, NOTE_E3, 0.5, NOTE_FS3, 0.5, NOTE_FS3, 1.5,

REST, 2.0, NOTE_A3, 0.5, NOTE_A3, 0.5,
NOTE_B3, 0.5, NOTE_A3, 0.5, NOTE_FS3, 0.5, NOTE_D3, 1.0, NOTE_E3, 0.5, NOTE_FS3, 0.5, NOTE_FS3, 1.5,

REST, 1.5, NOTE_A3, 0.5, NOTE_A3, 0.5, NOTE_A3, 0.5,
NOTE_B3, 0.5, NOTE_A3, 0.5, NOTE_FS3, 0.5, NOTE_D3, 1.0, NOTE_E3, 0.5, NOTE_FS3, 0.5, NOTE_FS3, 1.5,

REST, 1.5, NOTE_A3, 0.5, NOTE_A3, 0.5, NOTE_A3, 0.5,
NOTE_B3, 0.5, NOTE_A3, 0.5, NOTE_FS3, 0.5, NOTE_D3, 1.0, NOTE_E3, 0.5, NOTE_FS3, 0.5, NOTE_FS3, 1.5,

// still alive… still alive…
REST, 1.5, NOTE_G3, 0.5, NOTE_A3, 0.5, NOTE_A3, 1.5,
REST, 1.5, NOTE_G3, 0.5, NOTE_FS3, 0.5, NOTE_FS3, 1.5,

// End of song
0.0, 0.0
};
}
}
}
}

Do you write to the buzzer pin anywhere in the code ?

UKHeliBob:
Do you write to the buzzer pin anywhere in the code ?

Hi! First chunk, const int buzzerPin = 5; //The buzzerPin is connected to pin 5 of the Arduino. - on the shield D5 is the buzzer

If there is any code in there that even attempts to write anything to the buzzer perhaps you could point it out to me. I can't see it. You have a buzzerPin defined but there is no code that does anything to it.

I think maybe you did a cut and paste job from somewhere and left some important parts of the code behind.

Steve

slipstick:
If there is any code in there that even attempts to write anything to the buzzer perhaps you could point it out to me. I can't see it. You have a buzzerPin defined but there is no code that does anything to it.

I think maybe you did a cut and paste job from somewhere and left some important parts of the code behind.

Steve

Is it not declared? the SW2 followed by the tones/notes. I added the noted exactly where the tone began on the Melodytone example sketch, but obviously that isn't right... What I had meant to do is have the SW2 button pressed to sound the buzzer to play the song. What would be the best way to fix that section?

Provide a link to the "Melodytone example sketch" and we can go from there. Did you actually mean Tone Melody? If so you left out the tone() command...and that's the one that makes the noise.

Steve

Nothing in the code changes the state of the buzzer pin so how is it ever going to make a noise ?

Just as baffling is that you have a series of notes in the song array and never read anything from the array.

You need to start with a very simple sketch from which you can learn how to use the buzzer and verify it's working correctly, then move on to integrating that code into your more complex project.

slipstick:
Provide a link to the "Melodytone example sketch" and we can go from there. Did you actually mean Tone Melody? If so you left out the tone() command...and that's the one that makes the noise.

Steve

...yep. Looks like that's what I missed.

pert:
You need to start with a very simple sketch from which you can learn how to use the buzzer and verify it’s working correctly, then move on to integrating that code into your more complex project.

I have. The buzzer works just fine with the 321 Lab. Which is:

#include “pitches.h”;
int buzzerPin = 5 ; //The buzzerPin is connected to pin 5 of the Arduino.
int button1Pin = 2; //The SW1 button is connect to pin 2 of the Arduino.

void setup() { //The Setup function runs once.
pinMode(buzzerPin, OUTPUT); //Setup red LED pin as an output pin.
pinMode(button1Pin, INPUT); //Setup button1 pin as an input pin.
}

void loop() { //The loop function runs forever.
if (digitalRead(button1Pin) == LOW) { //Check to see if button1 is pressed.
tone(buzzerPin, NOTE_D2,73);
delay(408);
tone(buzzerPin, 2000,50);
delay(400);
tone(buzzerPin, NOTE_G2,98);
delay(1000);
tone(buzzerPin, NOTE_G4,408);
delay(408);

Basically, the code I’m trying to copy and add the LED to is this:

slipstick:
Provide a link to the “Melodytone example sketch” and we can go from there. Did you actually mean Tone Melody? If so you left out the tone() command…and that’s the one that makes the noise.

Steve

Okay, this is where I’m currently at. Does this seem to be on track?

*/
#include “pitches.h”
#define REST 0

// each pin corresponds to an LED color:
int led0 = 10;
int led1 = 11;
int led2 = 12;

//declare internal variables
int brightness = 200;
int red = 0;
int blue = 0;
int green = 0;

//declare buzzer and the buttons
const int buzzerPin = 8; //The buzzerPin is connected to pin 5 of the Arduino.
int button1Pin = 2; //The SW1 button is connect to pin 2 of the Arduino.
int button2Pin = 3; //The SW2 button is connected to pin d3 of the Arduino.
int melody = { //The notes in the song
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int noteDurations = { // note durations: 4 = quarter note, 8 = eighth note, etc.
4, 8, 8, 4, 4, 4, 4, 4
};

// this routine runs each time you hit the reset button
void setup() {
// declare the relevant pins to be output
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buzzerPin, OUTPUT); //Setup buzzer pin as an output pin.
pinMode(button1Pin, INPUT); //Setup button1 pin as an input pin.
pinMode(button2Pin, INPUT); //Setup button2 as an input pin.

for (int thisNote = 0; thisNote < 8; thisNote++) { // iterate over the notes of the melody:

// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);

// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}

// this routine loops indefinitely
void loop() {
if (digitalRead(button1Pin) == LOW) { //Check to see if button1 is pressed.
for (float x=0;x < PI; x = x + 0.000004){
red = brightness * abs(sin(x*(180/PI))); // calculate red brightness
green = brightness * abs(sin((x+PI/3)(180/PI))); // calculate green brightness
blue = brightness * abs(sin((x+(2
PI)/3)*(180/PI)));// calculate the blue brightness
analogWrite(led0, red); // send the value to the LED
analogWrite(led1, green); // send the value to the LED
analogWrite(led2, blue); // send the value to the LED

if (digitalRead(button2Pin) == LOW) { //Check to see if button2 is pressed.

double song = {

// First line
REST, 2.0, tone,NOTE_G3, 0.5, tone,NOTE_FS3, 0.5, tone,NOTE_E3, 0.5, tone,NOTE_E3, 0.5,
tone,NOTE_FS3, 2.0, REST, 2.0,

REST, 1.5,tone,NOTE_A2, 0.5,
tone,NOTE_G3, 0.5, tone,NOTE_FS3, 0.5,tone,NOTE_E3, 0.5, tone,NOTE_E3, 1.0,

tone,NOTE_FS3, 1.5,
tone,NOTE_D3, 1.0, tone,NOTE_E3, 0.5, tone,NOTE_A2, 2.5,

REST, 1.5, tone,NOTE_A2, 0.5, ecttttttt

Does this seem to be on track?

No

Look how the tone() function is used in the code in reply #9

tone(buzzerPin, tone(buzzerPin, NOTE_D2,73);,73);  // use the pin named buzzerpin and send the note with a previously defined value of NOTE_D2 for 73 milliseconds

Where is the tone() function used like that in the code in reply #10 ?

The purpose of the song array is to hold the notes in the required sequence. You then iterate through the array and pass the details of the required note to the tone() function.

In your program you have an array melody and another noteDurations. Do you understand their purpose? Are you using them anywhere in the code for anything?

And then you have another array called song which seems to sort of contain note information though it is not very clear what the numbers 1.5, 0.5, 2.0 etc are supposed to mean. Normally you would expect that song would be created at the top of the program (like melody is). Then in the loop() section you would expect to see a for loop reading data OUT OF song and passing that data (suitably converted so 1.5 meant something useful) to a tone() function to play each note. None of that seems to be there.

Trying to make a program by cutting and pasting from several other programs when you don't understand how any of them work is a frustrating process.

Steve