// ====== START CONFIG SECTION ======================================================
int ledPins[] = {15, 5, 19};
int buzzerPins[] = {4, 18, 21};
// referee summon parameters
const int nbSummonBeeps = 3;
const note_t cfgSummonNote = NOTE_F;
const int cfgSummonOctave = 7; // F7
const int cfgSummonBeepMilliseconds = 3000;
const int cfgSummonSilenceMilliseconds = 1000;
const int cfgSummonLedDuration = cfgSummonBeepMilliseconds;
// ====== END CONFIG SECTION ======================================================
#include "Tone32.hpp"
#define ELEMENTCOUNT(x) (sizeof(x) / sizeof(x[0]))
// 6 pins where we have to detect transitions. initial state unknown.
int prevDecisionPinState[] = {-1, -1, -1, -1, -1, -1};
// for each referee, a Tone generator, and control for a LED
Tone32 tones[3] = {Tone32(buzzerPins[0], 0), Tone32(buzzerPins[1], 1), Tone32(buzzerPins[2], 2)};
int beepingIterations[] = {0, 0, 0};
int ledStartedMillis[] = {0, 0, 0};
int ledDuration[] = {0, 0, 0};
note_t beepNote;
int beepOctave;
int silenceMilliseconds;
int beepMilliseconds;
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(2000);//led delay
}
pinMode(25, INPUT_PULLUP);
setupPins();
setupTones();
// first use of wokwi buzzer is not heard.
simulationBuzzerWorkaround();
}
boolean buttonPressed = false;
void loop() {
delay(500);//led delay
// trigger on first press of button, then sit idle.
if (digitalRead(25) == LOW && !buttonPressed) {
changeSummonStatus(0, true);
changeSummonStatus(1, true);
changeSummonStatus(2, true);
buttonPressed = true;
}
buzzerLoop();
ledLoop();
}
void setupPins() {
for (int j = 0; j < ELEMENTCOUNT(ledPins); j++) {
pinMode(ledPins[j], OUTPUT);
}
for (int j = 0; j < ELEMENTCOUNT(buzzerPins); j++) {
pinMode(buzzerPins[j], OUTPUT);
}
}
void setupTones() {
for (int j = 0; j < ELEMENTCOUNT(tones); j++) {
tones[j] = Tone32(buzzerPins[j], j);
}
}
void buzzerLoop() {
for (int j = 0; j < ELEMENTCOUNT(beepingIterations); j++) {
if (beepingIterations[j] > 0 && !tones[j].isPlaying()) {
if (((beepingIterations[j] % 2) == 0)) {
Serial.print(millis()); Serial.println(" sound on");
tones[j].playNote(beepNote, beepOctave, beepMilliseconds);
} else {
Serial.print(millis()); Serial.println(" sound off");
tones[j].silence(silenceMilliseconds);
}
}
tones[j].update(); // turn off sound if duration reached.
if (!tones[j].isPlaying()) {
beepingIterations[j]--;
}
}
}
void ledLoop() {
for (int j = 0; j < ELEMENTCOUNT(ledStartedMillis); j++) {
if (ledStartedMillis[j] > 0) {
;
if (millis() - ledStartedMillis[j] >= ledDuration[j]) {
digitalWrite(ledPins[j], LOW);
ledStartedMillis[j] = 0;
}
}
}
}
void changeSummonStatus(int ref02Number, boolean warn) {
if (warn) {
digitalWrite(ledPins[ref02Number], HIGH);
beepingIterations[ref02Number] = nbSummonBeeps * 2;
ledStartedMillis[ref02Number] = millis();
ledDuration[ref02Number] = cfgSummonLedDuration;
beepNote = cfgSummonNote;
beepOctave = cfgSummonOctave;
silenceMilliseconds = cfgSummonSilenceMilliseconds;
beepMilliseconds = cfgSummonBeepMilliseconds;
} else {
digitalWrite(ledPins[ref02Number], LOW);
beepingIterations[ref02Number] = 0;
tones[ref02Number].stopPlaying();
}
}
//buzzer loop
void simulationBuzzerWorkaround() {
for (int j = 0; j <= 2; j++) {
changeSummonStatus(j, true);
int i = 0;
while (i++ < 1000) {
buzzerLoop();
}
changeSummonStatus(j, false);
i = 0;
while (i++ < 2000) {
buzzerLoop();
}
}
}