I want to create a two player game. The mechanics is that i want to play a few notes of a certain songs and the first one to push his/her designated push button will be the first one to guess the title of the song. What i want is to stop the music if either one of that button was pushed.
That sounds OK
What hardware will you be using ?
How far have you got with the project ?
What do you need help with ?
I will be using arduino Uno, and I just started the project a few days ago. The problem is that if I press either of the two button the music is not stopping
Sorry, my crystal ball is broken
What buttons would they be ?
How are they wired in the schematic that you have not posted ?
How are they dealt with in the sketch that you have not posted ?
And the sketch ?
Are you pertaining to the code?
Yes
#include "Tone440.h"
#define TONE_USE_DOUBLE
const int buzzerPin = 10;
// Variable declaration for player 1
const int btnP1Pin = 2;
const int ledP1Pin = 3;
// Variable declaration for player 2
const int ledP2Pin = 12;
const int btnP2Pin = 13;
unsigned int eventTimer = 1000;
unsigned long prevTime = 0;
unsigned long currTime = 0;
bool isPlayingState = true;
void setup() {
pinMode(btnP1Pin, INPUT_PULLUP);
pinMode(ledP1Pin, OUTPUT);
pinMode(btnP2Pin, INPUT_PULLUP);
pinMode(ledP2Pin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
currTime = millis();
while(true) {
if(isPlayingState) {
playSong1();
}
if(currTime - prevTime >= eventTimer){
if(digitalRead(btnP1Pin) == LOW){
digitalWrite(ledP1Pin, HIGH);
isPlayingState = false;
break;
}
else if(digitalRead(btnP2Pin) == LOW){
digitalWrite(ledP2Pin, HIGH);
isPlayingState = false;
break;
}
prevTime = currTime;
}
}
}
void playSong1() {
int melody[] = {
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D5,
NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5,
NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5,
NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5,
NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5,
NOTE_C5, NOTE_AS4, NOTE_A4, NOTE_F4,
NOTE_G4, 0, NOTE_G4, NOTE_D5,
NOTE_C5, 0, NOTE_AS4, 0,
NOTE_A4, 0, NOTE_A4, NOTE_A4,
NOTE_C5, 0, NOTE_AS4, NOTE_A4,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
NOTE_G4, 0, NOTE_G4, NOTE_D5,
NOTE_C5, 0, NOTE_AS4, 0,
NOTE_A4, 0, NOTE_A4, NOTE_A4,
NOTE_C5, 0, NOTE_AS4, NOTE_A4,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5
};
int noteDurations[] = {
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
};
for (int thisNote = 0; thisNote < 112; thisNote++)
{
int noteDuration = 750 / noteDurations[thisNote];
tone(buzzerPin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);}
}
That is because the song is being played by a for loop
for (int thisNote = 0; thisNote < 112; thisNote++)
{
int noteDuration = 750 / noteDurations[thisNote];
tone(buzzerPin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);
}
Once started there is nothing to stop all 112 notes being played
Once crude way to deal with this would be to read the button inputs inside the for loop, but it is not the best solution
Note that you do not need to create your own endless while loop because the loop() function will run over and over again in any case
It is working but it is not stopping the music in real time. It will take more than one press to stop the music.
If you have changed the code then please post the new version in a new post
#include "Tone440.h"
#define TONE_USE_DOUBLE
const int buzzerPin = 10;
// Variable declaration for player 1
const int btnP1Pin = 2;
const int ledP1Pin = 3;
// Variable declaration for player 2
const int ledP2Pin = 12;
const int btnP2Pin = 13;
unsigned int eventTimer = 1000;
unsigned long prevTime = 0;
unsigned long currTime = 0;
bool isPlayingState = true;
void setup() {
pinMode(btnP1Pin, INPUT_PULLUP);
pinMode(ledP1Pin, OUTPUT);
pinMode(btnP2Pin, INPUT_PULLUP);
pinMode(ledP2Pin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
currTime = millis();
playSong1();
}
void playSong1() {
int melody[] = {
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_AS4,
NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D5,
NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5,
NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5,
NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5,
NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5,
NOTE_C5, NOTE_AS4, NOTE_A4, NOTE_F4,
NOTE_G4, 0, NOTE_G4, NOTE_D5,
NOTE_C5, 0, NOTE_AS4, 0,
NOTE_A4, 0, NOTE_A4, NOTE_A4,
NOTE_C5, 0, NOTE_AS4, NOTE_A4,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
NOTE_G4, 0, NOTE_G4, NOTE_D5,
NOTE_C5, 0, NOTE_AS4, 0,
NOTE_A4, 0, NOTE_A4, NOTE_A4,
NOTE_C5, 0, NOTE_AS4, NOTE_A4,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
NOTE_G4,0, NOTE_G4, NOTE_AS5,
NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5
};
int noteDurations[] = {
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
4,4,4,4,
};
for (int thisNote = 0; thisNote < 112; thisNote++) {
if(digitalRead(btnP1Pin) == LOW){
digitalWrite(ledP1Pin, HIGH);
isPlayingState = false;
}
else if(digitalRead(btnP2Pin) == LOW){
digitalWrite(ledP2Pin, HIGH);
isPlayingState = false;
}
if(isPlayingState) {
int noteDuration = 750 / noteDurations[thisNote];
tone(buzzerPin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);}
}
}
You don't have anything in the code to exit the for loop when a button press is executed
However, remember that I said that reading the button state in the for loop was crude. There are better ways to do what you want but maybe get the crude method working first then you can improve on it
Ok I'll add it. I will try no make this code better. Thank you so much.
The best way to do what you want is not to use a for loop to play the song. Instead, whilst a boolean is true use non blocking timing using millis() to play the notes. This way, the loop() function can run freely and you can read the buttons very quickly. When one becomes pressed, set the boolean to false to stop the song playing
See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE
The latest sketch still won't read the buttons while a note is playing, only in between notes it reads the button inputs.
It will work as when isPlaingState is set false, the iterations of the loop will be very fast and done for in an instant. Not pretty but it will work.
If you want many more songs and better sound quality, use an mp3 player like the DFPlayer. Makes your code easier as well, as you just tell the player to start playing, and it will do its thing. Then when you read a button press you can tell the player to stop.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.