summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEnricoGuccii <partyka.003@gmail.com>2025-05-11 13:55:44 +0200
committerEnricoGuccii <partyka.003@gmail.com>2025-05-11 13:55:44 +0200
commitcf9a1f07ed519cadd58e38794616d0a9596be4ae (patch)
tree11f7c879517e5e861e0914ddee317ec04ce8690b
parent7da514aa6ec19df49828fd87013ba284f338fbdd (diff)
add buzzer
-rw-r--r--include/settings.h2
-rw-r--r--lib/buzzer/buzzer.cpp37
-rw-r--r--lib/buzzer/buzzer.h14
-rw-r--r--lib/melodies/melodies.cpp60
-rw-r--r--lib/melodies/melodies.h10
-rw-r--r--lib/rgb/rgb.cpp25
-rw-r--r--src/main.cpp27
7 files changed, 151 insertions, 24 deletions
diff --git a/include/settings.h b/include/settings.h
index 4fb4a48..886513c 100644
--- a/include/settings.h
+++ b/include/settings.h
@@ -21,4 +21,4 @@ const int BLUE_PIN = 6;
// const int IN4 = D4;
// BUZZER
-// const int BUZZER_PIN = 3; \ No newline at end of file
+const int BUZZER_PIN = 13;
diff --git a/lib/buzzer/buzzer.cpp b/lib/buzzer/buzzer.cpp
new file mode 100644
index 0000000..a83ec22
--- /dev/null
+++ b/lib/buzzer/buzzer.cpp
@@ -0,0 +1,37 @@
+#include <Arduino.h>
+#include "settings.h"
+#include "buzzer.h"
+
+#define BUZZER_CHANNEL 3
+#define BUZZER_RESOLUTION 8
+
+BUZZER::BUZZER()
+{
+ ledcSetup(BUZZER_CHANNEL, 2000, BUZZER_RESOLUTION);
+ ledcAttachPin(BUZZER_PIN, BUZZER_CHANNEL);
+}
+
+void BUZZER::playTone(uint16_t frequency, uint32_t duration)
+{
+ ledcWriteTone(BUZZER_CHANNEL, frequency);
+ if (duration > 0)
+ {
+ delay(duration);
+ stop();
+ }
+}
+
+void BUZZER::playMelody(const uint16_t melody[][2], size_t length)
+{
+
+ for (size_t i = 0; i < length; i++)
+ {
+ playTone(melody[i][0], melody[i][1]);
+ delay(50);
+ }
+}
+
+void BUZZER::stop()
+{
+ ledcWriteTone(BUZZER_CHANNEL, 0);
+} \ No newline at end of file
diff --git a/lib/buzzer/buzzer.h b/lib/buzzer/buzzer.h
new file mode 100644
index 0000000..f71a90a
--- /dev/null
+++ b/lib/buzzer/buzzer.h
@@ -0,0 +1,14 @@
+#include <Arduino.h>
+#include "settings.h"
+
+#pragma once
+
+class BUZZER
+{
+public:
+ BUZZER();
+
+ void playTone(uint16_t frequency, uint32_t duration = 0);
+ void stop();
+ void playMelody(const uint16_t melody[][2], size_t length);
+};
diff --git a/lib/melodies/melodies.cpp b/lib/melodies/melodies.cpp
new file mode 100644
index 0000000..73d343d
--- /dev/null
+++ b/lib/melodies/melodies.cpp
@@ -0,0 +1,60 @@
+#include <Arduino.h>
+#include "melodies.h"
+
+const uint16_t melody[][2] = {
+ {262, 300}, // C4,
+ {294, 300}, // D4,
+ {330, 300}, // E4,
+ {349, 300}, // F4,
+ {392, 300}, // G4,
+ {440, 300}, // A4,
+ {494, 300}, // B4,
+ {523, 300} // C5,
+};
+
+
+const uint16_t surprise[][2] = {
+ {523, 300}, // C5
+ {587, 300}, // D5
+ {659, 300}, // E5
+ {698, 300}, // F5
+ {784, 300}, // G5
+ {880, 300}, // A5
+ {987, 300}, // B5
+ {1047, 300} // C6
+};
+
+const uint16_t alaarm[][2] = {
+ {880, 100}, // A5
+ {987, 100}, // B5
+ {1047, 100}, // C6
+ {880, 100}, // A5
+ {987, 100}, // B5
+ {1047, 100}, // C6
+ {880, 100}, // A5
+ {987, 100}, // B5
+ {1047, 100} // C6
+};
+
+const uint16_t anger[][2] = {
+ {440, 100}, // A4
+ {523, 100}, // C5
+ {587, 100}, // D5
+ {659, 100}, // E5
+ {698, 100}, // F5
+ {784, 100}, // G5
+ {880, 100}, // A5
+ {987, 100}, // B5
+ {1047, 100} // C6
+};
+
+const uint16_t happy[][2] = {
+ {262, 300}, // C4
+ {294, 300}, // D4
+ {330, 300}, // E4
+ {349, 300}, // F4
+ {392, 300}, // G4
+ {440, 300}, // A4
+ {494, 300}, // B4
+ {523, 300} // C5
+};
diff --git a/lib/melodies/melodies.h b/lib/melodies/melodies.h
new file mode 100644
index 0000000..029c8fd
--- /dev/null
+++ b/lib/melodies/melodies.h
@@ -0,0 +1,10 @@
+#include <Arduino.h>
+
+#pragma once
+
+const uint16_t happy[][2];
+const uint16_t anger[][2];
+const uint16_t alaarm[][2];
+const uint16_t surprise[][2];
+
+
diff --git a/lib/rgb/rgb.cpp b/lib/rgb/rgb.cpp
index 174a9e5..249e7f0 100644
--- a/lib/rgb/rgb.cpp
+++ b/lib/rgb/rgb.cpp
@@ -3,15 +3,12 @@
#include "rgb.h"
#include <math.h>
+#define RED_CHANNEL 0
+#define GREEN_CHANNEL 1
+#define BLUE_CHANNEL 2
-// Kanały PWM (muszą być unikalne)
-#define RED_CHANNEL 0
-#define GREEN_CHANNEL 1
-#define BLUE_CHANNEL 2
-
-// Częstotliwość PWM i rozdzielczość
-#define PWM_FREQ 5000
-#define PWM_RES 10 // 10-bit (0–1023)
+#define PWM_FREQ 5000
+#define PWM_RES 10
int RGB::scaleColor(uint8_t val)
{
@@ -44,7 +41,6 @@ void RGB::setColorRGB(uint8_t red, uint8_t green, uint8_t blue, bool saveColor =
}
}
-
void RGB::fadeColor(uint8_t red, uint8_t green, uint8_t blue, int duration)
{
int startR = currentR;
@@ -77,8 +73,8 @@ void RGB::blink(int cycles, int interval)
void RGB::breathe(int cycles, int period)
{
- const int steps = 128;
- int stepDelay = period / (steps * 2);
+ const int steps = 128;
+ int stepDelay = period / (steps * 2);
uint8_t baseR = currentR;
uint8_t baseG = currentG;
@@ -93,7 +89,6 @@ void RGB::breathe(int cycles, int period)
delay(stepDelay);
}
-
for (int i = steps; i >= 0; --i)
{
float scale = (float)i / steps;
@@ -105,7 +100,6 @@ void RGB::breathe(int cycles, int period)
setColorRGB(baseR, baseG, baseB);
}
-
void RGB::rainbowCycle(int duration, float speed)
{
// speed (0.1 – slow, 1.0 – normal, 2.0 – fast)
@@ -114,9 +108,9 @@ void RGB::rainbowCycle(int duration, float speed)
for (int i = 0; i < steps; i++)
{
float angle = i * speed * 0.024;
- int red = sin(angle) * 127 + 128;
+ int red = sin(angle) * 127 + 128;
int green = sin(angle + 2) * 127 + 128;
- int blue = sin(angle + 4) * 127 + 128;
+ int blue = sin(angle + 4) * 127 + 128;
setColorRGB(red, green, blue, false);
delay(duration / steps);
@@ -124,4 +118,3 @@ void RGB::rainbowCycle(int duration, float speed)
setColorRGB(currentR, currentG, currentB, false);
}
-
diff --git a/src/main.cpp b/src/main.cpp
index c48769c..736b949 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2,26 +2,39 @@
#include "settings.h"
#include "rgb.h"
#include "oled.h"
+#include "Buzzer.h"
RGB rgb;
+BUZZER buzzer;
+
void setup()
-{
-}
+{}
void loop()
{
- // rgb.setColorRGB(255, 0, 0, true);
+
+ // buzzer.playMelody(happy, 8);
// delay(1000);
- // rgb.fadeColor(0, 255, 0, 1000);
+ // buzzer.playMelody(anger, 8);
// delay(1000);
- // rgb.blink(3, 500);
+ // buzzer.playMelody(alaarm, 9);
// delay(1000);
- // rgb.breathe(4, 1000);
+ // buzzer.playMelody(surprise, 9);
+ // delay(1000);
+
+ //buzzer.playMelody(melody, 8);
+ // rgb.setColorRGB(255, 0, 0, true);
+ // delay(1000);
+ // rgb.fadeColor(0, 255, 0, 1000);
+ // delay(1000);
+ // rgb.blink(3, 500);
+ // delay(1000);
+ // rgb.breathe(4, 1000);
// rgb.rainbowCycle(8000, 1.0);
// rgb.setColorRGB(0, 0, 0, true);
- // delay(1000);
+ // delay(1000);
// rgb.rainbowCycle(8000, 1);
// rgb.setColorRGB(0, 0, 0, true);
// delay(1000);