Utilized Tone() for BEEGEES Melody

I utilized the built-in Arduino Tone() function and "pitches.h" header
to recreate the BeeGees melody. It looks a lot cleaner in the code, and
it's easier to figure out the melody (which is also now in a nice
looking array). The only down side is that the volume is about half. I
would love to hack the arduino Tone() function to be able to play the
tone on both sides of the buzzer. But for now, I think the cleaner code
is more important.
This commit is contained in:
Pete Lewis 2013-02-18 14:45:24 -07:00
parent 47361618c5
commit 3fb180c8ef
3 changed files with 120 additions and 110 deletions

View File

@ -42,6 +42,7 @@
*/
#include "hardware_versions.h"
#include "pitches.h" // Used for the MODE_BEEGEES, for playing the melody on the buzzer!
// Define game parameters
#define ROUNDS_TO_WIN 13 //Number of rounds to succesfully remember before you win. 13 is do-able.
@ -404,10 +405,17 @@ void attractMode(void)
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//The following functions are related to Beegees Easter Egg only
// The following functions are related to Beegees Easter Egg only
// Notes in the melody. Each note is about an 1/8th note, "0"s are rests.
int melody[] = {
NOTE_G4, NOTE_A4, 0, NOTE_C5, 0, 0, NOTE_G4, 0, 0, 0,
NOTE_E4, 0, NOTE_D4, NOTE_E4, NOTE_G4, 0,
NOTE_D4, NOTE_E4, 0, NOTE_G4, 0, 0,
NOTE_D4, 0, NOTE_E4, 0, NOTE_G4, 0, NOTE_A4, 0, NOTE_C5, 0};
int noteDuration = 115; // This essentially sets the tempo, 115 is just about right for a disco groove :)
int LEDnumber = 0; // Keeps track of which LED we are on during the beegees loop
int count = 20; // for keeping rhythm straight in the beegees loop
// Do nothing but play bad beegees music
// This function is activated when user holds bottom right button during power up
@ -425,117 +433,24 @@ void play_beegees()
delay(1000); // Wait a second before playing song
digitalWrite(BUZZER1, LOW); // setup the "BUZZER1" side of the buzzer to stay low, while we play the tone on the other pin.
while(checkButton() == CHOICE_NONE) //Play song until you press a button
{
buzz(3);
delay(400);
buzz(4);
rest(1);
delay(600);
buzz(5);
rest(1);
rest(1);
delay(400);
buzz(3);
rest(1);
rest(1);
rest(1);
buzz(2);
rest(1);
buzz(1);
buzz(2);
buzz(3);
rest(1);
buzz(1);
buzz(2);
rest(1);
buzz(3);
rest(1);
rest(1);
buzz(1);
rest(1);
buzz(2);
rest(1);
buzz(3);
rest(1);
buzz(4);
rest(1);
buzz(5);
rest(1);
delay(700);
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 32; thisNote++) {
changeLED();
tone(BUZZER2, 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(BUZZER2);
}
}
}
//
void buzz(int tone){
//Declare an integer, "freq", for frequency of the note to be played.
int freq;
//5 different tones to select. Each tone is a different frequency.
if(tone == 1) freq = 2000;
if(tone == 2) freq = 1800;
if(tone == 3) freq = 1500;
if(tone == 4) freq = 1350;
if(tone == 5) freq = 1110;
//freq = (freq/2);
// Because frequency is determined by the wavelength (the time HIGH and the time LOW),
// you need to have "count" in order to keep a note the same length in time.
// "count" is the number of times this function will repeat the HIGH/LOW pattern - to create the sound of the note.
count = 40;
// In order to keep all 5 notes the same length in time, you must compare them to the longest note (tonic) - aka the "1" note.
count = count * (2000/freq);
// Change to the next LED
changeLED();
// this next for loop actually makes the buzzer pin move.
// it uses the "count" variable to know how many times to play the frequency.
// -this keeps the timing correct.
for(int i = 0 ; i < count ; i++)
{
digitalWrite(BUZZER1, HIGH);
digitalWrite(BUZZER2, LOW);
delayMicroseconds(freq);
digitalWrite(BUZZER1, LOW);
digitalWrite(BUZZER2, HIGH);
delayMicroseconds(freq);
}
delay(60);
}
//
void rest(int tone){
int freq;
if(tone == 1) freq = 2000;
if(tone == 2) freq = 1800;
if(tone == 3) freq = 1500;
if(tone == 4) freq = 1350;
if(tone == 5) freq = 1110;
//freq = (freq/2);
count = 40;
count = count * (2000/freq);
//change_led();
for(int i = 0 ; i < count ; i++)
{
digitalWrite(BUZZER1, LOW);
delayMicroseconds(freq);
digitalWrite(BUZZER1, LOW);
delayMicroseconds(freq);
}
delay(75);
}
// Each time this function is called the board moves to the next LED
void changeLED(void)
{

View File

@ -15,8 +15,8 @@
// Uncomment one of the following, corresponding to the board you have.
//#define BOARD_REV_2_3_2011 //Works with PTH board label '2-3-2011'
#define BOARD_REV_4_9_2009 //Works with SMD simoon board label '4/9/2009' and '9/7/11'
#define BOARD_REV_2_3_2011 //Works with PTH board label '2-3-2011'
//#define BOARD_REV_4_9_2009 //Works with SMD simoon board label '4/9/2009' and '9/7/11'
//#define BOARD_REV_6_25_2008
#ifdef BOARD_REV_2_3_2011

View File

@ -0,0 +1,95 @@
/*************************************************
* Public Constants
*************************************************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978