/**
Simon Game for Arduino
Copyright (C) 2016, Uri Shaked
Released under the MIT License.
*/
#include "pitches.h"
/* Constants - define pin numbers for LEDs,
buttons and speaker, and also the game tones: */
const byte ledPins[] = {9, 10, 11, 12};
const byte buttonPins[] = {2, 3, 4, 5};
#define SPEAKER_PIN 8
#define MAX_GAME_LENGTH 64
const int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
/* Global variables - store the game state */
byte gameSequence[MAX_GAME_LENGTH] = {0};
byte gameIndex = 0;
/**
Set up the Arduino board and initialize Serial communication
*/
void setup() {
Serial.begin(9600);
for (byte i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
// The following line primes the random number generator.
// It assumes pin A0 is floating (disconnected):
randomSeed(analogRead(A0));
}
/**
Lights the given LED and plays a suitable tone
*/
void lightLedAndPlayTone(byte ledIndex) {
digitalWrite(ledPins[ledIndex], HIGH);
tone(SPEAKER_PIN, gameTones[ledIndex]);
delay(300);
digitalWrite(ledPins[ledIndex], LOW);
noTone(SPEAKER_PIN);
}
/**
Plays the current sequence of notes that the user has to repeat
*/
void playSequence() {
for (int i = 0; i < gameIndex; i++) {
byte currentLed = gameSequence[i];
lightLedAndPlayTone(currentLed);
delay(50);
}
}
/**
Waits until the user pressed one of the buttons,
and returns the index of that button
*/
byte readButtons() {
while (true) {
for (byte i = 0; i < 4; i++) {
byte buttonPin = buttonPins[i];
if (digitalRead(buttonPin) == LOW) {
return i;
}
}
delay(1);
}
}
/**
Play the game over sequence, and report the game score
*/
void gameOver() {
Serial.print("Game over! your score: ");
Serial.println(gameIndex - 1);
gameIndex = 0;
delay(200);
// Play a Wah-Wah-Wah-Wah sound
tone(SPEAKER_PIN, NOTE_DS5);
delay(300);
tone(SPEAKER_PIN, NOTE_D5);
delay(300);
tone(SPEAKER_PIN, NOTE_CS5);
delay(300);
for (byte i = 0; i < 10; i++) {
for (int pitch = -10; pitch <= 10; pitch++) {
tone(SPEAKER_PIN, NOTE_C5 + pitch);
delay(5);
}
}
noTone(SPEAKER_PIN);
delay(500);
}
/**
Get the user's input and compare it with the expected sequence.
*/
bool checkUserSequence() {
for (int i = 0; i < gameIndex; i++) {
byte expectedButton = gameSequence[i];
byte actualButton = readButtons();
lightLedAndPlayTone(actualButton);
if (expectedButton != actualButton) {
return false;
}
}
return true;
}
/**
Plays a hooray sound whenever the user finishes a level
*/
/**
The main game loop
*/
void loop() {
playLevelUpSound2();
delay(1000);
}
int marioNotes[] = {NOTE_E5, NOTE_E5, NOTE_E5, NOTE_C5, NOTE_E5, NOTE_G5, NOTE_G4, NOTE_C5, NOTE_G4, NOTE_E4, NOTE_A4, NOTE_B4, NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_F5, NOTE_G5, NOTE_E5, NOTE_C5, NOTE_D5, NOTE_B4,
NOTE_C5, NOTE_G4, NOTE_E4, NOTE_A4, NOTE_B4, NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_F5, NOTE_G5, NOTE_E5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_G5, NOTE_FS5, NOTE_F5, NOTE_D5, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_C4,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_G5, NOTE_FS5, NOTE_F5, NOTE_D5, NOTE_E5, NOTE_C6, NOTE_C6, NOTE_C6};
int marioPauNo[] = {100, 100, 100, 100, 280, 280, 280, 280, 280, 280, 280, 280, 100, 280, 280 - 100, 280 - 100, 280 - 100, 280, 100, 100, 280, 100, 100, 280,
280, 280, 280, 280, 280, 100, 280, 280 - 100, 280 - 100, 280 - 100, 280, 100, 100, 280, 100, 100, 280, 100, 100, 100, 280, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 280, 100, 280, 100, 280};
int marioPauPa[] = {80, 80+100, 80+100, 80, 80, 80 + 360, 80 + 360,80 + 180, 80 + 180, 80 + 180, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80 + 180, 80, 80, 80, 80 + 180,
80 + 180, 80 + 180, 80 + 180, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80 + 180, 80, 80, 80, 80 + 540, 80, 80, 80, 80, 80+180, 80, 80, 80+180,
80, 80, 80+360, 80, 80, 80, 80, 80+180, 80, 80, 80+360};
int marioPlayer[] = {1, 2, 3, 4, 1, 2, 2, 4, 2, 1, 3, 1, 1, 3, 2, 1, 2, 3, 4, 2, 1, 4, 3, 1, 2, 4, 2, 1, 3, 1, 1, 3, 2, 1, 2, 3, 4, 2, 1, 4, 3, 1, 2, 4, 4, 3, 1, 4, 3, 4, 3, 4, 3, 4, 3, 2, 4, 4, 3, 1, 4, 3, 1 };
void playLevelUpSound2() {
for(short i = 0; i < 60; i++){
tone(SPEAKER_PIN, marioNotes[i] );
delay(marioPauNo[i]);
noTone(SPEAKER_PIN); // turn off previous sound
delay(marioPauPa[i]);
}
for(short i = 0; i < 60; i++){
Serial.print(marioPlayer[i]);
}
}