הקוד שלו משתמש גם בקובץ הצלילים לרמקול הפסיבי, ולכן ניתן להוריד קובץ ZIP שכולל גם את הקוד וגם את קובץ העזר של הצלילים
הפרויקט משלב קליטה של לחיצות כפתור תוך הקפדה על זיהוי לחיצה בודדת, יצירת נתונים אקראיים ושמירתם, שימוש ברמקול פסיבי להפקת צלילים מגוונים וכמובן עבודה עם מערכים ולולאות
#include "pitches.h"
#define BlueLedPin 6
#define RedLedPin 11
#define GreenLedPin 10
#define YellowLedPin 9
#define BlueBtnPin 3
#define RedBtnPin 12
#define YellowBtnPin 2
#define GreenBtnPin 4
#define buzzer A0
#define BTN_NUM 4
int btns[BTN_NUM] = {BlueBtnPin, RedBtnPin, YellowBtnPin, GreenBtnPin};
int leds[BTN_NUM] = {BlueLedPin, RedLedPin, YellowLedPin, GreenLedPin};
const int lastLed = 8;
int ledNumber = 1;
int sequence[lastLed] = {1, 2, 3, 4};
int userSequence[lastLed];
int gameStep = 0;
int flag;
boolean gameIsOn = false;
int lightSpeed = 500;
void setup() {
Serial.begin(9600);
for (int k = 0 ; k < BTN_NUM ; k++) {
pinMode(btns[k], INPUT_PULLUP);
pinMode(leds[k], OUTPUT);
digitalWrite(leds[k], LOW);
}
//generateSequence();
}
void loop() {
if (gameIsOn == false && ledNumber == 1) {
generateSequence();
}
waitToStart();
delay(1000);
if (gameIsOn == true) {
showSequence();
checkSequence();
}
if (userSequence[lastLed] == sequence[lastLed]) {
delay(300);
winRound();
}
}
void generateSequence() {
randomSeed(analogRead(1));
for (int i = 0; i < lastLed; i++) {
sequence[i] = random(0, 4);
}
}
void showSequence() {
// turn off all leds
for (int k = 0; k < BTN_NUM; k++) {
digitalWrite(leds[k], LOW);
}
// asign the led to the random sequence
for (int i = 0 ; i < ledNumber ; i++) {
// search the correct led for the same random number at the sequence
for(int ledPos = 0; ledPos < BTN_NUM; ledPos++){
if (sequence[i] == ledPos) {
sequence[i] = leds[ledPos];
}
}
// Light the current sequence led
digitalWrite(sequence[i], HIGH);
ledSounds();
delay(lightSpeed);
digitalWrite(sequence[i], LOW);
ledSounds();
delay(lightSpeed);
}
}
void checkSequence() {
for (int i = 0; i < ledNumber; i++ ) {
flag = 0;
while (flag == 0) {
/*---generic check---*/
for (int k = 0; k < BTN_NUM; k++) {
if (digitalRead(btns[k]) == LOW) {
digitalWrite(btns[k], HIGH);
ledSounds();
sequence[k] = (k + 1);
flag = 1;
delay(200);
}
for (int n = 0; n < BTN_NUM; n++) {
if (userSequence[i] != sequence[i]) {
Serial.println(sequence[i]);
wrongAnswer();
return;
}
}
digitalWrite(BlueLedPin, LOW);
ledSounds();
}
}
}
ledNumber++;
}
/*--- Leds Gestures for user experience---*/
void waitToStart() {
int ledIndex = 0;
int currentLed = 0;
int toShow[4] = {0, 1, 2, 3};
for (int i = 0; i < 1000; i++) {
if (gameIsOn == false) {
if (digitalRead(BlueBtnPin) == LOW) {
gameIsOn = true;
} else {
currentLed = toShow[ledIndex];
digitalWrite(leds[currentLed], HIGH);
delay(600);
digitalWrite(leds[currentLed], LOW);
ledIndex = (ledIndex + 1) % 4;
}
}
}
}
void wrongAnswer() {
for (int i = 0; i < 4; i++) {
for (int k = 0; k < BTN_NUM; k++) {
digitalWrite(leds[k], HIGH);
tone(buzzer, 131);
}
delay(300);
for (int n = 0; n < BTN_NUM; n++) {
digitalWrite(leds[n], LOW);
noTone(buzzer);
}
delay(300);
}
ledNumber = 1;
gameIsOn = false;
lightSpeed = 500;
}
void winRound() {
int ledIndex = 0;
int currentLed = 0;
int toShow[4] = {0, 1, 2, 3};
winSound();
for (int i = 0; i < 12; i++) {
currentLed = toShow[ledIndex];
digitalWrite(leds[currentLed], HIGH);
delay(100);
digitalWrite(leds[currentLed], LOW);
ledIndex = (ledIndex + 1) % 4;
}
lightSpeed = lightSpeed - 100;
ledNumber = 1;
gameIsOn = false;
}
/*---------SOUNDS--------------*/
void ledSounds() {
if (digitalRead(BlueLedPin) == HIGH) {
tone(buzzer, 1047);
} else if (digitalRead(RedLedPin) == HIGH) {
tone(buzzer, 1319);
} else if (digitalRead(YellowLedPin) == HIGH) {
tone(buzzer, 262);
} else if (digitalRead(GreenLedPin) == HIGH) {
tone(buzzer, 523);
} else {
noTone(buzzer);
}
}
void winSound() {
for (int i = 0; i < 1; i++) {
for (uint8_t nLoop = 0; nLoop < 2; nLoop ++) {
tone(buzzer, NOTE_A5);
delay(50);
tone(buzzer, NOTE_B5);
delay(50);
tone(buzzer, NOTE_C5);
delay(50);
tone(buzzer, NOTE_B5);
delay(50);
tone(buzzer, NOTE_C5);
delay(50);
tone(buzzer, NOTE_D5);
delay(50);
tone(buzzer, NOTE_C5);
delay(50);
tone(buzzer, NOTE_D5);
delay(50);
tone(buzzer, NOTE_E5);
delay(50);
tone(buzzer, NOTE_D5);
delay(50);
tone(buzzer, NOTE_E5);
delay(50);
tone(buzzer, NOTE_E5);
delay(50);
}
noTone(buzzer);
}
}