#include<Keypad.h>int buzzer =12; // Where the buzzer signal (S) is wiredint led_red = A3; // Where the red LED is wiredint led_green = A2; // Where the green LED is wiredint led_blue = A1; // Where the blue LED is wiredint led_yellow = A0; // Where the yellow LED is wiredconst byte ROWS =4;const byte COLS =4;constint winningAmount =7; // How many rounds of remembering - 1longsequence[winningAmount]; // Array to hold sequenceint count =0; // Sequence counterlong input =5; // Button indicatorint wait =500; // Variable delay as sequence gets longerchar customKey ='X'; // Temp variable for keypad inputcharbuttons[ROWS][COLS]={{'R','G','B','Y'},{'R','G','B','Y'},{'R','G','B','Y'},{'R','G','B','Y'}};byte rowPins[ROWS]={9,8,7,6};byte colPins[COLS]={5,4,3,2};Keypad customKeypad =Keypad(makeKeymap(buttons), rowPins, colPins, ROWS, COLS);/* playtone function taken from Oomlout sample takes a tone variable that is half the period of desired frequency and a duration in milliseconds */voidplaytone(inttone,intduration){for(long i =0; i < duration *1000L; i += tone *2){digitalWrite(buzzer, HIGH);delayMicroseconds(tone);digitalWrite(buzzer, LOW);delayMicroseconds(tone);}}/* functions to flash LEDs and play corresponding tones very simple - turn LED on, play tone for .5s, turn LED off*/voidflash_red(){digitalWrite(led_red, HIGH);playtone(2273,wait); // low AdigitalWrite(led_red, LOW);}voidflash_blue(){digitalWrite(led_blue, HIGH);playtone(1700,wait); // DdigitalWrite(led_blue, LOW);}voidflash_yellow(){digitalWrite(led_yellow, HIGH);playtone(1275,wait); // GdigitalWrite(led_yellow, LOW);}voidflash_green(){digitalWrite(led_green, HIGH);playtone(1136,wait); // high AdigitalWrite(led_green, LOW);}// a simple test function to flash all of the LEDs in turnvoidruntest(){flash_red();flash_green();flash_yellow();flash_blue();}/* a function to flash the LED corresponding to what is held in the sequence*/voidsquark(longled){switch(led){case0:flash_red();break;case1:flash_green();break;case2:flash_yellow();break;case3:flash_blue();break;}delay(50);}// function to congratulate winning sequencevoidcongratulate(){digitalWrite(led_red, HIGH); // turn all LEDs ondigitalWrite(led_green, HIGH);digitalWrite(led_yellow, HIGH);digitalWrite(led_blue, HIGH);playtone(1014,250); // play a jingledelay(25);playtone(1014,250);delay(25);playtone(1014,250);delay(25);playtone(956,500);delay(25);playtone(1014,250);delay(25);playtone(956,500);delay(2000);digitalWrite(led_red, LOW); // turn all LEDs offdigitalWrite(led_green, LOW);digitalWrite(led_yellow, LOW);digitalWrite(led_blue, LOW);resetCount(); // reset sequence}// function to reset after winning or losingvoidresetCount(){ count =0; wait =500;}// function to build and play the sequencevoidplaySequence(){sequence[count]=random(4); // add a new value to sequencefor(int i =0; i < count; i++){ // loop for sequence lengthsquark(sequence[i]); // flash/beep} wait =500-(count *15); // vary delay count++; // increment sequence length}// function to read sequence from playervoidreadSequence(){for(int i=1; i < count; i++){ // loop for sequence lengthwhile(input==5){ // wait until button pressed customKey =customKeypad.getKey();if(customKey =='R'){ // Red button input =0;}if(customKey =='G'){ // Green button input =1;}if(customKey =='Y'){ // Yellow button input =2;}if(customKey =='B'){ // Blue button input =3;}}if(sequence[i-1]== input){ // was it the right button?squark(input); // flash/buzzif(i +1== winningAmount){ // check for correct sequence of "winningAmount"congratulate(); // congratulate the winner}}else{playtone(4545,1000); // low tone for failsquark(sequence[i-1]); // double flash for the right coloursquark(sequence[i-1]);resetCount(); // reset sequence} input =5; // reset input customKey ='X';}}// standard sketch setup functionvoidsetup(){pinMode(led_red, OUTPUT); // configure LEDs and buzzer on outputspinMode(led_green, OUTPUT);pinMode(led_yellow, OUTPUT);pinMode(led_blue, OUTPUT);pinMode(buzzer, OUTPUT);randomSeed(analogRead(5)); // random seed for sequence generationSerial.begin(9600); //runtest();}// standard sketch loop functionvoidloop(){playSequence(); // play the sequencereadSequence(); // read the sequencedelay(1000); // wait a sec}