#include <Toggle.h>
constexpr byte pinLancer = 2;
constexpr byte pinChoix11 = A0;
constexpr byte pinChoix12 = A1;
constexpr byte pinChoix21 = A2;
constexpr byte pinChoix22 = A3;
Toggle boutonLancer(pinLancer);
Toggle switch11(pinChoix11);
Toggle switch12(pinChoix12);
Toggle switch21(pinChoix21);
Toggle switch22(pinChoix22);
// pas d'enum pour pouvoir faire des opérations bitwise
constexpr byte DES_INCONNU = 0;
constexpr byte DES_4 = 0b0101;
constexpr byte DES_6 = 0b0110;
constexpr byte DES_10 = 0b1001;
constexpr byte DES_20 = 0b1010;
byte des = DES_INCONNU;
void poll() {
boutonLancer.poll();
switch11.poll();
switch12.poll();
switch21.poll();
switch22.poll();
}
void printDes() {
switch (des) {
case DES_4: Serial.println("DES 4 FACES"); break;
case DES_6: Serial.println("DES 6 FACES"); break;
case DES_10: Serial.println("DES 10 FACES"); break;
case DES_20: Serial.println("DES 20 FACES"); break;
default: Serial.println("DES INCONNU."); break;
}
}
void choixDes() {
poll();
bool changement = false;
// on fait les 4 tests plutôt que tout en une fois pour forcer l'appel à onPress
changement |= switch11.onPress();
changement |= switch12.onPress();
changement |= switch21.onPress();
changement |= switch22.onPress();
if (changement) {
des = ((digitalRead(pinChoix11) == LOW) ? 0b01u : 0b10u) << 2;
des |= (digitalRead(pinChoix21) == LOW) ? 0b01u : 0b10u;
printDes();
}
}
int testLancer() {
poll();
int valeur = 0;
if (boutonLancer.onPress()) {
Serial.print(F("Lancer : "));
switch (des) {
case DES_4: valeur = random(1, 5); break;
case DES_6: valeur = random(1, 7); break;
case DES_10: valeur = random(1, 11); break;
case DES_20: valeur = random(1, 21); break;
default: Serial.println("DES INCONNU."); break;
}
Serial.println(valeur);
return valeur;
}
return 0;
}
void setup() {
randomSeed(analogRead(A5));
boutonLancer.begin(pinLancer);
switch11.begin(pinChoix11);
switch12.begin(pinChoix12);
switch21.begin(pinChoix21);
switch22.begin(pinChoix22);
Serial.begin(115200);
}
void loop() {
choixDes();
testLancer();
}Lancer
sw11
sw12
sw21
sw22