mirror of https://github.com/nealey/Simon-Says
Add more music, better sound
This commit is contained in:
parent
ff37da619a
commit
1bbcd81870
|
@ -75,34 +75,12 @@ void setup()
|
||||||
pinMode(BUZZER1, OUTPUT);
|
pinMode(BUZZER1, OUTPUT);
|
||||||
pinMode(BUZZER2, OUTPUT);
|
pinMode(BUZZER2, OUTPUT);
|
||||||
|
|
||||||
//Mode checking
|
play_greeting(); // After setup is complete, say hello to the world
|
||||||
gameMode = MODE_MEMORY; // By default, we're going to play the memory game
|
|
||||||
|
|
||||||
// Check to see if the lower right button is pressed
|
|
||||||
if (checkButton() == CHOICE_YELLOW) play_beegees();
|
|
||||||
|
|
||||||
// Check to see if upper right button is pressed
|
|
||||||
if (checkButton() == CHOICE_GREEN)
|
|
||||||
{
|
|
||||||
gameMode = MODE_BATTLE; //Put game into battle mode
|
|
||||||
|
|
||||||
//Turn on the upper right (green) LED
|
|
||||||
setLEDs(CHOICE_GREEN);
|
|
||||||
toner(CHOICE_GREEN, 150);
|
|
||||||
|
|
||||||
setLEDs(CHOICE_RED | CHOICE_BLUE | CHOICE_YELLOW); // Turn on the other LEDs until you release button
|
|
||||||
|
|
||||||
while(checkButton() != CHOICE_NONE) ; // Wait for user to stop pressing button
|
|
||||||
|
|
||||||
//Now do nothing. Battle mode will be serviced in the main routine
|
|
||||||
}
|
|
||||||
|
|
||||||
play_winner(); // After setup is complete, say hello to the world
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
attractMode(); // Blink lights while waiting for user to press a button
|
byte button = attractMode(); // Blink lights while waiting for user to press a button
|
||||||
|
|
||||||
// Indicate the start of game play
|
// Indicate the start of game play
|
||||||
setLEDs(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE | CHOICE_YELLOW); // Turn all LEDs on
|
setLEDs(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE | CHOICE_YELLOW); // Turn all LEDs on
|
||||||
|
@ -110,20 +88,28 @@ void loop()
|
||||||
setLEDs(CHOICE_OFF); // Turn off LEDs
|
setLEDs(CHOICE_OFF); // Turn off LEDs
|
||||||
delay(250);
|
delay(250);
|
||||||
|
|
||||||
if (gameMode == MODE_MEMORY)
|
switch (button) {
|
||||||
{
|
case CHOICE_RED:
|
||||||
// Play memory game and handle result
|
// Play memory game and handle result
|
||||||
if (play_memory() == true)
|
if (play_memory() == true)
|
||||||
play_winner(); // Player won, play winner tones
|
play_march(); // Player won, play winner tones
|
||||||
else
|
else
|
||||||
play_loser(); // Player lost, play loser tones
|
play_loser(); // Player lost, play loser tones
|
||||||
}
|
break;
|
||||||
|
|
||||||
if (gameMode == MODE_BATTLE)
|
case CHOICE_GREEN:
|
||||||
{
|
|
||||||
play_battle(); // Play game until someone loses
|
play_battle(); // Play game until someone loses
|
||||||
|
|
||||||
play_loser(); // Player lost, play loser tones
|
play_loser(); // Player lost, play loser tones
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CHOICE_BLUE:
|
||||||
|
play_getlucky();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CHOICE_YELLOW:
|
||||||
|
play_beegees();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -273,12 +259,14 @@ byte wait_for_button(void)
|
||||||
// Returns a '1' bit in the position corresponding to CHOICE_RED, CHOICE_GREEN, etc.
|
// Returns a '1' bit in the position corresponding to CHOICE_RED, CHOICE_GREEN, etc.
|
||||||
byte checkButton(void)
|
byte checkButton(void)
|
||||||
{
|
{
|
||||||
if (digitalRead(BUTTON_RED) == 0) return(CHOICE_RED);
|
byte choice = 0;
|
||||||
else if (digitalRead(BUTTON_GREEN) == 0) return(CHOICE_GREEN);
|
|
||||||
else if (digitalRead(BUTTON_BLUE) == 0) return(CHOICE_BLUE);
|
|
||||||
else if (digitalRead(BUTTON_YELLOW) == 0) return(CHOICE_YELLOW);
|
|
||||||
|
|
||||||
return(CHOICE_NONE); // If no button is pressed, return none
|
if (digitalRead(BUTTON_RED) == 0) choice |= CHOICE_RED;
|
||||||
|
else if (digitalRead(BUTTON_GREEN) == 0) choice |= CHOICE_GREEN;
|
||||||
|
else if (digitalRead(BUTTON_BLUE) == 0) choice |= CHOICE_BLUE;
|
||||||
|
else if (digitalRead(BUTTON_YELLOW) == 0) choice |= CHOICE_YELLOW;
|
||||||
|
|
||||||
|
return choice;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Light an LED and play tone
|
// Light an LED and play tone
|
||||||
|
@ -294,16 +282,16 @@ void toner(byte which, int buzz_length_ms)
|
||||||
switch(which)
|
switch(which)
|
||||||
{
|
{
|
||||||
case CHOICE_RED:
|
case CHOICE_RED:
|
||||||
buzz_sound(buzz_length_ms, 1136);
|
playNote(4, 0, buzz_length_ms);
|
||||||
break;
|
break;
|
||||||
case CHOICE_GREEN:
|
case CHOICE_GREEN:
|
||||||
buzz_sound(buzz_length_ms, 568);
|
playNote(4, 1, buzz_length_ms);
|
||||||
break;
|
break;
|
||||||
case CHOICE_BLUE:
|
case CHOICE_BLUE:
|
||||||
buzz_sound(buzz_length_ms, 851);
|
playNote(4, 3, buzz_length_ms);
|
||||||
break;
|
break;
|
||||||
case CHOICE_YELLOW:
|
case CHOICE_YELLOW:
|
||||||
buzz_sound(buzz_length_ms, 638);
|
playNote(4, 7, buzz_length_ms);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -311,43 +299,28 @@ void toner(byte which, int buzz_length_ms)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle buzzer every buzz_delay_us, for a duration of buzz_length_ms.
|
// Toggle buzzer every buzz_delay_us, for a duration of buzz_length_ms.
|
||||||
void buzz_sound(int buzz_length_ms, int buzz_delay_us)
|
void buzz_sound(int buzz_length_ms, unsigned int freq)
|
||||||
{
|
{
|
||||||
// Convert total play time from milliseconds to microseconds
|
tone(BUZZER2, freq, buzz_length_ms);
|
||||||
long buzz_length_us = buzz_length_ms * (long)1000;
|
delay(buzz_length_ms);
|
||||||
|
|
||||||
// Loop until the remaining play time is less than a single buzz_delay_us
|
|
||||||
while (buzz_length_us > (buzz_delay_us * 2))
|
|
||||||
{
|
|
||||||
buzz_length_us -= buzz_delay_us * 2; //Decrease the remaining play time
|
|
||||||
|
|
||||||
// Toggle the buzzer at various speeds
|
|
||||||
digitalWrite(BUZZER1, LOW);
|
|
||||||
digitalWrite(BUZZER2, HIGH);
|
|
||||||
delayMicroseconds(buzz_delay_us);
|
|
||||||
|
|
||||||
digitalWrite(BUZZER1, HIGH);
|
|
||||||
digitalWrite(BUZZER2, LOW);
|
|
||||||
delayMicroseconds(buzz_delay_us);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Play the winner sound and lights
|
// Play the winner sound and lights
|
||||||
void play_winner(void)
|
void play_greeting(void)
|
||||||
{
|
{
|
||||||
setLEDs(CHOICE_GREEN | CHOICE_BLUE);
|
setLEDs(CHOICE_GREEN | CHOICE_BLUE);
|
||||||
winner_sound();
|
greeting_sound();
|
||||||
setLEDs(CHOICE_RED | CHOICE_YELLOW);
|
setLEDs(CHOICE_RED | CHOICE_YELLOW);
|
||||||
winner_sound();
|
greeting_sound();
|
||||||
setLEDs(CHOICE_GREEN | CHOICE_BLUE);
|
setLEDs(CHOICE_GREEN | CHOICE_BLUE);
|
||||||
winner_sound();
|
greeting_sound();
|
||||||
setLEDs(CHOICE_RED | CHOICE_YELLOW);
|
setLEDs(CHOICE_RED | CHOICE_YELLOW);
|
||||||
winner_sound();
|
greeting_sound();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Play the winner sound
|
// Play the winner sound
|
||||||
// This is just a unique (annoying) sound we came up with, there is no magic to it
|
// This is just a unique (annoying) sound we came up with, there is no magic to it
|
||||||
void winner_sound(void)
|
void greeting_sound(void)
|
||||||
{
|
{
|
||||||
// Toggle the buzzer at various speeds
|
// Toggle the buzzer at various speeds
|
||||||
for (byte x = 250 ; x > 70 ; x--)
|
for (byte x = 250 ; x > 70 ; x--)
|
||||||
|
@ -355,11 +328,9 @@ void winner_sound(void)
|
||||||
for (byte y = 0 ; y < 3 ; y++)
|
for (byte y = 0 ; y < 3 ; y++)
|
||||||
{
|
{
|
||||||
digitalWrite(BUZZER2, HIGH);
|
digitalWrite(BUZZER2, HIGH);
|
||||||
digitalWrite(BUZZER1, LOW);
|
|
||||||
delayMicroseconds(x);
|
delayMicroseconds(x);
|
||||||
|
|
||||||
digitalWrite(BUZZER2, LOW);
|
digitalWrite(BUZZER2, LOW);
|
||||||
digitalWrite(BUZZER1, HIGH);
|
|
||||||
delayMicroseconds(x);
|
delayMicroseconds(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -369,96 +340,246 @@ void winner_sound(void)
|
||||||
void play_loser(void)
|
void play_loser(void)
|
||||||
{
|
{
|
||||||
setLEDs(CHOICE_RED | CHOICE_GREEN);
|
setLEDs(CHOICE_RED | CHOICE_GREEN);
|
||||||
buzz_sound(255, 1500);
|
buzz_sound(255, 220);
|
||||||
|
|
||||||
setLEDs(CHOICE_BLUE | CHOICE_YELLOW);
|
setLEDs(CHOICE_BLUE | CHOICE_YELLOW);
|
||||||
buzz_sound(255, 1500);
|
buzz_sound(255, 220);
|
||||||
|
|
||||||
setLEDs(CHOICE_RED | CHOICE_GREEN);
|
setLEDs(CHOICE_RED | CHOICE_GREEN);
|
||||||
buzz_sound(255, 1500);
|
buzz_sound(255, 220);
|
||||||
|
|
||||||
setLEDs(CHOICE_BLUE | CHOICE_YELLOW);
|
setLEDs(CHOICE_BLUE | CHOICE_YELLOW);
|
||||||
buzz_sound(255, 1500);
|
buzz_sound(255, 220);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show an "attract mode" display while waiting for user to press button.
|
// Show an "attract mode" display while waiting for user to press button.
|
||||||
void attractMode(void)
|
byte attractMode(void)
|
||||||
{
|
{
|
||||||
while(1)
|
byte led = 0;
|
||||||
{
|
byte button = 0;
|
||||||
setLEDs(CHOICE_RED);
|
|
||||||
delay(100);
|
|
||||||
if (checkButton() != CHOICE_NONE) return;
|
|
||||||
|
|
||||||
setLEDs(CHOICE_BLUE);
|
for (; button == 0;) {
|
||||||
|
setLEDs(1 << led);
|
||||||
delay(100);
|
delay(100);
|
||||||
if (checkButton() != CHOICE_NONE) return;
|
button = checkButton();
|
||||||
|
led = (led + 1) % 4;
|
||||||
setLEDs(CHOICE_GREEN);
|
|
||||||
delay(100);
|
|
||||||
if (checkButton() != CHOICE_NONE) return;
|
|
||||||
|
|
||||||
setLEDs(CHOICE_YELLOW);
|
|
||||||
delay(100);
|
|
||||||
if (checkButton() != CHOICE_NONE) return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
||||||
// 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.
|
const unsigned int freqs[] = {
|
||||||
int melody[] = {
|
3520, // A
|
||||||
NOTE_G4, NOTE_A4, 0, NOTE_C5, 0, 0, NOTE_G4, 0, 0, 0,
|
3729, // A#
|
||||||
NOTE_E4, 0, NOTE_D4, NOTE_E4, NOTE_G4, 0,
|
3951, // B
|
||||||
NOTE_D4, NOTE_E4, 0, NOTE_G4, 0, 0,
|
2093, // C
|
||||||
NOTE_D4, 0, NOTE_E4, 0, NOTE_G4, 0, NOTE_A4, 0, NOTE_C5, 0};
|
2217, // C#
|
||||||
|
2349, // D
|
||||||
|
2489, // D#
|
||||||
|
2637, // E
|
||||||
|
2794, // F
|
||||||
|
2960, // F#
|
||||||
|
3136, // G
|
||||||
|
3322, // G#
|
||||||
|
};
|
||||||
|
|
||||||
int noteDuration = 115; // This essentially sets the tempo, 115 is just about right for a disco groove :)
|
const int scale[] = {
|
||||||
int LEDnumber = 0; // Keeps track of which LED we are on during the beegees loop
|
0, 2, 3, 5, 7, 8, 10
|
||||||
|
};
|
||||||
|
|
||||||
|
void
|
||||||
|
playNote(int octave, int note, int duration)
|
||||||
|
{
|
||||||
|
unsigned int freq;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (note >= 0) {
|
||||||
|
freq = freqs[note];
|
||||||
|
for (i = octave; i < 7; i += 1) {
|
||||||
|
freq /= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
tone(BUZZER2, freq, duration);
|
||||||
|
setLEDs(note + 1);
|
||||||
|
}
|
||||||
|
delay(duration);
|
||||||
|
setLEDs(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
play(int bpm, char *tune)
|
||||||
|
{
|
||||||
|
unsigned int baseDuration = 60000 / bpm;
|
||||||
|
int duration = baseDuration;
|
||||||
|
int baseOctave = 4;
|
||||||
|
int octave = baseOctave;
|
||||||
|
|
||||||
|
int note = -2;
|
||||||
|
|
||||||
|
char *p = tune;
|
||||||
|
|
||||||
|
digitalWrite(BUZZER1, LOW); // setup the "BUZZER1" side of the buzzer to stay low, while we play the tone on the other pin.
|
||||||
|
for (; *p; p += 1) {
|
||||||
|
boolean playNow = false;
|
||||||
|
|
||||||
|
switch (*p) {
|
||||||
|
case '>':
|
||||||
|
octave = baseOctave + 1;
|
||||||
|
break;
|
||||||
|
case '<':
|
||||||
|
octave = baseOctave - 1;
|
||||||
|
break;
|
||||||
|
case 'A':
|
||||||
|
case 'B':
|
||||||
|
case 'C':
|
||||||
|
case 'D':
|
||||||
|
case 'E':
|
||||||
|
case 'F':
|
||||||
|
case 'G':
|
||||||
|
playNow = true;
|
||||||
|
note = scale[*p - 'A'];
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
case 'b':
|
||||||
|
case 'c':
|
||||||
|
case 'd':
|
||||||
|
case 'e':
|
||||||
|
case 'f':
|
||||||
|
case 'g':
|
||||||
|
playNow = true;
|
||||||
|
octave += 1;
|
||||||
|
note = scale[*p - 'a'];
|
||||||
|
break;
|
||||||
|
case '_':
|
||||||
|
playNow = true;
|
||||||
|
note = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for sharps or flats
|
||||||
|
switch (*(p+1)) {
|
||||||
|
case '#':
|
||||||
|
case '+':
|
||||||
|
note += 1;
|
||||||
|
if (note == 12) {
|
||||||
|
octave += 1;
|
||||||
|
note = 0;
|
||||||
|
}
|
||||||
|
p += 1;
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
note -= 1;
|
||||||
|
if (note == -1) {
|
||||||
|
octave -= 1;
|
||||||
|
note = 11;
|
||||||
|
}
|
||||||
|
p += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for octave
|
||||||
|
switch (*(p+1)) {
|
||||||
|
case ',':
|
||||||
|
octave -= 1;
|
||||||
|
p += 1;
|
||||||
|
break;
|
||||||
|
case '\'':
|
||||||
|
octave += 1;
|
||||||
|
p += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for duration
|
||||||
|
switch (*(p+1)) {
|
||||||
|
case '2':
|
||||||
|
duration *= 2;
|
||||||
|
p += 1;
|
||||||
|
break;
|
||||||
|
case '4':
|
||||||
|
duration *= 4;
|
||||||
|
p += 1;
|
||||||
|
break;
|
||||||
|
case '.':
|
||||||
|
duration += duration / 2;
|
||||||
|
p += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (playNow) {
|
||||||
|
playNote(octave, note, duration);
|
||||||
|
note = -1;
|
||||||
|
octave = baseOctave;
|
||||||
|
duration = baseDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkButton()) {
|
||||||
|
noTone(BUZZER2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (note >= 0) {
|
||||||
|
playNote(octave, note, duration);
|
||||||
|
}
|
||||||
|
noTone(BUZZER2);
|
||||||
|
}
|
||||||
|
|
||||||
// Do nothing but play bad beegees music
|
// Do nothing but play bad beegees music
|
||||||
// This function is activated when user holds bottom right button during power up
|
// This function is activated when user holds bottom right button during power up
|
||||||
void play_beegees()
|
void play_beegees()
|
||||||
{
|
{
|
||||||
//Turn on the bottom right (yellow) LED
|
while(checkButton() == CHOICE_NONE) {
|
||||||
setLEDs(CHOICE_YELLOW);
|
play(104 * 4, "E-F_a-__E-2__C_B-,CE-_B-,C_E-__B-,_C_E-_F_a-_");
|
||||||
toner(CHOICE_YELLOW, 150);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setLEDs(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE); // Turn on the other LEDs until you release button
|
void lplay(unsigned int tempo, char *tune, int count)
|
||||||
|
|
||||||
while(checkButton() != CHOICE_NONE) ; // Wait for user to stop pressing button
|
|
||||||
|
|
||||||
setLEDs(CHOICE_NONE); // Turn off LEDs
|
|
||||||
|
|
||||||
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
|
|
||||||
{
|
{
|
||||||
// iterate over the notes of the melody:
|
int i;
|
||||||
for (int thisNote = 0; thisNote < 32; thisNote++) {
|
|
||||||
changeLED();
|
for (i = 0; i < count; i += 1) {
|
||||||
tone(BUZZER2, melody[thisNote],noteDuration);
|
play(tempo * 3, tune);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Each time this function is called the board moves to the next LED
|
|
||||||
void changeLED(void)
|
void play_march()
|
||||||
{
|
{
|
||||||
setLEDs(1 << LEDnumber); // Change the LED
|
const int tempo = 80;
|
||||||
|
|
||||||
LEDnumber++; // Goto the next LED
|
lplay(tempo * 4, "GB-,D", 6);
|
||||||
if(LEDnumber > 3) LEDnumber = 0; // Wrap the counter if needed
|
lplay(tempo * 4, "GB-,D", 6);
|
||||||
|
lplay(tempo * 4, "GB-,D", 6);
|
||||||
|
lplay(tempo * 4, "E-G-,B-,", 5);
|
||||||
|
play(tempo * 4, "B-");
|
||||||
|
lplay(tempo * 4, "GB-,D", 6);
|
||||||
|
lplay(tempo * 4, "E-G-,B-,", 5);
|
||||||
|
play(tempo * 4, "B-");
|
||||||
|
lplay(tempo * 4, "GB-,D", 6);
|
||||||
|
lplay(tempo * 4, "GB-,D", 6);
|
||||||
|
|
||||||
|
lplay(tempo * 4, "dGB-", 6);
|
||||||
|
lplay(tempo * 4, "dGB-", 6);
|
||||||
|
lplay(tempo * 4, "dGB-", 6);
|
||||||
|
lplay(tempo * 4, "e-G-B-", 5);
|
||||||
|
play(tempo * 4, "B-");
|
||||||
|
lplay(tempo * 4, "G-B-,F", 6);
|
||||||
|
lplay(tempo * 4, "E-G-,B-,", 5);
|
||||||
|
play(tempo * 4, "B-");
|
||||||
|
lplay(tempo * 4, "GB-,D", 6);
|
||||||
|
lplay(tempo * 4, "GB-,D", 6);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void play_getlucky()
|
||||||
|
{
|
||||||
|
while (checkButton() == CHOICE_NONE) {
|
||||||
|
play(116 * 4, ("_______B__B_A_F+_"
|
||||||
|
"_______d__d_c+_A_"
|
||||||
|
"_______f+__f+_e_c+_"
|
||||||
|
"_______e__e_d_B_"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue