-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path3_serial_player.cpp
76 lines (64 loc) · 1.73 KB
/
3_serial_player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <Arduino.h>
#include <TonePlayer.h>
#include <BLDCSpeaker.h>
#include <BLDCMotor.h>
BLDCMotor motor1 = BLDCMotor(PB1, PB0, PA7,15, PA4);
BLDCSpeaker speaker1 = BLDCSpeaker(&motor1, 1.5, 50, 0);
TonePlayer player;
void setup() {
Serial.begin(115200);
delay(750);
/* WARNING: ensure voltage_limit isn't too high - you could overheat motor or MOSFETS!! */
motor1.voltage_limit = 1.5;
motor1.init();
Serial.println(F("** SERIAL PLAYER **"));
player.attachSpeaker(&speaker1);
Serial.println(F("Copy and paste '6cdec cdec efgp efgp +gagf-ec +gagf-ec c5g6cp c5g6-c' into your Serial console or 'type' your own song"));
}
void playNotesFromSerial()
{
static int octave = 5;
static int duration = 400;
// a string to hold incoming data
static String received_chars;
if (Serial.available())
{
char inChar = (char)Serial.read();
if (isDigit(inChar))
{
octave = atoi(&inChar);
Serial.print(F("Octave is now: ")); Serial.println(octave);
}
else if (isWhitespace(inChar)) {
// ignore
}
else if (inChar == '-')
{
Serial.println(F("Half Speed"));
duration *= 2; // slow it down
}
else if (inChar == '+')
{
Serial.println(F("Double Speed"));
duration /= 2; // speed it up
}
else if (isAlpha(inChar))
{
// char note = inChar;
float freq = toneFrequency((char) inChar, octave, false);
Serial.print(F("Playing: ")); Serial.print(inChar); Serial.println(octave);
player.playTone(freq, duration);
}
else
{
Serial.println(F("bad character, try pasting with mouse not keyboard!"));
}
}
}
void loop() {
if (player.isPlaying()) {
player.loop();
} else {
playNotesFromSerial();
}
}