#include <TM1637Display.h>
#define CLK1 2
#define DIO1 3
TM1637Display displayA(CLK1, DIO1);
#define CLK2 4
#define DIO2 5
TM1637Display displayB(CLK2, DIO2);
#define nbMilliSecondes(_temps_) ((_temps_ % 1000) / 10)
#define nbSecondes(_temps_) ((_temps_ / 1000) % 60)
#define nbMinutes(_temps_) (((_temps_ / 1000) / 60) % 60)
// Broches du bouton de depart et de stop
byte BP_A = 11;
byte BP_B = 12;
byte BP_plus = 10;// bouton ajoute une minute aux chronos
boolean chronoplus = false; // variable ajoute une minute aux chronos
boolean BP_plusRelache = true ;//verif bouton plus relaché
// Temps de jeu en millisecondes
unsigned long chronoA = 1200000;//70000; //c70 secondes pour essais
unsigned long tpsRestantA;
unsigned long tpsLimiteA = chronoA + millis();
unsigned long memo_temps_restantA;
//les etapes
unsigned long tpsPasseA;
boolean cycleLance = false ;
boolean jeu_commence = false ;
unsigned long chronoB = 1200000;//70000; //c70 secondes pour essais
unsigned long tpsRestantB;
unsigned long tpsLimiteB = chronoB + millis();
unsigned long memo_temps_restantB;
//les etapes
unsigned long tpsPasseB;
//les etapes
boolean etat_stand_by = true;
boolean etat_comptageA = false;
boolean memo_etatnondispo = false;
boolean etat_comptageB = false;
#define ON true
#define OFF false
//antirepetition BP_plus
const unsigned long nonRepetitionTempo = 200;//antirepetition de BP_plus
unsigned long nonRepetitionMillis = 0;
boolean nonRepetition = false;
void setup() {
Serial.begin(115200);
//mySerial.begin(9600);
pinMode(BP_A, INPUT_PULLUP);
pinMode(BP_B, INPUT_PULLUP);
pinMode(BP_plus, INPUT_PULLUP);
displayA.setBrightness(7);
displayA.showNumberDecEx((nbMinutes(chronoA) * 100) + nbSecondes(chronoA), 0b11100000, true);
displayB.setBrightness(7);
displayB.showNumberDecEx((nbMinutes(chronoB) * 100) + nbSecondes(chronoB), 0b11100000, true);
}
void compteareboursA() {
tpsRestantA = tpsLimiteA - millis(); tpsPasseA = chronoA - tpsRestantA;
Serial.print("A:");Serial.print(tpsRestantA);Serial.print(", ");Serial.println(tpsLimiteA);
if (tpsRestantA > 60000) displayA.showNumberDecEx((nbMinutes(tpsRestantA) * 100) + nbSecondes(tpsRestantA), 0b11100000, true);
else {
if (tpsRestantA <= 0) {
displayA.showNumberDecEx(0000, 0b11100000, true);
}
else {
displayA.showNumberDecEx((nbSecondes(tpsRestantA) * 100) + nbMilliSecondes(tpsRestantA), 0b11100000, true);
}
}
}
void compteareboursB() {
tpsRestantB = tpsLimiteB - millis(); tpsPasseB = chronoB - tpsRestantB;
Serial.print("B:");Serial.println(tpsRestantB);
if (tpsRestantB > 60000) displayB.showNumberDecEx((nbMinutes(tpsRestantB) * 100) + nbSecondes(tpsRestantB), 0b11100000, true);
else {
if (tpsRestantB <= 0) {
displayB.showNumberDecEx(0000, 0b11100000, true);
}
else {
displayB.showNumberDecEx((nbSecondes(tpsRestantB) * 100) + nbMilliSecondes(tpsRestantB), 0b11100000, true);
}
}
}
void loop() {
if ((!digitalRead(BP_plus)) && (BP_plusRelache == true) && (nonRepetition == false) && (!jeu_commence)) {
chronoA = chronoA + 60000 ;
chronoB = chronoA ;
Serial.print("BP_plus: ");Serial.print(chronoA);Serial.print(", ");Serial.println(chronoB);
nonRepetitionMillis = millis();
nonRepetition = true;
chronoplus = true ;
BP_plusRelache = false ;
tpsLimiteA = chronoA + millis(); tpsRestantA = tpsLimiteA - millis();
memo_temps_restantA = tpsRestantA ;
tpsLimiteB = chronoB + millis(); tpsRestantB = tpsLimiteB - millis();
memo_temps_restantB = tpsRestantB ;
Serial.print("A: ");Serial.print(tpsLimiteA);Serial.print(", ");Serial.println(chronoA);
Serial.print("B: ");Serial.print(tpsLimiteB);Serial.print(", ");Serial.println(chronoB);
compteareboursA();
compteareboursB();
}
if (chronoplus == true) {
chronoplus = false;
}
if ( (millis() - nonRepetitionMillis) >= nonRepetitionTempo) {
nonRepetition = false;
}
// memo_temps_restant = tpsRestant ;
if (digitalRead (BP_plus)) {
BP_plusRelache = true;
}
if (etat_stand_by) {
memo_etatnondispo = false;
tpsLimiteA = chronoA + millis();
memo_temps_restantA = tpsRestantA ;
tpsLimiteA = chronoA + millis(); tpsRestantA = tpsLimiteA - millis();
tpsLimiteB = chronoB + millis();
memo_temps_restantB = tpsRestantB ;
tpsLimiteB = chronoB + millis(); tpsRestantB = tpsLimiteB - millis();
}
if ((!digitalRead(BP_A)) && (!etat_comptageA) && (cycleLance == false)) {
tpsLimiteA = memo_temps_restantA + millis();
etat_stand_by = false;
etat_comptageA = true;
jeu_commence = true;
}
if ((!digitalRead(BP_B)) && (!etat_comptageB) && (cycleLance == false)) {
tpsLimiteB = memo_temps_restantB + millis();
etat_stand_by = false;
etat_comptageB = true;
jeu_commence = true;
}
if (!digitalRead(BP_B) && (etat_comptageA)) {
memo_temps_restantA = tpsRestantA;
etat_comptageA = false;
}
if (!digitalRead(BP_A) && (etat_comptageB)) {
memo_temps_restantB = tpsRestantB;
etat_comptageB = false;
}
//afficheur 4 digits
if (etat_comptageA) {
compteareboursA();
}
if ((tpsPasseA) >= chronoA) {
compteareboursA();
displayA.showNumberDecEx(0000, 0b11100000, true);
memo_etatnondispo = ON;
cycleLance = true ;
etat_comptageA = false;
etat_stand_by = true;
}
if (etat_comptageB) {
compteareboursB();
}
if ((tpsPasseB) >= chronoB) {
compteareboursB();
displayB.showNumberDecEx(0000, 0b11100000, true);
memo_etatnondispo = ON;
cycleLance = true ;
etat_comptageB = false;
etat_stand_by = true;
}
if (memo_etatnondispo == ON) {
//etat_comptage = false;
// etat_stand_by = true;
//memo_etatnondispo = false;
}
}