// Version 0.1
#define NUM_BUTTONS 2
#define NUM_LEDS 4
#define PIEZO_PIN 8
#include "KTS_Button.h"
KTS_Button buttons[NUM_BUTTONS] = { 7, 6 };
int LEDs[NUM_LEDS] = { 9, 10, 11, 12 };
int usedIndex[NUM_LEDS] = { -1, -1, -1, -1 };
void addUsedIndexes(int pinIndex) {
for (int i = 0; i < NUM_LEDS; i++) {
if (usedIndex[i] == -1) {
usedIndex[i] = pinIndex;
return;
}
}
}
void resetUsedIndexes() {
for (int i = 0; i < NUM_LEDS; i++)
usedIndex[i] = -1;
}
bool checkIndexUsed(int pinIndex) {
for (int i = 0; i < NUM_LEDS; i++) {
if (usedIndex[i] == pinIndex)
return true;
}
return false;
}
int getUnusedIndex() {
int pinIndex = random(0, NUM_LEDS);
while(checkIndexUsed(pinIndex)) {
pinIndex = random(0, NUM_LEDS);
}
addUsedIndexes(pinIndex);
return pinIndex;
}
void lightSequence(unsigned long min, unsigned long max) {
int randPinIndex = getUnusedIndex();
unsigned long randTime = random(min, max);
digitalWrite(LEDs[randPinIndex], HIGH);
delay(randTime);
digitalWrite(LEDs[randPinIndex], LOW);
Serial.print(F("LED "));
Serial.print(randPinIndex);
Serial.print(F(" was on for "));
Serial.print(randTime);
Serial.print(F(" millis"));
Serial.println();
}
void beep(unsigned int freq, unsigned long duration, unsigned long pause) {
tone(PIEZO_PIN, freq, duration);
delay(pause);
}
void toneSequence() {
for (int i = 0; i < 3; i++) {
beep(100, 500, 1000); // freq, duration, pause
}
beep(1000, 200, 1000);
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < NUM_LEDS; i++)
pinMode(LEDs[i], OUTPUT);
}
void loop() {
randomSeed(analogRead(A0));
for (int i = 0; i < NUM_BUTTONS; i++) {
if (buttons[i].read() == SINGLE_PRESS) {
switch(i) {
case 0:
toneSequence();
for (int j = 0; j < 4; j++)
lightSequence(500, 3000); // Call lightSequence with the min and max delay times
resetUsedIndexes();
break;
case 1:
toneSequence();
for (int j = 0; j < 4; j++)
lightSequence(50, 7500); // same lightSequence call with new delay min/maxes
resetUsedIndexes();
break;
}
}
}
}