/* ============================================
code is placed under the MIT license
Copyright (c) 2026 JAY
POUR LE FORUM : https://www.developpez.net/forums/u1627910/jay-m/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include "Globals.h"
#include "Piste.h"
#include "RoueCodeuse.h"
#include <Toggle.h>
Piste lesPistes[] = {
// #piste, R, G, B, LaserRouge, LaserJaune, LaserBleu, BandeauGauche, BandeauDroite, DinMatrice, FdcArrivee, FdcDepart, Dir, Step, Enable, activation, acceleration, vitesseMax
{ 1, 0, 0, 255, 4, 3, 2, 22, 23, 24, 35, 36, 45, 46, A0, A12, ACCELERATION, VITESSE_MAX},
{ 2, 255, 0, 0, 7, 6, 5, 25, 26, 27, 37, 38, 47, 48, A1, A13, ACCELERATION, VITESSE_MAX},
{ 3, 0, 255, 0, 10, 9, 8, 28, 29, 30, 39, 40, 49, 50, A2, A14, ACCELERATION, VITESSE_MAX},
{ 4, 255, 0, 255, 13, 12, 11, 31, 32, 33, 41, 42, 51, 52, A3, A15, ACCELERATION, VITESSE_MAX},
};
const uint8_t NUM_PISTES = sizeof lesPistes / sizeof * lesPistes; // Nombre de pistes
const uint8_t pinCommencer = 44;
const uint8_t pinLedCommencer = 53;
const uint8_t pinRaz = A5;
const uint8_t pin1RoueCodeuse = 14;
const uint8_t pin2RoueCodeuse = 15;
const uint8_t pin4RoueCodeuse = 16;
const uint8_t pin8RoueCodeuse = 17;
const uint32_t tempoRoueCodeuse = 250; // en ms. ne pas lire plus souvent que cela
Toggle boutonCommencer(pinCommencer);
Toggle boutonRaz(pinRaz);
RoueCodeuse roueCodeuse(pin1RoueCodeuse, pin2RoueCodeuse, pin4RoueCodeuse, pin8RoueCodeuse, tempoRoueCodeuse); // 1 2 4 8
enum EtatJeu : byte {JEU_EN_ATTENTE, JEU_EN_COURS, JEU_GAGNANT};
EtatJeu etatJeu = JEU_EN_ATTENTE;
void commencer() {
if (etatJeu != JEU_EN_ATTENTE) { // on arrête le jeu
for (Piste& piste : lesPistes)
if (piste.etat != PISTE_INACTIVE) piste.commande(PISTE_ARRET);
digitalWrite(pinLedCommencer, LED_OFF);
etatJeu = JEU_EN_ATTENTE;
} else {
for (Piste& piste : lesPistes)
if (piste.etat != PISTE_INACTIVE) {
piste.commande(PISTE_ATTENTE_BALLE);
digitalWrite(pinLedCommencer, LED_ON);
etatJeu = JEU_EN_COURS;
}
}
}
void razAction() {
etatJeu = JEU_EN_ATTENTE;
digitalWrite(pinLedCommencer, LED_OFF);
for (Piste& piste : lesPistes) {
if (piste.etat != PISTE_INACTIVE) piste.commande(PISTE_HOMING);
}
}
void razFin() {
etatJeu = JEU_EN_ATTENTE;
for (Piste& piste : lesPistes) {
if (piste.etat != PISTE_INACTIVE) piste.commande(PISTE_ARRET);
}
}
void action() {
boutonCommencer.poll();
if (boutonCommencer.onPress()) {
D_println(F("COMMENCER"));
commencer();
}
boutonRaz.poll();
if (boutonRaz.onPress()) {
D_println(F("DEBUT RAZ"));
razAction();
}
if (boutonRaz.onRelease()) {
D_println(F("FIN RAZ"));
razFin();
}
static uint8_t valeurRoue = 0xFF;
uint8_t nouvelleValeurRoue = roueCodeuse.lireValeur();
if (nouvelleValeurRoue != valeurRoue) {
valeurRoue = nouvelleValeurRoue;
D_print(F("Roue Codeuse : ")); D_println(valeurRoue);
for (Piste& piste : lesPistes) piste.setFacteurVitesse(valeurRoue + 1); // de 1 à 10
}
for (Piste& piste : lesPistes) piste.action();
}
void gestionVainqueur() {
static uint32_t debutJeuGagnant = 0;
if (etatJeu == JEU_EN_COURS) {
Piste* pisteGagante = nullptr; // pas de vainqueur
for (Piste& piste : lesPistes) {
if (piste.etat == PISTE_VAINQUEUR) {
pisteGagante = &piste;
etatJeu = JEU_GAGNANT;
debutJeuGagnant = millis();
D_print(F("Piste ")); D_print(pisteGagante->numeroPiste); D_println(F(" - VAINQUEUR"));
break;
}
}
if (pisteGagante != nullptr) { // on a un gagnant
// on notifie aux autres qu'ils ont perdu
for (Piste& piste : lesPistes)
if (&piste != pisteGagante && piste.etat != PISTE_INACTIVE) piste.commande(PISTE_PERDANT);
}
}
else if (etatJeu == JEU_GAGNANT) {
if (millis() - debutJeuGagnant > dureeAnimationGagnant) {
// on a fini les animation
for (Piste& piste : lesPistes)
if (piste.etat != PISTE_INACTIVE) piste.commande(PISTE_ARRET);
digitalWrite(pinLedCommencer, LED_OFF);
etatJeu = JEU_EN_ATTENTE;
}
}
}
void setup() {
pinMode(pinLedCommencer, OUTPUT); digitalWrite(pinLedCommencer, LED_OFF);
boutonCommencer.begin(pinCommencer);
boutonRaz.begin(pinRaz);
roueCodeuse.begin();
D_SerialBegin(115200);
for (Piste& piste : lesPistes) piste.begin();
}
void loop() {
action();
gestionVainqueur();
}GO
RaZ
fdc Arrivée
fdc Départ
fdc Arrivée
fdc Départ
fdc Arrivée
fdc Départ
fdc Arrivée
fdc Départ
1
2
4
8
Roue Codeuse (0 - 9)
ACTIVATION
ACTIVATION
ACTIVATION
ACTIVATION