Created
December 25, 2016 21:17
-
-
Save Vratislav/64574cfe2a1a5760ef31e1f8235fa661 to your computer and use it in GitHub Desktop.
Simon Says for Arduino Uno
This file contains hidden or 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
| const int START_LEVEL = 3; | |
| const int NUMBER_OF_BUTTONS = 4; | |
| const int MAX_SEQUENCE_LENGTH = 256; | |
| //INPUT PINS | |
| const int btnBluePin = 2; | |
| const int btnGreenPin = 3; | |
| const int btnRedPin = 4; | |
| const int btnYellowPin = 5; | |
| const int btnPins[] = {btnBluePin, btnGreenPin, btnRedPin, btnYellowPin}; | |
| //OUTPUT PINS | |
| const int ledBluePin = 9; | |
| const int ledGreenPin = 6; | |
| const int ledRedPin = 7; | |
| const int ledYellowPin = 10; | |
| const int ledPins[] = {ledBluePin, ledGreenPin, ledRedPin, ledYellowPin}; | |
| const int BUZZER_PIN = 8; | |
| //TONE FREQS | |
| const int BLUE_BTN_FREQ = 349; | |
| const int GREEN_BTN_FREQ = 392; | |
| const int RED_BTN_FREQ = 440; | |
| const int YELLOW_BTN_FREQ = 523; | |
| const int DEFEAT_FREQ = 200; | |
| const int TONE_FREQS[] = {BLUE_BTN_FREQ, GREEN_BTN_FREQ, RED_BTN_FREQ, YELLOW_BTN_FREQ}; | |
| //GAME STATE | |
| const int GAME_STATE_LEVEL_START = 0; | |
| const int GAME_STATE_PLAYBACK = 1; | |
| const int GAME_STATE_PLAY = 2; | |
| const int GAME_STATE_DEFEAT = 3; | |
| const int GAME_STATE_LEVEL_WON = 4; | |
| int gameState = GAME_STATE_LEVEL_START; | |
| int level = START_LEVEL; | |
| int cursor = 0; | |
| int playbackCursor = 0; | |
| int sequence[MAX_SEQUENCE_LENGTH]; | |
| /* | |
| * ---------------------- | |
| * Convenience functions | |
| * ---------------------- | |
| */ | |
| int btnPinToIndex(int btnPin){ | |
| if(btnPin == btnBluePin){ | |
| return 0; | |
| }else if(btnPin == btnGreenPin){ | |
| return 1; | |
| }else if(btnPin == btnRedPin){ | |
| return 2; | |
| }else if(btnPin == btnYellowPin){ | |
| return 3; | |
| } | |
| } | |
| void playTone(int index){ | |
| digitalWrite(ledPins[index],HIGH); | |
| tone(BUZZER_PIN, TONE_FREQS[index]); | |
| delay(500); | |
| noTone(BUZZER_PIN); | |
| digitalWrite(ledPins[index], LOW); | |
| } | |
| bool playback(int cursorPos, int level){ | |
| if(cursorPos < level){ | |
| playTone(sequence[cursorPos]); | |
| delay(200); | |
| return false; | |
| }else{ | |
| return true; | |
| } | |
| } | |
| void generateSequence(int cursor,int currentLevel){ | |
| for(int i = cursor; i <= min(currentLevel,MAX_SEQUENCE_LENGTH-1); i++){ | |
| const int randNote = random(0, NUMBER_OF_BUTTONS); | |
| sequence[i] = randNote; | |
| Serial.println("Generated note:"); | |
| Serial.println(randNote, DEC); | |
| } | |
| } | |
| int checkAndPlayPlayerInput(int cursorPos){ | |
| for(int i = 0; i < NUMBER_OF_BUTTONS; i++){ | |
| if(digitalRead(btnPins[i]) == HIGH){ | |
| playTone(i); | |
| delay(200); | |
| if(sequence[cursorPos] == i){ | |
| return 1; | |
| }else{ | |
| return -1; | |
| } | |
| } | |
| } | |
| return 0; | |
| } | |
| void doDefeatAudioViz(){ | |
| digitalWrite(ledBluePin,HIGH); | |
| digitalWrite(ledGreenPin,HIGH); | |
| digitalWrite(ledRedPin,HIGH); | |
| digitalWrite(ledYellowPin,HIGH); | |
| tone(BUZZER_PIN, 146); | |
| delay(450); | |
| tone(BUZZER_PIN, 110); | |
| delay(450); | |
| digitalWrite(ledBluePin,LOW); | |
| digitalWrite(ledGreenPin,LOW); | |
| digitalWrite(ledRedPin,LOW); | |
| digitalWrite(ledYellowPin,LOW); | |
| noTone(BUZZER_PIN); | |
| delay(1500); | |
| } | |
| void setup() { | |
| // put your setup code here, to run once: | |
| randomSeed(analogRead(4)); | |
| Serial.begin(9600); | |
| pinMode(btnBluePin, INPUT); | |
| pinMode(btnGreenPin, INPUT); | |
| pinMode(btnRedPin, INPUT); | |
| pinMode(btnYellowPin, INPUT); | |
| pinMode(ledBluePin, OUTPUT); | |
| pinMode(ledGreenPin, OUTPUT); | |
| pinMode(ledRedPin, OUTPUT); | |
| pinMode(ledYellowPin, OUTPUT); | |
| pinMode(BUZZER_PIN, OUTPUT); | |
| } | |
| void loop() { | |
| // put your main code here, to run repeatedly: | |
| if(gameState == GAME_STATE_LEVEL_START){ | |
| generateSequence(cursor,level); | |
| gameState = GAME_STATE_PLAYBACK; | |
| cursor = 0; | |
| }else if(gameState == GAME_STATE_PLAYBACK){ | |
| //cursor = cursor - 1; | |
| bool playbackFinished = playback(playbackCursor, level); | |
| playbackCursor++; | |
| if(playbackFinished){ | |
| gameState = GAME_STATE_PLAY; | |
| } | |
| }else if(gameState == GAME_STATE_PLAY){ | |
| int result = checkAndPlayPlayerInput(cursor); | |
| if(result == 1){ | |
| cursor++; | |
| //Check if this is end of the level | |
| if(cursor >= level){ | |
| level++; | |
| gameState = GAME_STATE_LEVEL_START; | |
| cursor = level; | |
| playbackCursor = level-1; | |
| delay(1000); | |
| } | |
| }else if(result == -1){ | |
| gameState = GAME_STATE_DEFEAT; | |
| } | |
| }else if(gameState == GAME_STATE_DEFEAT){ | |
| doDefeatAudioViz(); | |
| level = START_LEVEL; | |
| cursor = 0; | |
| playbackCursor = 0; | |
| gameState = GAME_STATE_LEVEL_START; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The way the board is connected:
( ping me if you want exact schematic)