Last active
March 1, 2018 15:11
-
-
Save jacobjoaquin/5b02d6437a96c18c6412528c9cf2731d to your computer and use it in GitHub Desktop.
Simone Says
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
"Simone Says" | |
Coded by Jacob Joaquin | |
Build Diagram Here: | |
https://www.tinkercad.com/things/kgfQCV9AwxG | |
Requires Bounce2 Library: | |
1. Go to: Sketch->Include Library->Manage Libraries... | |
2. Search for "bounce2" | |
3. Select Bounce2 and click Install | |
*/ | |
#include <Bounce2.h> | |
// Constants | |
const int NO_BUTTON = -1; // If no button is being pressed | |
const int nButtons = 4; | |
// Bounce | |
Bounce * buttons = new Bounce[nButtons]; | |
// Pins | |
const int ledPins[] = {9, 10, 11, 12}; | |
const int buttonPins[] = {4, 5, 6, 7}; | |
const int buzzerPin = 8; | |
// Frequencies of notes | |
const unsigned int frequencies[] = {329, 440, 523, 660}; | |
const unsigned int noteWrong = 100; | |
// Note player buffer | |
int playedNotes[128] = {0}; | |
int notesIndex = 0; | |
// Game Modes | |
enum { | |
PLAYBACK, | |
USER, | |
GAMEOVER | |
}; | |
int mode = PLAYBACK; | |
void setup() { | |
pinMode(buttonPins[0], INPUT); | |
pinMode(buttonPins[1], INPUT); | |
pinMode(buttonPins[2], INPUT); | |
pinMode(buttonPins[3], INPUT); | |
pinMode(ledPins[0], OUTPUT); | |
pinMode(ledPins[1], OUTPUT); | |
pinMode(ledPins[2], OUTPUT); | |
pinMode(ledPins[3], OUTPUT); | |
pinMode(13, OUTPUT); | |
randomSeed(analogRead(0)); | |
for (int i = 0; i < nButtons; i++) { | |
buttons[i].attach( buttonPins[i] , INPUT_PULLUP); | |
buttons[i].interval(25); | |
} | |
resetGame(); | |
} | |
void loop() { | |
if (mode == PLAYBACK) { | |
doPlayback(); | |
} else if (mode == USER) { | |
doUser(); | |
} else if (mode == GAMEOVER) { | |
doGameOver(); | |
} | |
} | |
// Reset the game back to initial conditions | |
void resetGame() { | |
mode = PLAYBACK; | |
notesIndex = 0; | |
} | |
// Adds a randomly chosen note to playedNotes[] | |
void newNote() { | |
int note = random(4); | |
playedNotes[notesIndex] = note; | |
notesIndex++; | |
} | |
// Plays the sequence of notes in playedNotes[] | |
void playNotes() { | |
unsigned long duration = 250; | |
for (int i = 0; i < notesIndex; i++) { | |
int note = playedNotes[i]; | |
digitalWrite(ledPins[note], HIGH); | |
tone(buzzerPin, frequencies[note], duration); | |
delay(duration); | |
digitalWrite(ledPins[note], LOW); | |
delay(duration); | |
} | |
} | |
// Return the current button that is being pressed | |
int getButtonPressed() { | |
for (int i = 0; i < nButtons; i++) { | |
buttons[i].update(); | |
if (buttons[i].rose()) { | |
return i; | |
} | |
} | |
return NO_BUTTON; | |
} | |
// Do Playback | |
void doPlayback() { | |
newNote(); | |
playNotes(); | |
mode = USER; | |
} | |
void doUser() { | |
int pressedIndex = 0; | |
while (pressedIndex < notesIndex && mode != GAMEOVER) { | |
int buttonPressed = NO_BUTTON; | |
int currentNote = playedNotes[pressedIndex]; | |
// Wait for button press | |
do { | |
buttonPressed = getButtonPressed(); | |
} while (buttonPressed == NO_BUTTON); | |
// If correct button, play note and update | |
if (buttonPressed == currentNote) { | |
// Play Note while being pressed | |
unsigned int note = frequencies[buttonPressed]; | |
tone(buzzerPin, note); | |
digitalWrite(ledPins[buttonPressed], HIGH); | |
// Wait for release | |
do { | |
buttons[buttonPressed].update(); | |
} while (!buttons[buttonPressed].fell()); | |
noTone(buzzerPin); | |
pressedIndex++; | |
mode = PLAYBACK; | |
digitalWrite(ledPins[buttonPressed], LOW); | |
delay(20); | |
} | |
// If incorrect button, game over. | |
else { | |
mode = GAMEOVER; | |
} | |
} | |
// Pause if in PLAYBACK mode | |
if (mode == PLAYBACK) { | |
delay(500); | |
} | |
} | |
// Game over, man. Game over. | |
void doGameOver() { | |
tone(buzzerPin, noteWrong, 1000); | |
for (int i = 0; i < nButtons; i++) { | |
digitalWrite(ledPins[i], HIGH); | |
} | |
delay(1000); | |
for (int i = 0; i < nButtons; i++) { | |
digitalWrite(ledPins[i], LOW); | |
} | |
delay(3000); | |
resetGame(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment