Skip to content

Commit c41d06f

Browse files
committed
Add 2 examples: BuzzerSweep, EncoderSound
BuzzerSweep requires the modulino buzzer, it is going to play a sound sweeping its frequency EncoderSound requires the modulino buzzer and the modulino knob, the user can play with the knob to change the frequency of the sound played by the buzzer. The button of the knob will mute/unmute the system
1 parent 72a6a8e commit c41d06f

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

examples/BuzzerSweep/BuzzerSweep.ino

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "Modulino.h"
2+
3+
#define FREQUENCY_LOWEST 100 //Lowest frequency in hertz
4+
#define FREQUENCY_HIGHEST 8000 //Highest frequency in hertz
5+
#define FREQUENCY_STEP 10 //Each iteration changes frequency by this number
6+
7+
ModulinoBuzzer buzzer;
8+
9+
int frequency = FREQUENCY_LOWEST;
10+
String frequency_text = String(frequency) + " Hz";
11+
12+
void setup() {
13+
Serial.begin(115200);
14+
15+
//Inizialise modulinos
16+
Modulino.begin();
17+
buzzer.begin();
18+
19+
delay(100);
20+
}
21+
22+
void loop() {
23+
//Increase played frequency by the desired step.
24+
//Start again if highest frequency has been played
25+
frequency = frequency + FREQUENCY_STEP;
26+
if (frequency > FREQUENCY_HIGHEST) {
27+
frequency = FREQUENCY_LOWEST;
28+
}
29+
30+
//Play the frequency on the buzzer
31+
buzzer.tone(frequency, 1000);
32+
33+
//Convert the frequency value to string and add Hz
34+
frequency_text = String(frequency) + " Hz";
35+
Serial.println(frequency_text);
36+
}
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include "Modulino.h"
2+
3+
#define FREQUENCY_DEFAULT 4000 //Starting frequency in hertz
4+
#define FREQUENCY_LOWEST 1 //Minimum frequency in hertz
5+
#define FREQUENCY_HIGHEST 20000 //Maximum frequency in hertz
6+
#define FREQUENCY_STEP 200 //How much the frequency changes when the knob is turned by 1 click
7+
#define CYCLE_WAIT_MS 25 //Amount of added delay between loop iterations
8+
9+
//Create objects for modulinos
10+
ModulinoKnob encoder;
11+
ModulinoBuzzer buzzer;
12+
13+
//Declare variables for the sketch
14+
int frequency = FREQUENCY_DEFAULT; //frequency which will be played by the buzzer
15+
int actual_value = FREQUENCY_DEFAULT; //Current encoder value.
16+
int previous_value = actual_value; //Previous encoder value.
17+
bool mute_status = false; //To use the encoder button as an MUTE/PLAY button.
18+
19+
String frequency_text = String(frequency) + " Hz";
20+
String encoder_text = "(encoder value " + String(actual_value) + ")";
21+
String mute_text = "- now playing";
22+
23+
void setup() {
24+
Serial.begin(115200);
25+
26+
//Inizialise modulinos
27+
Modulino.begin();
28+
buzzer.begin();
29+
encoder.begin();
30+
31+
//Wait for modulino encoder inizialization and set starting value
32+
delay(100);
33+
encoder.set(FREQUENCY_DEFAULT);
34+
}
35+
36+
void loop() {
37+
//Acquire encoder value
38+
actual_value = encoder.get();
39+
40+
/*Check if the encoder value has changed with respect to previous
41+
acquisition, if so increase/decrease the encoder value by the
42+
desired FREQUENCY_STEP, then update the previous value variable
43+
*/
44+
if (previous_value != actual_value) {
45+
if (actual_value > previous_value) {
46+
actual_value = actual_value + FREQUENCY_STEP;
47+
} else {
48+
actual_value = actual_value - FREQUENCY_STEP;
49+
}
50+
51+
if (actual_value < FREQUENCY_LOWEST) {
52+
actual_value = FREQUENCY_LOWEST;
53+
encoder.set(FREQUENCY_LOWEST);
54+
}
55+
if (actual_value > FREQUENCY_HIGHEST) {
56+
actual_value = FREQUENCY_HIGHEST;
57+
encoder.set(FREQUENCY_HIGHEST);
58+
}
59+
encoder.set(actual_value);
60+
previous_value = actual_value;
61+
}
62+
63+
//Check if encoder has been pressed. In that case toggle
64+
//mute status. Update mute text to diplay the MUTE/PLAY on serial
65+
if (encoder.isPressed()) {
66+
mute_status = !mute_status;
67+
if (mute_status == false) {
68+
mute_text = "- now playing";
69+
} else {
70+
mute_text = "- muted";
71+
}
72+
}
73+
74+
//Play the frequency on the buzzer only if the system is not muted.
75+
if (mute_status == false) {
76+
buzzer.tone(actual_value, 1000);
77+
} else {
78+
buzzer.noTone();
79+
}
80+
81+
//Convert the frequency value to string and add Hz
82+
frequency_text = String(frequency) + " Hz";
83+
84+
//Convert the encoder value to string
85+
encoder_text = "(encoder value " + String(actual_value) + ")";
86+
87+
//Print each of the sensor values on serial
88+
Serial.print(frequency_text + " ");
89+
Serial.print(encoder_text + " ");
90+
Serial.println(mute_text);
91+
92+
delay(CYCLE_WAIT_MS);
93+
}

0 commit comments

Comments
 (0)