// sketch for Active Buzzer
#include "ThreadHandler.h"
#define SPEAKER_PIN 8
enum State {BOOT, READY, RECORDING, FINISHED_RECORDING};
bool boot = true;
int recording = 0; // 0: not started, 1: recording, 2: finished recording
int count = 0;
/**
Set up the Arduino board and initialize Serial communication
*/
void setup() {
Serial.begin(9600);
pinMode(SPEAKER_PIN, OUTPUT);
}
/**
The main game loop
*/
void loop() {
if (boot) {
boot = false;
playPowerOn();
delay(3000);
}
count = count + 1;
if (count == 10) {
recording = 1;
} else if (count == 20) {
recording = 2;
}
Serial.print("count: ");
Serial.print(count);
Serial.print(", recording: ");
Serial.println(recording);
if (recording == 0) {
playPulse(2000);
} else if (recording == 1) {
playPulse(600);
} else {
playSOS();
delay(2000);
}
}
/**
Plays a hooray sound whenever the user finishes a level
*/
void playPulse(int ms) {
tone(SPEAKER_PIN, 500, 200);
delay(200);
noTone(SPEAKER_PIN);
delay(ms);
}
/**
Plays a hooray sound whenever the user finishes a level
*/
void playPowerOn() {
for (int i = 0; i <= 2; i++) {
tone(SPEAKER_PIN, 256, 1000);
delay(500);
noTone(SPEAKER_PIN);
delay(500);
}
tone(SPEAKER_PIN, 500, 1000);
delay(1000);
noTone(SPEAKER_PIN);
delay(200);
noTone(SPEAKER_PIN);
}
/**
Plays a hooray sound whenever the user finishes a level
*/
void playSOS() {
for (int i = 0; i <= 2; i++) {
tone(SPEAKER_PIN, 500, 100);
delay(100);
noTone(SPEAKER_PIN);
delay(100);
}
delay(300);
for (int i = 0; i <= 1; i++) {
tone(SPEAKER_PIN, 500, 500);
delay(500);
noTone(SPEAKER_PIN);
delay(200);
}
delay(300);
for (int i = 0; i <= 2; i++) {
tone(SPEAKER_PIN, 500, 100);
delay(100);
noTone(SPEAKER_PIN);
delay(100);
}
noTone(SPEAKER_PIN);
}