ArduinoProjects/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.ino

143 lines
2.8 KiB
C++
Raw Blame History

/*
Name: Babito2Anniversary.ino
Created: 01/01/2021 15:36:29
Author: Lorenzo Dellac<61>
*/
#include <Servo.h>
const int buzzerPin = 6;
const int servoPin = 9;
const int pause = 0;
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 494;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
const int alpiniNotes[] =
{
c,g,e,e,c,g,e,e,e,d,e,f,d,pause,
d,e,f,f,f,g,a,a,a,g,f,e,pause,g,g,e,pause,
c,g,e,e,c,g,e,e,e,d,e,f,d,pause,
d,e,f,f,f,g,a,a,a,g,f,e,pause,g,g,e,pause,
e,f,g,a,a,pause,cH,b,a,g,g,pause,e,f,g,a,a,cH,cH,b,a,g,g,pause,
e,f,g,a,a,pause,cH,b,a,g,g,pause,e,f,g,a,a,b,b,cH,pause,
pause
};
const int alpiniDurations[] =
{
500,500,250,250,250,250,250,125,125,250,250,500,500,1000,
500,500,250,250,250,250,250,125,125,250,250,500,50,375,125,500,1000,
500,500,250,250,250,250,250,125,125,250,250,500,500,1000,
500,500,250,250,250,250,250,125,125,250,250,500,50,375,125,500,1000,
250,250,250,500,500,250,250,250,250,500,500,250,250,250,250,500,500,375,125,250,250,500,500,500,
250,250,250,500,500,250,250,250,250,500,500,250,250,250,250,500,500,500,500,1000,1000,
10000
};
int alpiniMusicSize = 0;
int musicPosition = 0;
unsigned long currentMillis;
unsigned long lastMillisNote = 0;
unsigned long lastMillisServo = 0;
Servo servo;
int servoAngle = 90;
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(servoPin, OUTPUT);
servo.attach(servoPin);
servo.write(servoAngle);
currentMillis = millis();
lastMillisNote = millis();
alpiniMusicSize = ((sizeof(alpiniNotes) / sizeof(alpiniNotes[0])));
}
void loop() {
openLid(0.7f);
playMusic(0.7f);
currentMillis = millis();
}
void openLid(float speed)
{
if (currentMillis >= lastMillisServo + (100.0 * 1/speed))
{
if (servoAngle >= 0)
{
servoAngle--;
}
if (servoAngle > 0)
{
servo.write(servoAngle);
}
else
{
servo.detach();
}
lastMillisServo = millis();
}
}
void playMusic(float speed)
{
if (currentMillis >= lastMillisNote + alpiniDurations[musicPosition - 1] * 1 / speed)
{
noTone(buzzerPin);
if (!(alpiniNotes[musicPosition] == pause))
{
tone(buzzerPin, alpiniNotes[musicPosition], (alpiniDurations[musicPosition] * 1 / speed) - 10);
}
if (musicPosition < alpiniMusicSize)
{
musicPosition++;
}
else
{
musicPosition = 0;
}
lastMillisNote = millis();
}
}