Adding BicycleControl.ino, and the Musical Class.

This commit is contained in:
Andrew Lalis 2017-02-17 09:42:26 +01:00
parent b4546a67f2
commit cc3e1dd045
3 changed files with 121 additions and 10 deletions

View File

@ -63,13 +63,117 @@ unsigned long leftPressedTime = 0;
//Right option button.
bool rightPressed = 0;
unsigned long rightPressedTime = 0;
//Music variables.
short BPM = 100;
short Q = 60000 / BPM;
short H = 2 * Q;
short E = Q / 2;
short S = Q / 4;
short W = Q * 4;
/*
* Song file: bpm, followed by notes and a byte representing what division of a note it is.
*/
short mySong[] = {
120,
NOTE_D5, E,
NOTE_E5, E,
NOTE_F5, Q + E,
NOTE_F5, E,
NOTE_F5, E,
NOTE_G5, E,
NOTE_A5, Q + E,
NOTE_A5, E,
NOTE_A5, E,
NOTE_C5, E,//10
NOTE_G5, Q + E,
NOTE_G5, E,
NOTE_F5, E,
NOTE_E5, E,
NOTE_D5, Q + E,
NOTE_D5, E,
NOTE_D5, E,
NOTE_E5, E,
NOTE_F5, Q + E,
NOTE_F5, E,//20
NOTE_F5, E,
NOTE_G5, E,
NOTE_A5, Q + E,
NOTE_A5, E,
NOTE_A5, E,
NOTE_C5, E,
NOTE_D5, Q + E,
NOTE_D5, E,
NOTE_C5, E,
NOTE_E5, E,//30
NOTE_D5, Q + E,
NOTE_D5, E,
NOTE_D5, E,
NOTE_E5, E,
NOTE_F5, Q,
NOTE_E5, E,
NOTE_E5, E,
NOTE_D5, Q,
NOTE_C5, Q,
NOTE_B5, E,//40
NOTE_B5, E,
NOTE_A5, Q,
NOTE_G5, Q + E,
NOTE_G5, E,
NOTE_F5, E,
NOTE_A5, E,
NOTE_G5, Q + E,
NOTE_G5, Q,
NOTE_F5, S,
NOTE_E5, S,//50
NOTE_F5, Q,
NOTE_F5, S,
NOTE_E5, S,
NOTE_F5, Q,
NOTE_F5, S,
NOTE_E5, S,
NOTE_G5, E,
NOTE_F5, E,
NOTE_E5, E,
NOTE_D5, Q,//60
NOTE_D5, S,
NOTE_C5, S,
NOTE_D5, Q,
NOTE_D5, S,
NOTE_C5, S,
NOTE_D5, Q,
NOTE_C5, S,
NOTE_D5, S,
NOTE_E5, E,
NOTE_F5, E,//70
NOTE_C5, E,
NOTE_D5, Q,
NOTE_D5, S,
NOTE_E5, S,
NOTE_F5, Q,
NOTE_F5, S,
NOTE_G5, S,
NOTE_A5, Q,
NOTE_E5, S,
NOTE_F5, S,//80
NOTE_G5, E,
NOTE_F5, E,
NOTE_E5, E,
NOTE_D5, Q,
NOTE_D5, S,
NOTE_C5, S,
NOTE_D5, Q,
NOTE_D5, S,
NOTE_C5, S,
NOTE_D5, Q,//90
NOTE_C5, S,
NOTE_D5, S,
NOTE_E5, E,
NOTE_F5, E,
NOTE_C5, E,
NOTE_D5, Q + E,
NOTE_D5, Q,
NOTE_REST, E,
NOTE_D5, Q,
NOTE_REST, E,//100
NOTE_D5, Q,
NOTE_REST, E,
NOTE_D5, Q,
NOTE_REST, E
};
const short beep1[] = {
100,
@ -385,10 +489,13 @@ void setup() {
startupScreen();
drawMainScreen();
updateAll();
Musical::setBuzzerPin(BUZZER);
Musical::playSequence(mySong, 104);
}
void loop() {
updateBlinkerStatus();
updateInputs();
Musical::update();
}

View File

@ -39,15 +39,19 @@ void Musical::playSequence(short * sequence, int length)
songLength = length;
currentNote = 0;
lastNoteTime = 0;
isPlaying = true;
}
//Updates the playing song to allow multitasking.
void Musical::update()
{
if (lastNoteTime > millis() && currentNote < songLength) {
if (isPlaying && lastNoteTime > millis() && currentNote < songLength) {
//It is necessary to play the next note.
playNote(currentSong[currentNote * 2 + 1], currentSong[currentNote * 2 + 2]);
lastNoteTime = millis();
lastNoteTime = millis() + currentSong[currentNote * 2 + 2];
currentNote++;
}
if (currentNote == songLength) {
isPlaying = false;
}
}

View File

@ -106,7 +106,7 @@ private:
static short BPM, Q, H, E, S, W, pin;
static short* currentSong;
static int songLength;
static bool isPlaying = false;
static bool isPlaying;
static int currentNote;
static unsigned long lastNoteTime;
};