YourDuinoStarter BeeperTone

From ArduinoInfo
Jump to: navigation, search

YourDuinoStarter Example: - Beeper_Tone

This is easy to connect. Just use two wires to plug onto the Beeper (Green back, both pins the same length) and then to Pin 11 and GND. Or plug the Electronic Brick version on a 3-pin cable.

This needs the Library "pitches" which must be in the same folder as the sketch. In current versions of the Arduino IDE there is a "Down-Symbol" at the far right. Click that and select "New Tab" and name it "pitches.h". Paste the contents of the downloaded file into that window and save it along with the sketch.

You can download pitches.h here:

To install the library see:

(Copy the text in the box below and Paste it into a blank Arduino IDE window)
(Runs once and stops. Push RESET button to replay.) If using a small piezo "beeper" it is not loud; listen close.

/* YourDuinoStarter Example: Beeper Tone test
 - Uses the Arduino Tone Library to make sounds on the
   Beeper in the starter Kit, or an 8 Ohm speaker.
 - SEE the comments after "//" on each line below
 - Beeper/Speaker on Arduino port 11 (Can be changed)
 - If speaker is used, Use 100 ohm resistor 
   (or two 220 ohms in parallel) in series with speaker.
 - V1.02 02/15/13
 Questions: */

/*-----( Import needed libraries )-----*/
#include "pitches.h"

/*-----( Declare Constants and Pin Numbers )-----*/
#define TonePIN  11  // Which pin to connect to

/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
int thisNote;          // which note to play now
int noteDuration;      // How long to play the note
int pauseBetweenNotes; // Length between notes

// notes in the melody: (0 is a 'rest' )
// This is an "array" of data holding the melody
int melody[] = {

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };

void setup()   /****** SETUP: RUNS ONCE ******/
  // go through each of the notes of the melody:
  for (thisNote = 0; thisNote < 8; thisNote++)
  { // Calculate and play one note and a pause after it

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

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    pauseBetweenNotes = noteDuration * 1.30;
   }  // End of one note

}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
// Nothing. Once was enough :-)
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
// none

//*********( THE END )***********