UPDATE
I looked at Satwik Pasani's answer and used his tips. So I have a new code which I think works better and I wanted to show it to you.
#define RedTone 165
#define GreenTone 330
#define YelloTone 659
#define BlueTone 1319
#define FailTone 100
#define Success1 440
#define Success2 600
#define BuzzerPin 8
/* The led and button pins are set this way because I use their position as a hexadecimal.
LED PINS:
red:0
green:1
yello:2
blue:3
Button Pins:
red:4
green:5
yello:6
blue:7
*/
byte mem[20];
byte order[4] = {0x01, 0x02, 0x04, 0x08};
int sound[9] = {0, RedTone, GreenTone, 0, YelloTone, 0, 0, 0, BlueTone}; //the tones are placed in the arrey in the hex spot for turning on the leds.
void setup() {
DDRD = 0x0F;
randomSeed(analogRead(0));
pinMode(8, 1);
}
void blink(int i) { //Blinking and making sound
PORTD = i;
tone(BuzzerPin, sound[i]);
delay(500);
PORTD = 0x00;
noTone(BuzzerPin);
delay(500);
}
void fs(bool s) { //Sucsseded of Failed
if (s == 0) {
PORTD = 0x15;
tone(BuzzerPin, FailTone);
delay(1000);
PORTD = 0x00;
noTone(BuzzerPin);
delay(500);
}
else {
tone(BuzzerPin, Success1);
delay(250);
tone(BuzzerPin, Success2);
delay(350);
noTone(BuzzerPin);
delay(500);
}
}
void set_mem() { //sets the random order of the leds and transforms into the led's pin in hexadecimal
int b;
for (int i = 0; i < 20; i++)
mem[i] = order[random(4)];
}
bool check(int i) { //checks if the pressed button is correct
byte look;
for (int x = 0; x <= i; x++) {
look = 0;
do
{
look = PIND >> 4; //makes the button press pin to the pin of led with the same color
}
while (look == 0); //waits until a button is pressed
blink(look); //lights up the button's led
if (look != mem[x]) //checks if the wrong button was pressed
return 0; //tells program to reset game
}
return 1;
}
void loop() {
set_mem();
for (int i = 0; i < 20; i++) {
fs(1); //shows if the player succeded
for (int x = 0; x <= i; x++)
blink(mem[x]);
if (check(i) == 0) {
fs(0); //shows if the player failed
break; //couses a restart of the game
}
}
}
New place to test out code: Here