#include "pitches.h"\
/*const- define pin num for leds, buttons and more
*/
const uint8_t ledPins[] = {0,1,6,7};
const uint8_t buttonPins[] = {2,3,4,5};
const ledPins[0]=Yellow;
const ledPins[1]=Blue;
const ledPins[2]=Green;
const ledPins[3]=Red;
const buttonPins[0]=Blue_P;
const buttonPins[1]=Yellow_P;
const buttonPins[2]=Green_P;
const buttonPins[3]=Red_P;
#define SPEAKER_PIN 8;
#define MAX_GAME_LENGTH=100;
uint8_t gameSequence[MAX_GAME_LENGTH] = {0};
uint8_t gameIndex = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Yellow, OUTPUT);
pinMode(Blue OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Red, OUTPUT);
pinMode(Yellow_P, INPUT_PULLUP);
pinMode(Blue_P, INPUT_PULLUP);
pinMode(Green_P, INPUT_PULLUP);
pinMode(Red_P, INPUT_PULLUP);
pinMode(SPEAKER_PIN,OUTPUT);
// The following line primes the random number generator.
// It assumes pin A3 is floating (disconnected):
randomSeed(analogRead(A3));
}
/**
the led Lights the given LED and plays a suitable tone
ask
*/
void lightandplaytone(byte ledIndex){
digitalWrite(ledPins[ledIndex], HIGH);
tone(SPEAKER_PIN, gameTones[ledIndex]);
digitalWrite(ledPins[ledIndex], LOW);
noTone(SPEAKER_PIN);
delay(100);
}
void playsequence(){
for(int i=0; i<gameIndex; i++){
byte currentled=gameSequence[i];
lightandplaytone(currentled);
delay(50);
}
}
byte readbuttons(){
while(true){
for(int i=0; i<4; i++){
/*if the button is pressed
*/
if(digitalRead(buttonPins)=LOW){
return i;
}
}
delay(1);
}
}
void gameover(){
Serial.print("Game Over");
GameIndex=0
delay(200);
}
bool checkUserSequence(){
for(int i=0;i<GameIndex; i++){
byte expectedbutton=gameSequence[i];
byte actualbutton=readButtons();
if (expectedButton != actualButton) {
return false;
}
}
return true;
}
void loop() {
// put your main code here, to run repeatedly:
// Add a random color to the end of the sequence
gameSequence[gameIndex]=random(0,4);
gameIndex++;
playSequence();
if(checkUsersequence()==false){
gameOver();
}
delay (300);
}