#define BUZ_PIN 9
#define DO 261
#define RE 293
#define MI 329
#define FA 349
#define SO 392
#define LA 440
#define TI 494
int music[] = {DO, RE, MI, FA, SO, LA, TI};
void setup() {
Serial.begin(9600);
}
void loop() {
char data;
int i = 0;
int duration = 500;
if (Serial.available()) {
data = Serial.read();
if (data == 'q') {
duration /= 2;
}
if (data == 'w') {
duration *= 2;
}
if (data == 'a') {
tone(BUZ_PIN, music[i], duration);
delay(duration);
}
if (data == 's') {
tone(BUZ_PIN, music[i + 1], duration);
delay(duration);
}
if (data == 'd') {
tone(BUZ_PIN, music[i + 2], duration);
delay(duration);
}
if (data == 'f') {
tone(BUZ_PIN, music[i + 3], duration);
delay(duration);
}
if (data == 'g') {
tone(BUZ_PIN, music[i + 4], duration);
delay(duration);
}
if (data == 'h') {
tone(BUZ_PIN, music[i + 5], duration);
delay(duration);
}
if (data == 'j') {
tone(BUZ_PIN, music[i + 6], duration);
delay(duration);
}
}
}
I'm Beginner
If you input qa into the serial monitor, the beat should be output as x2, but it is output with the same beat as a. The same goes for wa(1/2 a ).
w(q)a, w(q)s, w(q)d, w(q)f, w(q)g, w(q)h ,w(q)j are all output in one beat
How should I fix it here?
not sure what you expect duration to be. duration is defined in loop and initialized to 500 each iteration of loop(). define it globally, (outside of any function) if you want it's value to be persistent between iterations of loop.
also don't understand the purpose of the delays() after calling tone()
It's going right all over the place. But after that, when I entered Twinkle, Twinkle Little Star, the speed was arbitrary. The reason for using the delay is that if you don't use that, only the beep is output quickly. How should I fix it?
#define BUZ_PIN 9
#define DO 261
#define RE 293
#define MI 329
#define FA 349
#define SO 392
#define LA 440
#define TI 494
int music[] = { DO, RE, MI, FA, SO, LA, TI };
int duration = 500;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char data = Serial.read();
if (data == 'q') {
duration /= 2;
}
if (data == 'w') {
duration *= 2;
}
if (data == 'a') {
tone(BUZ_PIN, music[0], duration);
delay(duration);
}
if (data == 's') {
tone(BUZ_PIN, music[1], duration);
delay(duration);
}
if (data == 'd') {
tone(BUZ_PIN, music[2], duration);
delay(duration);
}
if (data == 'f') {
tone(BUZ_PIN, music[3], duration);
delay(duration);
}
if (data == 'g') {
tone(BUZ_PIN, music[4], duration);
delay(duration);
}
if (data == 'h') {
tone(BUZ_PIN, music[5], duration);
delay(duration);
}
if (data == 'j') {
tone(BUZ_PIN, music[6], duration);
delay(duration);
}
}
}
Because you created the variable "duration" in the loop it got set back to 500 after every cycle of the loop.
The variable "i" is unneccesarry I think, you can just enter the number right inside the brackets to select which value of the array to select.
I also changed where "data" is incemented, but it doesn't matter.+
(Sorry for bad English, it's not my first language)
By declaring duration as global, the speed of qa a wa is solved. However, if you input the notes of the song, the speed increases as the verse goes by.
on the serial monitor
aagg hhqg ffdd sswa
ggff ddqs ggff ddqs
aagg hhqg ffdd sswa
By declaring duration as global, the speed of qa a wa is solved. However, if you input the notes of the song, the speed increases as the verse goes by.
on the serial monitor
aagg hhqg ffdd sswa
ggff ddqs ggff ddqs
aagg hhqg ffdd sswa
If you input a song to output aagg hhqg, the speed is output quickly from here.
The speed of qa wa a is solved! But when I type in a song, it speeds up as I go backwards. Can you tell me what's wrong? DODOREREMIMISOL-DODOREREMIMISOL (Output as the speed increases from this part)
q The problem has been resolved.
I couldn't upload the video here, so I just link the driving result video.
What is the reason for the song speed like that?
I think it's because you’re only increasing the speed of the song
("q" makes everything from now on slower, "w" faster)
You either have to decrease the speed by adding "q" after speeding it up or you have to change your code to set the variable "duration" back to 500 after every note like this:
#define BUZ_PIN 9
#define DO 261
#define RE 293
#define MI 329
#define FA 349
#define SO 392
#define LA 440
#define TI 494
int music[] = { DO, RE, MI, FA, SO, LA, TI };
int duration = 500;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char data = Serial.read();
if (data == 'q') {
duration *= 2;
}
if (data == 'w') {
duration /= 2;
}
if (data == 'a') {
tone(BUZ_PIN, music[0], duration);
delay(duration);
duration = 500;
}
if (data == 's') {
tone(BUZ_PIN, music[1], duration);
delay(duration);
duration = 500;
}
if (data == 'd') {
tone(BUZ_PIN, music[2], duration);
delay(duration);
duration = 500;
}
if (data == 'f') {
tone(BUZ_PIN, music[3], duration);
delay(duration);
duration = 500;
}
if (data == 'g') {
tone(BUZ_PIN, music[4], duration);
delay(duration);
duration = 500;
}
if (data == 'h') {
tone(BUZ_PIN, music[5], duration);
delay(duration);
duration = 500;
}
if (data == 'j') {
tone(BUZ_PIN, music[6], duration);
delay(duration);
duration = 500;
}
}
}
Or a bit cleaner like this:
#define BUZ_PIN 9
#define DO 261
#define RE 293
#define MI 329
#define FA 349
#define SO 392
#define LA 440
#define TI 494
int music[] = { DO, RE, MI, FA, SO, LA, TI };
int duration = 500;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char data = Serial.read();
switch (data) {
case 'q': duration *= 2; break;
case 'w': duration /= 2; break;
case 'a': playTone(0); break;
case 's': playTone(1); break;
case 'd': playTone(2); break;
case 'f': playTone(3); break;
case 'g': playTone(4); break;
case 'h': playTone(5); break;
case 'j': playTone(6); break;
default: break;
}
}
}
void playTone(int t) {
tone(BUZ_PIN, music[t], duration);
delay(duration);
duration = 500;
}
(Atleast with this code "aagg hhqg ffdd sswa ggff ddqs ggff ddqs aagg hhqg ffdd sswa" sounds right in my opinion)