void loop() not working

Checked many kind of posts, didn't find one fit my situation.
I just test a buzzer, Serial monitor shown that no loop() happened. why?

/*
SparkFun Inventor's Kit 
Example sketch 11

BUZZER

  Use the buzzer to play a song!

  The buzzer in your Inventor's Kit is an electromechanical
  component you can use to make noise. Inside the buzzer is a
  coil of wire and a small magnet. When current flows through 
  the coil, it becomes magnetized and pulls towards the magnet,
  creating a tiny "click". When you do this thousands of times
  per second, you create tones.

  The Arduino has a built-in command called tone() which clicks
  the buzzer at a certain frequency. This sketch knows the
  frequencies of the common notes, allowing you to create songs.
  We're never going to let you down!

Hardware connections:

  The buzzer has two pins. One is positive and one is negative.
  The postitive pin is marked by a "+" symbol on both the top
  and bottom of the buzzer.

  Connect the positive pin to Arduino digital pin 9.
  (Note that this must be a PWM pin.)
  Connect the negative pin to GND.

  Tip: if the buzzer doesn't fit into the breadboard easily,
  try rotating it slightly to fit into diagonal holes.

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
(This sketch was originally developed by D. Cuartielles for K3)
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/

/*
This sketch uses the buzzer to play songs.
The Arduino's tone() command will play notes of a given frequency.
We'll provide a function that takes in note characters (a-g),
and returns the corresponding frequency from this table:

  note  frequency
  c     262 Hz
  d     294 Hz
  e     330 Hz
  f     349 Hz
  g     392 Hz
  a     440 Hz
  b     494 Hz
  C     523 Hz

For more information, see http://arduino.cc/en/Tutorial/Tone
*/

const int buzzerPin = 9;

// We'll set up an array with the notes we want to play
// change these values to make different songs!

// Length must equal the total number of notes and spaces 

const int songLength = 18;

// Notes is an array of text characters corresponding to the notes
// in your song. A space represents a rest (no tone)

char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest

// Beats is an array of values for each note and rest.
// A "1" represents a quarter-note, 2 a half-note, etc.
// Don't forget that the rests (spaces) need a length as well.

int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};

// The tempo is how fast to play the song.
// To make the song play faster, decrease this value.

int tempo = 113;


void setup() 
{
   Serial.begin(9600);
   
  pinMode(buzzerPin, OUTPUT);

  Serial.print("setup");
    Serial.println();


}


void loop() 
{

  Serial.print("loop");
    Serial.println();
    
  int i, duration;

  for (i = 0; i < songLength; i++) // step through the song arrays
  {
    duration = beats[i] * tempo;  // length of note/rest in ms

    if (notes[i] == ' ')          // is this a rest? 
    {
      delay(duration);            // then pause for a moment
    }
    else                          // otherwise, play the note
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);            // wait for tone to finish
    }
    delay(tempo/10);              // brief pause between notes
  }

  // We only want to play the song once, so we'll pause forever:
  while(true){}
  // If you'd like your song to play over and over,
  // remove the above statement
}


int frequency(char note) 
{
  // This function takes a note character (a-g), and returns the
  // corresponding frequency in Hz for the tone() function.

  int i;
  const int numNotes = 8;  // number of notes we're storing

  // The following arrays hold the note characters and their
  // corresponding frequencies. The last "C" note is uppercase
  // to separate it from the first lowercase "c". If you want to
  // add more notes, you'll need to use unique characters.

  // For the "char" (character) type, we put single characters
  // in single quotes.

  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};

  // Now we'll search through the letters in the array, and if
  // we find it, we'll return the frequency for that note.

  for (i = 0; i < numNotes; i++)  // Step through the notes
  {
    if (names[i] == note)         // Is this the one?
    {
      return(frequencies[i]);     // Yes! Return the frequency
    }
  }
  return(0);  // We looked through everything and didn't find it,
              // but we still need to return a value, so return 0.
}

post024.JPG

Because of this:

  // We only want to play the song once, so we'll pause forever:
  while(true){}

It doesn't look like setup() is running either.

In fact I can't see anything in your sketch that would give the output shown in your image.

Are you sure the sketch is uploaded to the board?

GypsumFantastic:
It doesn't look like setup() is running either.

What do you mean?

It looks like the only character received by Serial Monitor is an unprintable character.

Are you using a board with native USB like a Leonardo or Micro? If so you are required to put this line afdter Serial.begin():

  while (!Serial);

This waits for the USB connection to be established before trying to send characters.

Thanks all.
I guess some thing wrong, the setup didn't run either, I didn't heard any thing.
and some sketch was working yesterday, not work now.
Is that fine to reinstall the IDE?

aarg:
What do you mean?

There's a print statement in setup() that just prints the word "setup". I can't see that in your screenshot of the serial monitor.

It is the Tone library no good, I used 'digitalWrite(Buzzer, HIGH/LOW);' Buzzer works, but tone(Buzzer, 2000) not.
Where can find the Tone.h and how to fix please?
Best

It is the Tone library no good, I used 'digitalWrite(Buzzer, HIGH/LOW);' Buzzer works, but tone(Buzzer, 2000) not.

You have an active buzzer, ie it only requires a voltage across it to make a noise at a single fixed frequency. The Tone library is used with a passive buzzer, ie one that requires the pin that the device is connected to to be turned on and off at the required frequency

So, in order to generate different tones you need to use a passive output device such as a speaker.

UKHeliBob:
You have an active buzzer, ie it only requires a voltage across it to make a noise at a single fixed frequency. The Tone library is used with a passive buzzer, ie one that requires the pin that the device is connected to to be turned on and off at the required frequency

So, in order to generate different tones you need to use a passive output device such as a speaker.

UKHeliBob:
You have an active buzzer, ie it only requires a voltage across it to make a noise at a single fixed frequency. The Tone library is used with a passive buzzer, ie one that requires the pin that the device is connected to to be turned on and off at the required frequency

So, in order to generate different tones you need to use a passive output device such as a speaker.

Thanks.

the first code work, second not.

const int buzzer = 9; //buzzer to arduino pin 9

void setup(){
  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
}

void loop(){
  digitalWrite(buzzer, HIGH); // sets the digital pin 13 on
  delay(1000);            // waits for a second
  digitalWrite(buzzer, LOW);  // sets the digital pin 13 off
  delay(1000);            // waits for a second
}
const int buzzer = 9; //buzzer to arduino pin 9

void setup(){
  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
}

void loop(){
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(1000);        // ...for 1sec
}

the first code work, second not.

Exactly as you would expect if you use an active buzzer with both

Have you tried putting 5V straight across your buzzer ?
Does it buzz ?

Can you post a link to where you got the buzzer ?

UKHeliBob:
Exactly as you would expect if you use an active buzzer with both

Have you tried putting 5V straight across your buzzer ?
Does it buzz ?

Can you post a link to where you got the buzzer ?

UKHeliBob:
Exactly as you would expect if you use an active buzzer with both

Have you tried putting 5V straight across your buzzer ?
Does it buzz ?

Can you post a link to where you got the buzzer ?

Thanks.

it was tested by 5v, OK. tested by another computer OK. just this computer Tone no work. sketch stop as long as meet Tone.

it was tested by 5v,

Do you mean that putting 5V across the buzzer makes it buzz ?

UKHeliBob:
Do you mean that putting 5V across the buzzer makes it buzz ?

Thanks.
yes, 5v directly go +/-, buzz. in fact I used a Triode for my test, and it was working well before.
it is not working since the conflict of Tone.cpp and ultrasonic sensor's NewPing.cpp on timer2. I can't remember what I have done on the cpps, replaced some Tone.cpp from other project. and then then come to here....
Best.

Hmmmm. If your buzzer buzzes on DC, why are you using tone()?

aarg:
Hmmmm. If your buzzer buzzes on DC, why are you using tone()?

Thanks
My Buzzer buzz at few different sounds, which I found online. are there any other solution?
Best.

Yes, digitalWrite(buzzer, HIGH);
...assuming that the pin can safely handle the buzzer supply current (it must be less than 20mA).

Do you not understand that if it makes a noise on 5V, it will make the same noise on 5V from a digital pin?

aarg:
Yes, digitalWrite(buzzer, HIGH);
...assuming that the pin can safely handle the buzzer supply current (it must be less than 20mA).

Do you not understand that if it makes a noise on 5V, it will make the same noise on 5V from a digital pin?

Thanks, you are right.

I am thinking which way easier, modify library or re sketch the music for buzzer, actually simple music.

I found a TonePlayer.cpp, just don't know how to replace the Tone.cpp.

also, JM shown me the pulseIn() solution for ultrasonic sensor, because I have six ultrasonic sensors, seems NewPing is better for that many?

Best.

I am thinking which way easier, modify library or re sketch the music for buzzer, actually simple music.

Neither will work, at least not satisfactorily. If you use Tone then at best you will get the fixed note from the buzzer modulated by the note from Tone, but don't expect miracles. Modifying the Tone library will not allow you to alter the tone produced by the fixed frequency buzzer.

Use a passive output device if you want to use Tone (or any other library) to control the output frequency

UKHeliBob:
Neither will work, at least not satisfactorily. If you use Tone then at best you will get the fixed note from the buzzer modulated by the note from Tone, but don't expect miracles. Modifying the Tone library will not allow you to alter the tone produced by the fixed frequency buzzer.

Use a passive output device if you want to use Tone (or any other library) to control the output frequency

Thanks.
Because the Tone is build in (am I right?), I am try to modify the NewPing.cpp, I shouldn't even think of this in my knowledge. Thing is I'll use servo motors in the future, that very likely result in other conflict.

I am planning just simply modify the Timer2 as Timer3 in many places, can it work?
There is a line like : ASSR &= ~(1<<AS2);
what the AS2 means? is it belong to Timer2?

Best