#include "pitches.h"
#include "themes.h"
int f = 0, f2 = 911, d, startF1 = 635, startF2 = 911, EndF1 = 912, EndF2 = 634, d1 = 7, d2 = 10, horn = 250, d3 = 5;
#define Speaker 11
#define btn1 4
#define btn2 3
#define switchModeBtn 2
#define policeLeds 5
int nextAuto = 0;
unsigned long changeAfter = 9000;
unsigned long lastTimeChange = 0;
unsigned long fTimer = 0;
bool music = false;
bool musicStarted = false;
int selectedTheme = 0;
void setup()
{
Serial.begin(115200);
pinMode(btn1, INPUT_PULLUP);
pinMode(btn2, INPUT_PULLUP);
pinMode(switchModeBtn, INPUT_PULLUP);
pinMode(policeLeds, OUTPUT);
}
void loop()
{
if(music)
startMusic();
else
policeSiren();
digitalWrite(policeLeds, !musicStarted);
}
void dedectHorn()
{
dedectSwitchMode();
while (digitalRead(btn2) == LOW)
{
if(music)
{
tone(Speaker, horn);
}
else
{
noTone(Speaker);
tone(Speaker, horn);
delay(d3);
noTone(Speaker);
delay(d3);
}
}
}
void dedectSwitchMode()
{
if(digitalRead(switchModeBtn) == LOW && musicStarted == music)
{
noTone(Speaker);
while(digitalRead(switchModeBtn) == LOW);
music = !music;
// Serial.print("music switched: ");
// Serial.println(music);
}
}
void policeSiren()
{
musicStarted = false;
digitalWrite(policeLeds, HIGH);
if (f < EndF1)
{
if (f == 0 || fTimer == 0)
{
f = startF1;
fTimer = millis();
}
else if (fTimer + d <= millis())
{
f++;
fTimer = millis();
}
Serial.print("Start 1:");
Serial.println(f);
tone(Speaker, f);
if (digitalRead(btn1) == LOW)
{
f = EndF1;
f2 = EndF1;
}
if (nextAuto == 1)
d = 0;
else
d = d1;
}
else if (f2 > EndF2 && f >= EndF1)
{
// Serial.println("Start 2");
if (fTimer + d <= millis())
{
f2--;
fTimer = millis();
}
Serial.print("Start 2:");
Serial.println(f2);
tone(Speaker, f2);
if (digitalRead(btn1) == LOW)
f2 = EndF2;
if (nextAuto == 1)
d = 0;
else
d = d2;
}
else if (f2 <= EndF2 && f >= EndF1)
{
f = digitalRead(btn1) == LOW ? EndF2 : 0;
f2 = startF2;
Serial.println("Start again");
}
if(lastTimeChange + changeAfter <= millis())
{
nextAuto++;
if(nextAuto >= 2) nextAuto = 0;
// Serial.println(nextAuto);
lastTimeChange = nextAuto > 0 ? millis() - (changeAfter / 4) : millis();
}
dedectHorn();
}
void startMusic()
{
musicStarted = true;
digitalWrite(policeLeds, LOW);
Play_MarioUW();
Play_MarioUW();
Play_MarioUW();
delay(500);
// Play_Pirates();
Play_CrazyFrog();
Play_CrazyFrog();
Play_CrazyFrog();
Play_CrazyFrog();
delay(500);
// Play_Titanic();
}
void Play_Pirates()
{
for (int thisNote = 0; thisNote < (sizeof(Pirates_note)/sizeof(int)); thisNote++) {
if(!music) break;
dedectHorn();
int noteDuration = 1000 / Pirates_duration[thisNote];//convert duration to time delay
tone(Speaker, Pirates_note[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.05; //Here 1.05 is tempo, increase to play it slower
delay(pauseBetweenNotes);
noTone(Speaker); //stop music on pin 8
}
}
void Play_CrazyFrog()
{
for (int thisNote = 0; thisNote < (sizeof(CrazyFrog_note)/sizeof(int)); thisNote++) {
if(!music) break;
dedectHorn();
int noteDuration = 1000 / CrazyFrog_duration[thisNote]; //convert duration to time delay
tone(Speaker, CrazyFrog_note[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;//Here 1.30 is tempo, decrease to play it faster
delay(pauseBetweenNotes);
noTone(Speaker); //stop music on pin 8
}
}
void Play_MarioUW()
{
for (int thisNote = 0; thisNote < (sizeof(MarioUW_note)/sizeof(int)); thisNote++) {
if(!music) break;
dedectHorn();
int noteDuration = 1000 / MarioUW_duration[thisNote];//convert duration to time delay
tone(Speaker, MarioUW_note[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.80;
delay(pauseBetweenNotes);
noTone(Speaker); //stop music on pin 8
}
}
void Play_Titanic()
{
for (int thisNote = 0; thisNote < (sizeof(Titanic_note)/sizeof(int)); thisNote++) {
if(!music) break;
dedectHorn();
int noteDuration = 1000 / Titanic_duration[thisNote];//convert duration to time delay
tone(Speaker, Titanic_note[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 2.70;
delay(pauseBetweenNotes);
noTone(Speaker); //stop music on pin 8
}
}