Permalink
Browse files

Criar biblioteca MusicPlayer para tocar sons

  • Loading branch information...
eduardoweiland committed Jan 13, 2018
1 parent 796ec38 commit 8cc6dd42ed49d8c6b172840ae588b72766309236
Showing with 98 additions and 74 deletions.
  1. +65 −0 lib/MusicPlayer/MusicPlayer.cpp
  2. +22 −0 lib/MusicPlayer/MusicPlayer.h
  3. +11 −74 src/Placarduino.ino
@@ -0,0 +1,65 @@
#include <Arduino.h>
#include <Tone.h>

#include "MusicPlayer.h"

MusicPlayer::MusicPlayer(Tone *tone)
{
this->buzzer = tone;
}

void MusicPlayer::playNote(uint16_t frequency, uint32_t duration)
{
this->buzzer->play(frequency, duration * 0.85);
delay(duration);
}

void MusicPlayer::pause(uint32_t duration)
{
delay(duration);
}

void MusicPlayer::cMajorScale()
{
int i;
int notes[] = { NOTE_C6, NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6, NOTE_C7 };

for (i = 0; i < 8; ++i) {
this->playNote(notes[i], 35 + (i * 5));
}
}

void MusicPlayer::superMarioTheme()
{
this->playNote(NOTE_E6, 140);
this->playNote(NOTE_E6, 140);
this->pause(140);
this->playNote(NOTE_E6, 140);
this->pause(140);
this->playNote(NOTE_C6, 140);
this->playNote(NOTE_E6, 260);
this->playNote(NOTE_G6, 260);
this->pause(260);
this->playNote(NOTE_G5, 260);
}

void MusicPlayer::sweetChildOMine()
{
int i, j;
int firstNote[] = { NOTE_CS5, NOTE_DS5, NOTE_FS5 };

for (i = 0; i < 3; ++i) {
for (j = 0; j < 2; ++j) {
this->playNote(firstNote[i], 260);
this->playNote(NOTE_CS6, 260);
this->playNote(NOTE_GS5, 260);
this->playNote(NOTE_FS5, 260);
this->playNote(NOTE_FS6, 260);
this->playNote(NOTE_GS5, 260);
this->playNote(NOTE_F6, 260);
this->playNote(NOTE_GS5, 260);
}
}

this->playNote(NOTE_CS5, 260);
}
@@ -0,0 +1,22 @@
#ifndef PLACARDUINO_MUSIC_PLAYER_H
#define PLACARDUINO_MUSIC_PLAYER_H

#include <Tone.h>

class MusicPlayer
{
public:
MusicPlayer(Tone *buzzer);

void cMajorScale();
void superMarioTheme();
void sweetChildOMine();

void playNote(uint16_t frequency, uint32_t duration);
void pause(uint32_t duration);

private:
Tone *buzzer;
};

#endif // PLACARDUINO_MUSIC_PLAYER_H
@@ -3,6 +3,7 @@
#include <Tone.h>

#include "LcdBigNumbers.h"
#include "MusicPlayer.h"

/*
* CONSTANTES
@@ -42,13 +43,15 @@ int addPlayer2ButtonState = LOW;

// Objeto para controle de um buzzer
Tone buzzer;
MusicPlayer musicPlayer(&buzzer);


/*
* SETUP
*/

void setup() {
void setup()
{
setupInputs();
setupLCD();
setupBuzzer();
@@ -66,7 +69,8 @@ void setup() {
* LOOP
*/

void loop() {
void loop()
{
checkButtons();
}

@@ -109,72 +113,11 @@ void printWelcome()
lcd.print("a.k.a. DAMN");
}

// Escala de Dó maior
void playWelcome()
{
// Básico
int i;
int notes[] = { NOTE_C6, NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6, NOTE_C7 };

for (i = 0; i < 8; ++i) {
buzzer.play(notes[i], 30 + (i * 5));
delay(35 + (i * 5));
}
musicPlayer.cMajorScale();
}

// Super Mário
//void playWelcome()
//{
// buzzer.play(NOTE_E6, 120);
// delay(140);
// buzzer.play(NOTE_E6, 120);
// delay(140);
// delay(140);
// buzzer.play(NOTE_E6, 120);
// delay(140);
// delay(140);
// buzzer.play(NOTE_C6, 120);
// delay(140);
// buzzer.play(NOTE_E6, 240);
// delay(260);
// buzzer.play(NOTE_G6, 240);
// delay(260);
// delay(260);
// buzzer.play(NOTE_G5, 240);
// delay(260);
//}

// Sweet Child O' Mine
//void playWelcome()
//{
// int i, j;
// int firstNote[] = { NOTE_CS5, NOTE_DS5, NOTE_FS5 };
//
// for (i = 0; i < 3; ++i) {
// for (j = 0; j < 2; ++j) {
// buzzer.play(firstNote[i], 240);
// delay(260);
// buzzer.play(NOTE_CS6, 240);
// delay(260);
// buzzer.play(NOTE_GS5, 240);
// delay(260);
// buzzer.play(NOTE_FS5, 240);
// delay(260);
// buzzer.play(NOTE_FS6, 240);
// delay(260);
// buzzer.play(NOTE_GS5, 240);
// delay(260);
// buzzer.play(NOTE_F6, 240);
// delay(260);
// buzzer.play(NOTE_GS5, 240);
// delay(260);
// }
// }
//
// buzzer.play(NOTE_CS5, 240);
// delay(260);
//}

void checkScoreInput(int inputPin, int *stateStore, int *scoreStore, int op, void(*feedbackFunction)())
{
int newButtonState = digitalRead(inputPin);
@@ -224,18 +167,12 @@ void printScore()

void playFeedbackPositive()
{
buzzer.play(NOTE_C6, 120);
delay(140);

buzzer.play(NOTE_C7, 420);
delay(500);
musicPlayer.playNote(NOTE_C6, 140);
musicPlayer.playNote(NOTE_C7, 480);
}

void playFeedbackNegative()
{
buzzer.play(NOTE_C7, 120);
delay(140);

buzzer.play(NOTE_C6, 420);
delay(500);
musicPlayer.playNote(NOTE_C7, 140);
musicPlayer.playNote(NOTE_C6, 480);
}

0 comments on commit 8cc6dd4

Please sign in to comment.