#include <FastLED.h>
#define ledpin 9 // Pino de dados da matriz de LEDs
#define Numleds 4 // Número de LEDs na matriz
const byte buttonpins [4] = {15,16,14,17};
const unsigned int tones[4] = {440,330,220,110};
CRGB leds[Numleds];
long int cores[Numleds] = {0xFFFF00,0x0000FF,0xFF0000,0x7FFF00};
//se o número de leds for alterado, essas cores também precisam ser
const byte buzzerpin = 13;
const long soundtimer = 100;
const byte tamanhomaximo = 5;
byte rodada = 0;
byte sequencia [tamanhomaximo];
void setup() {
FastLED.addLeds<WS2812, ledpin, GRB>(leds, Numleds); // GRB ordering is typical
randomSeed(analogRead(A6));
pinMode(buzzerpin, OUTPUT);
for(int i=0;i<=3;i++){
pinMode(buttonpins[i], INPUT_PULLUP);
}
}
void loop() {
for(byte i=0;i<tamanhomaximo;i++){
criasequencia();
for (byte i=0;i<=rodada;i++){
byte j = lebotao();
if (j != sequencia[i]) {
errou();
rodada = 0;
return;
//while(1);
}
}
rodada=rodada+1;
if (rodada == tamanhomaximo){
ganhou();
rodada = 0;
}
delay(800);
}
}
void criasequencia(){
sequencia[rodada] = random(4);
for (byte i=0;i<=rodada;i++){
acende_toca(sequencia[i]);
}
delay(150);
}
byte lebotao(){
byte flag = 0;
byte resultado;
while (flag == 0){
for(int i=0;i<=3;i++){
if(digitalRead(buttonpins[i])==LOW){
acende_toca(i);
resultado = i;
delay(50);
flag = 1;
}
}
}
return (resultado);
}
void acende_toca(byte i){
leds[i] = cores[i];
FastLED.show();
tone(buzzerpin,tones[i],soundtimer);
delay(200);
leds[i] = CRGB::Black;
FastLED.show();
}
void errou(){
for (int i=0;i<=3;i++){
leds[i] = cores[2];
}
FastLED.show();
tone(buzzerpin,70,1000);
delay(1000);
for (int i=0;i<=3;i++){
leds[i] = CRGB::Black;
}
FastLED.show();
delay(1000);
}
void ganhou(){
for (int i=0;i<=3;i++){
leds[i] = cores[3];
}
FastLED.show();
tone(buzzerpin,700,1000);
delay(1000);
for (int i=0;i<=3;i++){
leds[i] = CRGB::Black;
}
FastLED.show();
delay(1000);
}