Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* CONSTANTS */
- /* RED */
- const int ledPinR = 2;
- const int bttnPinR = 6;
- const int cR = 0;
- const int tR = 310;
- /* GREEN */
- const int ledPinG = 3;
- const int bttnPinG = 7;
- const int cG = 1;
- const int tG = 415;
- /* BLUE */
- const int ledPinB = 4;
- const int bttnPinB = 8;
- const int cB = 2;
- const int tB = 209;
- /* YELLOW */
- const int ledPinY = 5;
- const int bttnPinY = 9;
- const int cY = 3;
- const int tY = 252;
- /* TONES */
- const int lose = 5;
- const int toneOff = 6;
- /* SPEAKER */
- const int spkrPin = 11;
- /* BUTTON */
- const int no_bttn = 24;
- const int ok_bttn = 23;
- /* RNG */
- int sequence [32];
- /* SETUP */
- void setup() {
- /* Defines outputs and inputs */
- pinMode(ledPinR, OUTPUT);
- pinMode(bttnPinR, INPUT_PULLUP);
- pinMode(ledPinG, OUTPUT);
- pinMode(bttnPinG, INPUT_PULLUP);
- pinMode(ledPinB, OUTPUT);
- pinMode(bttnPinB, INPUT_PULLUP);
- pinMode(ledPinY, OUTPUT);
- pinMode(bttnPinY, INPUT_PULLUP);
- pinMode(spkrPin, OUTPUT);
- /* Makes sure all lights are initially off */
- digitalWrite(ledPinR, HIGH);
- digitalWrite(ledPinG, HIGH);
- digitalWrite(ledPinB, HIGH);
- digitalWrite(ledPinY, HIGH);
- }
- /* RNG */
- void fillSequence() {
- int index = 0;
- while (index < 32) {
- sequence[index] = random(4);
- index++;
- }
- }
- /* Initial Loop */
- void loop() {
- executeSimon();
- }
- /* Turns on LED */
- void ledOn(int color) {
- if (color == cR) {
- digitalWrite(ledPinR, LOW);
- }
- else if (color == cG) {
- digitalWrite(ledPinG, LOW);
- }
- else if (color == cB) {
- digitalWrite(ledPinB, LOW);
- }
- else if (color == cY) {
- digitalWrite(ledPinY, LOW);
- }
- }
- /* Turns off LED */
- void ledOff(int color) {
- if (color == cR) {
- digitalWrite(ledPinR, HIGH);
- }
- else if (color == cG) {
- digitalWrite(ledPinG, HIGH);
- }
- else if (color == cB) {
- digitalWrite(ledPinB, HIGH);
- }
- else if (color == cY) {
- digitalWrite(ledPinY, HIGH);
- }
- }
- /* Plays the given tone */
- void playTone(int givenTone) {
- if (givenTone == cR) {
- tone(spkrPin, 310);
- }
- else if (givenTone == cG) {
- tone(spkrPin, 415);
- }
- else if (givenTone == cB) {
- tone(spkrPin, 209);
- }
- else if (givenTone == cY) {
- tone(spkrPin, 252);
- }
- else if (givenTone == lose) {
- tone(spkrPin, 42);
- }
- else if (givenTone == toneOff) {
- noTone(spkrPin);
- }
- }
- /* Winning sequence execution */
- void onWin(int color) {
- int x = 6;
- while (x>0) {
- playTone(color);
- ledOn(color);
- delay(70);
- playTone(toneOff);
- ledOff(color);
- delay(20);
- x = x - 1;
- }
- }
- /* Losing sequence execution */
- void onLose(int color) {
- int x = 6;
- while (x>0) {
- playTone(color);
- ledOn(color);
- delay(70);
- playTone(toneOff);
- ledOff(color);
- delay(20);
- x = x - 1;
- }
- }
- /* Special Win Condition */
- void onWinExtra(int color) {
- playTone(color);
- ledOn(color);
- delay(200);
- playTone(toneOff);
- ledOff(color);
- delay(200);
- playTone(color);
- ledOn(color);
- delay(200);
- playTone(toneOff);
- ledOff(color);
- delay(200);
- playTone(color);
- ledOn(color);
- delay(200);
- playTone(toneOff);
- ledOff(color);
- delay(200);
- ledOn(cR);
- playTone(cR);
- delay(200);
- ledOff(cR);
- playTone(cG);
- ledOn(cG);
- delay(200);
- ledOff(cG);
- ledOn(cY);
- playTone(cY);
- delay(200);
- ledOff(cY);
- ledOn(cB);
- playTone(cB);
- delay(200);
- ledOff(cB);
- playTone(toneOff);
- delay(200);
- ledOn(cY);
- ledOn(cB);
- ledOn(cR);
- ledOn(cG);
- playTone(cY);
- playTone(cR);
- playTone(cG);
- playTone(cB);
- delay(200);
- playTone(toneOff);
- ledOff(cY);
- ledOff(cB);
- ledOff(cR);
- ledOff(cG);
- }
- /* Special Lose Condition */
- void onLoseExtra(int color) {
- playTone(color);
- ledOn(color);
- delay(200);
- playTone(toneOff);
- ledOff(color);
- delay(200);
- playTone(color);
- ledOn(color);
- delay(200);
- playTone(toneOff);
- ledOff(color);
- delay(200);
- playTone(color);
- ledOn(color);
- delay(200);
- playTone(toneOff);
- ledOff(color);
- delay(200);
- ledOn(cR);
- playTone(cR);
- ledOn(cY);
- playTone(cY);
- delay(200);
- ledOff(cR);
- ledOff(cY);
- ledOn(cG);
- playTone(cG);
- ledOn(cB);
- playTone(cB);
- delay(200);
- ledOff(cG);
- ledOff(cB);
- ledOn(cR);
- playTone(cR);
- ledOn(cY);
- playTone(cY);
- delay(200);
- ledOff(cR);
- ledOff(cY);
- ledOn(cG);
- playTone(cG);
- ledOn(cB);
- playTone(cB);
- delay(200);
- ledOff(cG);
- ledOff(cB);
- playTone(toneOff);
- }
- /*Returns the pressed button */
- int getButton() {
- int isPressedR = digitalRead(bttnPinR);
- int isPressedG = digitalRead(bttnPinG);
- int isPressedB = digitalRead(bttnPinB);
- int isPressedY = digitalRead(bttnPinY);
- int button = no_bttn;
- if (isPressedR == LOW) {
- button = cR;
- }
- else if (isPressedG == LOW) {
- button = cG;
- }
- else if (isPressedB == LOW) {
- button = cB;
- }
- else if (isPressedY == LOW) {
- button = cY;
- }
- return button;
- }
- /* Attracts the player / Difficulty Screen */
- int attract() {
- int button = no_bttn;
- long int oldTime = millis();
- long int newTime;
- while (button == no_bttn) {
- newTime = millis();
- if (newTime > (oldTime + 2500)) {
- ledOff(cR);
- ledOff(cB);
- ledOff(cG);
- ledOff(cY);
- oldTime = millis();
- }
- else if (newTime > (oldTime + 2000)) {
- ledOn(cG);
- }
- else if (newTime > (oldTime + 1500)) {
- ledOn(cR);
- }
- else if (newTime > (oldTime + 1000)) {
- ledOn(cB);
- }
- else if (newTime > (oldTime + 500)) {
- ledOn(cY);
- }
- button = getButton();
- }
- ledOff(cR);
- ledOff(cG);
- ledOff(cB);
- ledOff(cY);
- return button;
- }
- /* Correct Button Pressed Checker */
- int getButtonPlay(int correctColor) {
- int button = no_bttn;
- long int startTime = millis();
- long int elapsedTime = 0;
- while (elapsedTime < 3000) {
- int buttonPress = getButton();
- if (buttonPress != no_bttn) {
- if (correctColor == buttonPress) {
- ledOn(correctColor);
- playTone(correctColor);
- while (getButton() == correctColor) {
- }
- ledOff(correctColor);
- playTone(toneOff);
- button = ok_bttn;
- break;
- }
- else {
- break;
- }
- }
- elapsedTime = millis() - startTime;
- }
- return button;
- }
- /* Plays the LED that gradually speeds up */
- void ledPlay(int color, int sequence) {
- ledOn(color);
- playTone(color);
- if (sequence < 6) {
- delay(420);
- }
- else if (sequence > 5 && sequence < 14) {
- delay(320);
- }
- else if (sequence > 13) {
- delay(220);
- }
- ledOff(color);
- playTone(toneOff);
- }
- /* Plays the sequence in order */
- void playSequence(int colorsToPlay) {
- int index = 0;
- while (index <colorsToPlay) {
- ledPlay(sequence[index], colorsToPlay);
- delay(50);
- index++;
- }
- return;
- }
- /* Obtains information regarding the button press of the player */
- int getButtonSequence(int colorsToPlay) {
- int index = 0;
- while (index < colorsToPlay) {
- int button = getButtonPlay(sequence [index]);
- if (button == no_bttn) {
- break;
- }
- index = index + 1;
- }
- return index;
- }
- /* Plays the game */
- void playSimon(int size) {
- int colorsToPlay = 1;
- int lastButton;
- while (colorsToPlay <= size) {
- playSequence(colorsToPlay);
- lastButton = getButtonSequence(colorsToPlay);
- if (lastButton != colorsToPlay) {
- onLoseExtra(sequence[lastButton]);
- delay(1000);
- return;
- }
- colorsToPlay++;
- delay(800);
- }
- onWinExtra(sequence[size-1]);
- delay(1000);
- }
- /* Skill Level */
- void executeSimon() {
- int skill = attract();
- delay(1000);
- int size = (skill + 1) * 8;
- fillSequence();
- playSimon(size);
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement