4
0
Fork 0
mirror of https://github.com/zzzzDev4/IAS-Better-Tea.git synced 2025-04-21 07:31:20 +02:00

Better sound, removed potentiometer

This commit is contained in:
zzzz 2024-05-12 19:57:32 +02:00
parent b4e402c601
commit a6c5005eb0
6 changed files with 171 additions and 69 deletions

54
src/SmartSound.cpp Normal file
View file

@ -0,0 +1,54 @@
#include "SmartSound.hpp"
#include "nevergonna.h"
SmartSound::SmartSound(int buzzerPin)
{
m_buzzerPin = buzzerPin;
}
void SmartSound::play(Sound sound, int tempo)
{
switch (sound)
{
case NeverGonna:
playSound(nevergonna, nevergonna_notes, tempo);
break;
case Bing:
break;
}
}
void SmartSound::playSound(int melody[], int notes, int tempo)
{
// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
// iterate over the notes of the melody.
// Remember, the array is twice the number of notes (notes + durations)
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2)
{
// calculates the duration of each note
divider = melody[thisNote + 1];
if (divider > 0)
{
// regular note, just proceed
noteDuration = (wholenote) / divider;
}
else if (divider < 0)
{
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(m_buzzerPin, melody[thisNote], noteDuration * 0.9);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
noTone(m_buzzerPin);
}
}

20
src/SmartSound.hpp Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#include <Arduino.h>
#include "pitches.h"
class SmartSound
{
public:
enum Sound {
NeverGonna,
Bing,
};
public:
SmartSound(int buzzerPin);
void play(Sound sound, int tempo = 144);
private:
int m_buzzerPin;
void playSound(int melody[], int notes, int tempo = 114);
};

View file

@ -1,6 +1,5 @@
#include "main.hpp" #include "main.hpp"
#include "secrets.hpp" #include "secrets.hpp"
#include "song.h"
#include <FastLED.h> #include <FastLED.h>
#define WIRE Wire #define WIRE Wire
@ -36,6 +35,8 @@ SmartDial smartDial(POTENTIOMETER_PIN);
SmartDisplay smartDisplay(&display); SmartDisplay smartDisplay(&display);
TeaNetworking teaNet; TeaNetworking teaNet;
SmartSound sound(BUZZER_PIN);
SmartButton buttonLeft(TACTILE_BTN_LEFT); SmartButton buttonLeft(TACTILE_BTN_LEFT);
SmartButton buttonRight(TACTILE_BTN_RIGHT); SmartButton buttonRight(TACTILE_BTN_RIGHT);
@ -122,8 +123,6 @@ void loop()
{ {
smartDisplay.printWaitForRFIDScreen(); smartDisplay.printWaitForRFIDScreen();
// playSong();
// this blocks execution until a tag has been detected // this blocks execution until a tag has been detected
String uid = smartRFID.readTag(); String uid = smartRFID.readTag();
@ -155,7 +154,7 @@ void loop()
} }
case DISPLAY_SCANNED_TEA_CONFIG: case DISPLAY_SCANNED_TEA_CONFIG:
{ {
int dir = smartDial.smoothEdit(&currentTea.m_steepingSeconds, 0, 6000); // int dir = smartDial.smoothEdit(&currentTea.m_steepingSeconds, 0, 6000);
if (buttonLeft.isJustPressed()) if (buttonLeft.isJustPressed())
{ {
@ -165,7 +164,15 @@ void loop()
{ {
currentTea.m_steepingSeconds += 10; currentTea.m_steepingSeconds += 10;
} }
smartDisplay.printTeaConfigScreen(currentTea.m_teaName, currentTea.m_steepingSeconds, dir); smartDisplay.printTeaConfigScreen(currentTea.m_teaName, currentTea.m_steepingSeconds, 0);
if (isBtnPressed() && currentTea.m_steepingSeconds <= 0)
{
// rickroll
Serial.println("Easter");
sound.play(SmartSound::NeverGonna, 155);
break;
}
// change state if start pressed // change state if start pressed
if (isBtnPressed()) if (isBtnPressed())
@ -201,27 +208,6 @@ void loop()
}; };
} }
void playSong()
{
int size = sizeof(durations) / sizeof(int);
for (int note = 0; note < size; note++)
{
// to calculate the note duration, take one second divided by the note type.
// e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int duration = 1000 / durations[note];
tone(BUZZER_PIN, melody[note], duration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = duration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(BUZZER_PIN);
}
}
void toggle_led() void toggle_led()
{ {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));

View file

@ -17,6 +17,7 @@
#include "TeaNetworking.hpp" #include "TeaNetworking.hpp"
#include "TeaData.hpp" #include "TeaData.hpp"
#include "SmartButton.hpp" #include "SmartButton.hpp"
#include "SmartSound.hpp"
void playSong(); void playSong();
bool toggle_led(void *); bool toggle_led(void *);

84
src/nevergonna.h Normal file
View file

@ -0,0 +1,84 @@
#pragma once
#include "pitches.h"
// notes of the moledy followed by the duration.
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
int nevergonna[] = {
// Never Gonna Give You Up - Rick Astley
// Score available at https://musescore.com/chlorondria_5/never-gonna-give-you-up_alto-sax
// Arranged by Chlorondria
/*
NOTE_D5, -4, NOTE_E5, -4, NOTE_A4, 4, // 1
NOTE_E5, -4, NOTE_FS5, -4, NOTE_A5, 16, NOTE_G5, 16, NOTE_FS5, 8,
NOTE_D5, -4, NOTE_E5, -4, NOTE_A4, 2,
NOTE_A4, 16, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 8, NOTE_D5, 16,
NOTE_D5, -4, NOTE_E5, -4, NOTE_A4, 4, // repeat from 1
NOTE_E5, -4, NOTE_FS5, -4, NOTE_A5, 16, NOTE_G5, 16, NOTE_FS5, 8,
NOTE_D5, -4, NOTE_E5, -4, NOTE_A4, 2,
NOTE_A4, 16, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 8, NOTE_D5, 16,
REST, 4, NOTE_B4, 8, NOTE_CS5, 8, NOTE_D5, 8, NOTE_D5, 8, NOTE_E5, 8, NOTE_CS5, -8,
NOTE_B4, 16, NOTE_A4, 2, REST, 4,
REST, 8, NOTE_B4, 8, NOTE_B4, 8, NOTE_CS5, 8, NOTE_D5, 8, NOTE_B4, 4, NOTE_A4, 8, // 7
NOTE_A5, 8, REST, 8, NOTE_A5, 8, NOTE_E5, -4, REST, 4,
NOTE_B4, 8, NOTE_B4, 8, NOTE_CS5, 8, NOTE_D5, 8, NOTE_B4, 8, NOTE_D5, 8, NOTE_E5, 8, REST, 8,
REST, 8, NOTE_CS5, 8, NOTE_B4, 8, NOTE_A4, -4, REST, 4,
REST, 8, NOTE_B4, 8, NOTE_B4, 8, NOTE_CS5, 8, NOTE_D5, 8, NOTE_B4, 8, NOTE_A4, 4,
NOTE_E5, 8, NOTE_E5, 8, NOTE_E5, 8, NOTE_FS5, 8, NOTE_E5, 4, REST, 4,*/
NOTE_D5, 2, NOTE_E5, 8, NOTE_FS5, 8, NOTE_D5, 8, // 13
NOTE_E5, 8, NOTE_E5, 8, NOTE_E5, 8, NOTE_FS5, 8, NOTE_E5, 4, NOTE_A4, 4,
REST, 2, NOTE_B4, 8, NOTE_CS5, 8, NOTE_D5, 8, NOTE_B4, 8,
REST, 8, NOTE_E5, 8, NOTE_FS5, 8, NOTE_E5, -4, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_FS5, -8, NOTE_FS5, -8, NOTE_E5, -4, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_E5, -8, NOTE_E5, -8, NOTE_D5, -8, NOTE_CS5, 16, NOTE_B4, -8, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16, // 18
NOTE_D5, 4, NOTE_E5, 8, NOTE_CS5, -8, NOTE_B4, 16, NOTE_A4, 8, NOTE_A4, 8, NOTE_A4, 8,
NOTE_E5, 4, NOTE_D5, 2, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_FS5, -8, NOTE_FS5, -8, NOTE_E5, -4, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_A5, 4, NOTE_CS5, 8, NOTE_D5, -8, NOTE_CS5, 16, NOTE_B4, 8, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_D5, 4, NOTE_E5, 8, NOTE_CS5, -8, NOTE_B4, 16, NOTE_A4, 4, NOTE_A4, 8, // 23
NOTE_E5, 4, NOTE_D5, 2, REST, 4, /*
REST, 8, NOTE_B4, 8, NOTE_D5, 8, NOTE_B4, 8, NOTE_D5, 8, NOTE_E5, 4, REST, 8,
REST, 8, NOTE_CS5, 8, NOTE_B4, 8, NOTE_A4, -4, REST, 4,
REST, 8, NOTE_B4, 8, NOTE_B4, 8, NOTE_CS5, 8, NOTE_D5, 8, NOTE_B4, 8, NOTE_A4, 4,
REST, 8, NOTE_A5, 8, NOTE_A5, 8, NOTE_E5, 8, NOTE_FS5, 8, NOTE_E5, 8, NOTE_D5, 8,
REST, 8, NOTE_A4, 8, NOTE_B4, 8, NOTE_CS5, 8, NOTE_D5, 8, NOTE_B4, 8, // 29
REST, 8, NOTE_CS5, 8, NOTE_B4, 8, NOTE_A4, -4, REST, 4,
NOTE_B4, 8, NOTE_B4, 8, NOTE_CS5, 8, NOTE_D5, 8, NOTE_B4, 8, NOTE_A4, 4, REST, 8,
REST, 8, NOTE_E5, 8, NOTE_E5, 8, NOTE_FS5, 4, NOTE_E5, -4,
NOTE_D5, 2, NOTE_D5, 8, NOTE_E5, 8, NOTE_FS5, 8, NOTE_E5, 4,
NOTE_E5, 8, NOTE_E5, 8, NOTE_FS5, 8, NOTE_E5, 8, NOTE_A4, 8, NOTE_A4, 4,
REST, -4, NOTE_A4, 8, NOTE_B4, 8, NOTE_CS5, 8, NOTE_D5, 8, NOTE_B4, 8, // 35
REST, 8, NOTE_E5, 8, NOTE_FS5, 8, NOTE_E5, -4, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_FS5, -8, NOTE_FS5, -8, NOTE_E5, -4, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_E5, -8, NOTE_E5, -8, NOTE_D5, -8, NOTE_CS5, 16, NOTE_B4, 8, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_D5, 4, NOTE_E5, 8, NOTE_CS5, -8, NOTE_B4, 16, NOTE_A4, 4, NOTE_A4, 8,
NOTE_E5, 4, NOTE_D5, 2, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16, // 40
NOTE_FS5, -8, NOTE_FS5, -8, NOTE_E5, -4, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_A5, 4, NOTE_CS5, 8, NOTE_D5, -8, NOTE_CS5, 16, NOTE_B4, 8, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_D5, 4, NOTE_E5, 8, NOTE_CS5, -8, NOTE_B4, 16, NOTE_A4, 4, NOTE_A4, 8,
NOTE_E5, 4, NOTE_D5, 2, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_FS5, -8, NOTE_FS5, -8, NOTE_E5, -4, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16, // 45
NOTE_A5, 4, NOTE_CS5, 8, NOTE_D5, -8, NOTE_CS5, 16, NOTE_B4, 8, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_D5, 4, NOTE_E5, 8, NOTE_CS5, -8, NOTE_B4, 16, NOTE_A4, 4, NOTE_A4, 8,
NOTE_E5, 4, NOTE_D5, 2, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_FS5, -8, NOTE_FS5, -8, NOTE_E5, -4, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16, // 45
NOTE_A5, 4, NOTE_CS5, 8, NOTE_D5, -8, NOTE_CS5, 16, NOTE_B4, 8, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 16, NOTE_B4, 16,
NOTE_D5, 4, NOTE_E5, 8, NOTE_CS5, -8, NOTE_B4, 16, NOTE_A4, 4, NOTE_A4, 8,
NOTE_E5, 4, NOTE_D5, 2, REST, 4*/};
// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int nevergonna_notes = sizeof(nevergonna) / sizeof(nevergonna[0]) / 2;

View file

@ -1,43 +0,0 @@
#include "pitches.h"
int melody[] = {
REST, REST, REST, NOTE_DS4,
NOTE_E4, REST, NOTE_FS4, NOTE_G4, REST, NOTE_DS4,
NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_B4,
NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_D4,
NOTE_E4, REST, REST, NOTE_DS4,
NOTE_E4, REST, NOTE_FS4, NOTE_G4, REST, NOTE_DS4,
NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_C5, NOTE_B4, NOTE_G4, NOTE_B4, NOTE_E5,
NOTE_DS5,
NOTE_D5, REST, REST, NOTE_DS4,
NOTE_E4, REST, NOTE_FS4, NOTE_G4, REST, NOTE_DS4,
NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_B4,
NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_D4,
NOTE_E4, REST,
REST, NOTE_E5, NOTE_D5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4,
NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4,
NOTE_G4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4
};
int durations[] = {
2, 4, 8, 8,
4, 8, 8, 4, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
2, 16, 16, 16, 16,
2, 4, 8, 4,
4, 8, 8, 4, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
1,
2, 4, 8, 8,
4, 8, 8, 4, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
2, 16, 16, 16, 16,
4, 4,
4, 8, 8, 8, 8, 8, 8,
16, 8, 16, 8, 16, 8, 16, 8,
16, 16, 16, 16, 16, 2
};