ENIB 2020 : Kubo-Matik
Sommaire
Photo de l'équipe
Que fait ce projet ?
Le Kubo-Matik est un cube multi-fonction adapté aux jeux de société. Il a donc quatre modes : compte à rebours, pile ou face, lancé de dé, et buzzer.
Liste des composants
- 7 LEDs (3 rouges, 3 bleues, 1 jaune)
- 1 haut-parleur
- 3 boutons poussoirs
- 1 carte Wemos
Partie électrique
Mode d'emploi
Pour mettre le Kubo-Matik un mode, il suffit d'appuyer sur le bouton "Mode" jusqu'à ce que le haut-parleur dise le mode voulut.
Mode compte à rebours
Ce mode est un simple compte à rebours. Chaque LED représente 5 secondes. Appuyer sur le bouton poussoir sur le haut de la boite pour lancer le compte à rebours. Ainsi, à chaque seconde, la LED "Seconde" va s'activer à chaque seconde, et toutes les 5 secondes, une LED rouge ou verte va s'éteindre. Dès que le décompte est à zéro, le son "fin du compte à rebours" sera lancé.
Il est possible de changer le temps du compte à rebours. Il suffit d'appuyer sur le bouton "Timer" jusqu'à ce que la LED correspondant au temps voulut soit allumée, avant de lancer le compte à rebours.
Mode lancé de dé
Dans ce mode, il suffit d'appuyer sur le bouton en haut de la boite pour "lancer le dé". Le résultat du dé sera affiché grâce aux six LEDs rouges et vertes.
Mode pile ou face
Ce mode permet de simuler un lancé de pièce Il suffit d'appuyer sur le bouton en haut de la boite pour "lancer la pièce". Ainsi, ce sont soit les LEDs rouges, soit les LEDs vertes qui vont s'allumer, symbolisant le résultat de la pièce.
Mode buzzer
Ce mode permet de simuler un buzzer. Appuyer sur le bouton en haut de la boite pour en faire sortir un son.
Code
Le code ci-dessous ne prend pas en compte la partie sonor du Kubo-Matik par faute de temps. N'hésitez pas à modifier et à améliorer le code !
#include <Arduino.h> //#include "AudioFileSourceSPIFFS.h" //#include "AudioGeneratorMP3.h" //#include "AudioOutputI2SNoDAC.h" // Green LED int green_led_1 = D0; int green_led_2 = D1; int green_led_3 = D2; // Red LED int red_led_1 = D3; int red_led_2 = D4; int red_led_3 = D5; // Yellow LED int yellow_led_1 = D6; int led_list[] = {D0, D3, D1, D4, D2, D5}; // Button int mode_button = D8; int setting_button = 13; int buzzer_button = RX; bool previous_setting_button_state = false; bool previous_buzzer_button_state = false; bool previous_mode_button_state = false; // Sound int speaker = 1; unsigned int mode; // Count-down variables unsigned int count_down_index = 0; unsigned int count_down_initial[] = {5, 10, 15, 20, 25, 30}; // seconds unsigned int count_down_value = 0; bool count_down_state = false; // Head and tails bool head_tails_state = false; int randomInt; // Dice roll bool dice_roll_state = false; void setup() { // put your setup code here, to run once: /////////////////change pin to gpio//////// //GPIO 1 (TX) swap the pin to a GPIO. pinMode(1, FUNCTION_3); //GPIO 3 (RX) swap the pin to a GPIO. pinMode(3, FUNCTION_3); /////////////////////// Serial.begin(9600); // OUTPUT Mode pinMode(green_led_1, OUTPUT); digitalWrite(green_led_1, LOW); pinMode(green_led_2, OUTPUT); pinMode(green_led_3, OUTPUT); pinMode(red_led_1, OUTPUT); pinMode(red_led_2, OUTPUT); pinMode(red_led_3, OUTPUT); pinMode(yellow_led_1, OUTPUT); pinMode(speaker, OUTPUT); // INPUT Mode pinMode(mode_button, INPUT); pinMode(setting_button, INPUT); pinMode(buzzer_button, INPUT); randomSeed(analogRead(0)); mode = 1; } // Mode List // // 0 => Buzzer // 1 => Count-down // 2 => Head and tails // 3 => Dice roll void loop() { // put your main code here, to run repeatedly: bool mode_button_state = digitalRead(mode_button); if ((dice_roll_state == false) && (head_tails_state == false) && (count_down_state == false)) { if ((mode_button_state != previous_mode_button_state) && (mode_button_state == HIGH)) { if (mode >= 3) { mode = 0; } else { mode += 1; } delay(250); switch (mode) { case 0: digitalWrite(green_led_1, HIGH); delay(500); break; case 1: digitalWrite(red_led_1, HIGH); delay(500); break; case 2: digitalWrite(green_led_2, HIGH); delay(500); break; case 3: digitalWrite(red_led_2, HIGH); delay(500); break; } } } if (mode == 0) { // Buzzer while (digitalRead(buzzer_button)) { digitalWrite(red_led_1, LOW); digitalWrite(red_led_2, LOW); digitalWrite(red_led_3, LOW); digitalWrite(green_led_1, HIGH); digitalWrite(green_led_2, HIGH); digitalWrite(green_led_3, HIGH); delay(100); digitalWrite(green_led_1, LOW); digitalWrite(green_led_2, LOW); digitalWrite(green_led_3, LOW); digitalWrite(red_led_1, HIGH); digitalWrite(red_led_2, HIGH); digitalWrite(red_led_3, HIGH); delay(100); } } else if (mode == 1) { // Count-down if (count_down_state == false) { bool setting_button_state = digitalRead(setting_button); if ((setting_button_state != previous_setting_button_state) && (setting_button_state == HIGH)) { if (count_down_index >=5) { count_down_index = 0; } else { count_down_index += 1; } delay(250); } previous_setting_button_state = setting_button_state; switch (count_down_index) { case 0: digitalWrite(green_led_1, HIGH); break; case 1: digitalWrite(red_led_1, HIGH); break; case 2: digitalWrite(green_led_2, HIGH); break; case 3: digitalWrite(red_led_2, HIGH); break; case 4: digitalWrite(green_led_3, HIGH); break; case 5: digitalWrite(red_led_3, HIGH); break; } if (digitalRead(buzzer_button)) { count_down_state = true; count_down_value = count_down_initial[count_down_index]; } } else { if (count_down_value >= 5) { digitalWrite(green_led_1, HIGH); if (count_down_value >= 10) { digitalWrite(red_led_1, HIGH); if (count_down_value >= 15) { digitalWrite(green_led_2, HIGH); if (count_down_value >= 20) { digitalWrite(red_led_2, HIGH); if (count_down_value >= 25) { digitalWrite(green_led_3, HIGH); if (count_down_value >= 30) { digitalWrite(red_led_3, HIGH); } } } } } } digitalWrite(yellow_led_1, HIGH); delay(100); digitalWrite(yellow_led_1, LOW); delay(900); count_down_value -= 1; if (count_down_value <= 0) { count_down_state = false; } } } else if (mode == 2) { // Head and tails if (head_tails_state == false) { bool buzzer_button_state = digitalRead(buzzer_button); if ((buzzer_button_state != previous_buzzer_button_state) && (buzzer_button_state == HIGH)) { randomInt = random(1, 7); head_tails_state = true; } previous_buzzer_button_state = buzzer_button_state; } else { for (int k = 0; k < 4; k++) { for (int j = 0; j < 6; j++) { digitalWrite(led_list[j], HIGH); delay(50); digitalWrite(led_list[j], LOW); delay(50); } } for (int i = 0; i < 5; i++) { if (randomInt % 2) { digitalWrite(green_led_1, LOW); digitalWrite(green_led_2, LOW); digitalWrite(green_led_3, LOW); delay(200); digitalWrite(green_led_1, HIGH); digitalWrite(green_led_2, HIGH); digitalWrite(green_led_3, HIGH); delay(200); } else{ digitalWrite(red_led_1, LOW); digitalWrite(red_led_2, LOW); digitalWrite(red_led_3, LOW); delay(200); digitalWrite(red_led_1, HIGH); digitalWrite(red_led_2, HIGH); digitalWrite(red_led_3, HIGH); delay(200); } } delay(2000); head_tails_state = false; } } else { // Dice roll if (dice_roll_state == false) { bool buzzer_button_state = digitalRead(buzzer_button); if ((buzzer_button_state != previous_buzzer_button_state) && (buzzer_button_state == HIGH)) { randomInt = random(1, 7); dice_roll_state = true; } previous_buzzer_button_state = buzzer_button_state; } else { for (int k = 0; k < 4; k++) { for (int j = 0; j < 6; j++) { digitalWrite(led_list[j], HIGH); delay(50); digitalWrite(led_list[j], LOW); delay(50); } } if (randomInt >= 1) { delay(100); digitalWrite(green_led_1, HIGH); if (randomInt >= 2) { delay(100); digitalWrite(red_led_1, HIGH); if (randomInt >= 3) { delay(100); digitalWrite(green_led_2, HIGH); if (randomInt >= 4) { delay(100); digitalWrite(red_led_2, HIGH); if (randomInt >= 5) { delay(100); digitalWrite(green_led_3, HIGH); if (randomInt >= 6) { delay(100); digitalWrite(red_led_3, HIGH); } } } } } } delay(2000); dice_roll_state = false; } } digitalWrite(green_led_1, LOW); digitalWrite(red_led_1, LOW); digitalWrite(green_led_2, LOW); digitalWrite(red_led_2, LOW); digitalWrite(green_led_3, LOW); digitalWrite(red_led_3, LOW); }