#include <FastLED.h>
const byte boutonPins[] = {9, 8, 7, 6, 5, 4, 3, 2, 14, 15, 16, 17};
const byte ledPin = 19;
#define Mante boutonPins[0]
#define Scorpion boutonPins[1]
#define Fourmi boutonPins[2]
#define ScarabeJ boutonPins[3]
#define Coccinelle boutonPins[4]
#define Libellule boutonPins[5]
#define Guepe boutonPins[6]
#define Criquet boutonPins[7]
#define Mouche boutonPins[8]
#define Coquerelle boutonPins[9]
#define Sauterelle boutonPins[10]
#define ScarabeR boutonPins[11]
#define cocon 18
#define qtyLed 512
CRGB leds[qtyLed];
const uint8_t enigmeSequence[] = {Guepe,Scorpion,Mante,Sauterelle,Libellule,Coquerelle,Mouche,Sauterelle,ScarabeJ,
Libellule,Mouche,Guepe,Criquet,Libellule,Coccinelle,ScarabeJ,Scorpion,Mante,Coquerelle,Fourmi,Coccinelle,
Mante,ScarabeR,Criquet,Fourmi,ScarabeR,Coccinelle,Scorpion,Coquerelle,ScarabeR,Sauterelle,Criquet};
uint8_t enigmeIndex = 0;
byte boutonactuel = 0;
byte boutonattendu = 0;
byte boutoncheck = 0;
bool victoire = 0;
void setup() {
Serial.begin(9600);
for(byte i=0;i<12;i++){
pinMode(boutonPins[i], INPUT_PULLUP);
}
pinMode(ledPin, OUTPUT);
FastLED.addLeds<WS2812B,ledPin,GRB>(leds,qtyLed);
pinMode(cocon, OUTPUT);
digitalWrite(cocon, HIGH);
for(int i=0;i<512;i++){
leds[i]=CHSV(0, 0, 0);
}
FastLED.show();
}
void reset(){
for(byte j=0;j<3;j++){
for(byte k=0;k<32;k++){
for(byte i=0;i<16;i++){
leds[i+16*k]=CHSV(0, 0, 255);
}
FastLED.show();
delay(100);
}
for(int i=0;i<512;i++){
leds[i]=CHSV(0, 0, 0);
}
for(byte k=0;k<32;k++){
for(byte i=0;i<16;i++){
leds[i+16*k]=CHSV(0, 0, 0);
}
FastLED.show();
delay(100);
}
}
digitalWrite(cocon, HIGH);
victoire =0;
}
bool boutonlacher(){
while (true) {
for (byte i = 0; i < 12; i++) {
if (digitalRead(boutonPins[i]) == LOW) {
boutoncheck = 1;
break;
}
boutoncheck = 0;
}
if(boutoncheck==0){
delay(15);
return;
}
}
}
void sequenceled() {
for(int i=0;i<16;i++){
leds[16*enigmeIndex+i]=CHSV(0, 0, 255);
}
FastLED.show();
boutonlacher();
}
byte lireBoutons() {
while (true) {
for (byte i = 0; i < 12; i++) {
byte boutonPin = boutonPins[i];
if (digitalRead(boutonPin) == LOW) {
return boutonPins[i];
}
}
delay(1);
}
}
bool valideSequence() {
boutonattendu = enigmeSequence[enigmeIndex];
Serial.print("bouton attendu : ");
Serial.println(boutonattendu);
boutonactuel = lireBoutons();
Serial.print("bouton actuel : ");
Serial.println(boutonactuel);
if (boutonattendu != boutonactuel) {
return false;
}
delay(300);
return true;
}
void echec() {
for(byte k=0;k<=enigmeIndex;k++){
for(byte i=0;i<16;i++){
leds[i+16*(enigmeIndex-k)]=CHSV(0, 0, 0);
}
FastLED.show();
delay(200);
Serial.println(k);
}
Serial.println("Echec");
enigmeIndex = 0;
delay(200);
}
void loop() {
if (victoire == 1){
return;
}
if (!valideSequence()) {
echec();
}
else {
sequenceled();
enigmeIndex++;
}
if (enigmeIndex == 32) {
for(byte j=0;j<3;j++){
for(int i=0;i<qtyLed;i++){
leds[i]=CHSV(213, 255, 255);
}
FastLED.show();
delay(300);
for(int i=0;i<qtyLed;i++){
leds[i]=CHSV(0, 0, 255);
}
FastLED.show();
delay(300);
for(int i=0;i<qtyLed;i++){
leds[i]=CHSV(213, 255, 255);
}
FastLED.show();
delay(300);
}
victoire =1;
Serial.println("Victoire");
digitalWrite(cocon,LOW);
}
}