/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
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 "Clock.h"
#include <Encoder.h> // https://www.pjrc.com/teensy/td_libs_Encoder.html
const byte encoderClkPin = 2;
const byte encoderDtPin = 3;
const byte encoderSwPin = 4;
long position;
Encoder encoder(encoderDtPin, encoderClkPin);
Toggle btnEncoder;
Clock clocks[] = {
{"Green Team", 8, 9, 10}, // nom, clk, dio, button
{"Blue Team", 6, 7, 5},
};
const byte numClocks = sizeof clocks / sizeof * clocks;
const unsigned long defaultDuration = 10000ul; // ms
bool encoderChanged() {
long newPosition = encoder.read() >> 2;
if (newPosition != position) {
position = newPosition;
return true;
}
return false;
}
void checkEncoder() {
if (encoderChanged()) {
Serial.println(position);
}
btnEncoder.poll();
if (btnEncoder.onPress()) {
Serial.println("PRESSED");
}
}
void setup() {
Serial.begin(115200);
btnEncoder.begin(encoderSwPin);
for (auto &c : clocks) {
c.begin();
c.setTime(defaultDuration);
}
}
void loop() {
bool timeOut = false;
Clock* winningClock = nullptr;
for (auto &c : clocks) {
switch (c.update()) {
case Clock::NONE: break;
case Clock::TIMEOUT:
timeOut = true;
winningClock = &c;
break;
case Clock::PRESSED:
for (auto &c : clocks) c.pause(); // on arrête toutes les horloges
c.play(); // seulement celle ci démarre
break;
}
}
if (timeOut) {
Serial.print(winningClock->getName()); Serial.println(" HAS WON");
for (auto &c : clocks) {
c.pause();
c.setTime(defaultDuration);
}
}
checkEncoder();
}
Réglages