Hey im trying to program a song with a arduino uno and i have the song coded and i want to turn the song on and off with a button ive looked all over for source code but i cant find any that helps can someone help me please
post the code
what button do you have?
Welcome to the forum
Your topic was MOVED to its current forum category as it is more suitable than the original
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
I think that this is what you meant to post:
void setup()
{
int pin=12; // the out put pin
int C4=262;
int D4=294;
int E4=330;
int F4=349;
int G4=392;
int C5=523;
int beat1=150;
int beat2= beat1* 2;
int beat3=beat1*3;
int beat6=beat1*6;
for(int i=0; i<2;i++) { // loop; index i; condition ; increment {}
//play row row your boat
tone(pin,C4); // Tone(pin frequancy)
delay(beat3);
noTone(pin);
delay(20);
}
tone(pin,C4); // Tone(pin frequancy)
delay(beat2);
noTone(pin);
delay(15);
tone(pin,D4); // Tone(pin frequancy)
delay(beat1);
noTone(pin);
delay(15);
tone(pin,E4); // Tone(pin frequancy)
delay(beat3);
noTone(pin);
delay(20);
// gentyl down the steam
tone(pin,E4); // Tone(pin frequancy)
delay(beat2);
noTone(pin);
delay(15);
tone(pin,D4); // Tone(pin frequancy)
delay(beat1);
noTone(pin);
delay(15);
tone(pin,E4);
delay(beat2);
noTone(pin);
delay(15);
tone (pin,F4);
delay (beat1);
noTone (pin);
delay(15);
tone (pin,G4);
delay (beat6);
noTone (pin);
delay(15);
// merrily merrily
for(int i=0; i<3;i++) {
tone (pin,C5);
delay (beat1);
noTone (pin);
delay(20);
}
for(int i=0; i<3;i++) {
tone (pin, G4);
delay (beat1);
noTone (pin);
delay(20);
}
//merrily merrily
for(int i=0; i<3;i++) {
tone (pin, E4);
delay (beat1);
noTone (pin);
delay(20);
}
tone (pin, C4);
delay (beat1);
noTone (pin);
delay(20);
tone (pin, C4);
delay (beat1);
noTone (pin);
delay(20);
tone (pin, C4);
delay (beat1);
noTone (pin);
delay(15);
//life is but a dream
tone (pin,G4);
delay (beat2);
noTone (pin);
delay(15);
tone (pin, F4);
delay (beat1);
noTone (pin);
delay(15);
tone (pin,E4);
delay(beat2);
noTone (pin);
delay(15);
tone (pin, D4);
delay (beat1);
noTone (pin);
delay(15);
tone (pin,C4);
delay (beat6);
noTone (pin);
}
void loop () {
}
yes so if someone could help me program the button i would be very happy
the way your code work is very sequential and you have delays all along. It does not make it easy to check for a button press in an asynchronous way
One (ugly) way to achieve your need would be to have an interrupt triggered by the button and stay within the interrupt until the button is released and then pressed again...
The proper way to do this requires refactoring your code to get rid of the delays. Look at Using millis() for timing. A beginners guide and Several things at the same time
PS/ (Using arrays would also make it easier - see https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody to get an idea for that)
So ive compressed the code with arrays and i want the song to go on and off with the push of a button how do i do that can i get sample code the link for millis for leds i dont have any in this circuit
int pin=12; // the out put pin
int C4=262;
int D4=294;
int E4=330;
int F4=349;
int G4=392;
int C5=523;
int beat1=150;
int beat2= beat1* 2;
int beat3=beat1*3;
int beat6=beat1*6;
int melody[]={C4,C4,C4,D4,E4,E4,D4,E4,F4,G4,C5,C5,C5,G4,G4,G4,E4,E4,E4,C4,C4,C4,G4,F4,E4,D4,C4};
int beat[]={beat3,beat3,beat2,beat1,beat3,beat2,beat1,beat2,beat1,beat6,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat2,beat1,beat2,beat1,beat6};
void setup() {
for (int i=0; i<27; i++)
{
tone(pin,melody [i]);
delay(beat[i]);
noTone(pin);
delay(20);
}
}
void loop ()
{
}
the way it's written you can only check for button press in between the delays. better than nothing....
read this to add a button
https://create.arduino.cc/projecthub/muhammad-aqib/arduino-button-tutorial-using-arduino-digitalread-function-08adb5
//modify the code as follow////
/// Step1. adding the input pushbutton///
/// - pushbutton attached to pin 2 from +5V
/// - 10K resistor attached to pin 2 from ground
////Step 2. move code to void loop///
///Step3. waiting code execution till the button is pressed (or wait input pin goto HIGH)/////
const int pinOut=12; // the out put pin. better to make it as constant integer
const int buttonPin=2; //input button connect to pin 2
int C4=262;
int D4=294;
int E4=330;
int F4=349;
int G4=392;
int C5=523;
int beat1=150;
int beat2= beat1* 2;
int beat3=beat1*3;
int beat6=beat1*6;
int melody[]={C4,C4,C4,D4,E4,E4,D4,E4,F4,G4,C5,C5,C5,G4,G4,G4,E4,E4,E4,C4,C4,C4,G4,F4,E4,D4,C4};
int beat[]={beat3,beat3,beat2,beat1,beat3,beat2,beat1,beat2,beat1,beat6,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat2,beat1,beat2,beat1,beat6};
void setup() {
// initialize the pint 12 pin as an output:
pinMode(pinOut, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop ()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
for (int i=0; i<27; i++)
{
tone(pinOut,melody [i]);
delay(beat[i]);
noTone(pinOut);
delay(20);
}
}
}
then once the song starts you are toast, you can't stop it...
you need to read the button when you are not stuck in the delay (and ideally get rid of the delay)
So i tried that and it doesnt work and my wireing has to be like this so i cant do resistors and +5V
const int pinOut=12; // the out put pin. better to make it as constant integer
const int buttonPin=2; //input button connect to pin 2
int pin=12; // the out put pin
int C4=262;
int D4=294;
int E4=330;
int F4=349;
int G4=392;
int C5=523;
int beat1=150;
int beat2= beat1* 2;
int beat3=beat1*3;
int beat6=beat1*6;
int buttonState = 0; // current state of the button
int melody[]={C4,C4,C4,D4,E4,E4,D4,E4,F4,G4,C5,C5,C5,G4,G4,G4,E4,E4,E4,C4,C4,C4,G4,F4,E4,D4,C4};
int beat[]={beat3,beat3,beat2,beat1,beat3,beat2,beat1,beat2,beat1,beat6,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat2,beat1,beat2,beat1,beat6};
void setup() {
// initialize the pint 12 pin as an output:
pinMode(pinOut, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop ()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
for (int i=0; i<27; i++)
{
tone(pinOut,melody [i]);
delay(beat[i]);
noTone(pinOut);
delay(20);
}
}
}
Make a electric schéma that is readable… Fritzing just does not cut it
With button.pdf (4.0 KB)
yes that is the one now how do i program it to turn on and off a song with a button
The crude version but with limited changes to your code would be to check the button within your for loop after each delay.
If. You have delays that are stopping any activity for close to a second then the lag upon pressing is noticeable and possibly annoying for a human
for (int i=0; i<27; i++) {
tone(pinOut,melody [i]);
delay(beat[i]);
noTone(pinOut);
if (digitalRead(buttonPin) == HIGH) { do something as button is pressed }
delay(20);
if (digitalRead(buttonPin) == HIGH) { do something as button is pressed }
}
Another option, improving on the previous one, is to use the tone() function as you do but pass the duration (that is used in the delay at the moment) as a third parameter. This will issue the sound then asynchronously for the right duration and your code will go on. Then instead of delay (because you want to wait until the end of the sound before triggering the next call to tone) you use a while loop using millis and check the button within that loop. That will make the UX more reactive
The option that requires the largest change in your code would be a state machine using millis to know when to do what.
Ok so i have code and when i hit the button the song plays. if i want to turn the song off when i hit the button during the middle of the song can i do that?
const int buttonPin=2;
const int speakerPin=12;
int buttonState = 0;
int C4=262;
int D4=294;
int E4=330;
int F4=349;
int G4=392;
int C5=523;
int beat1=150;
int beat2= beat1* 2;
int beat3=beat1*3;
int beat6=beat1*6;
int melody[]={C4,C4,C4,D4,E4,E4,D4,E4,F4,G4,C5,C5,C5,G4,G4,G4,E4,E4,E4,C4,C4,C4,G4,F4,E4,D4,C4};
int beat[]={beat3,beat3,beat2,beat1,beat3,beat2,beat1,beat2,beat1,beat6,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat1,beat2,beat1,beat2,beat1,beat6};
void setup()
{
pinMode(speakerPin, OUTPUT);
pinMode (buttonPin, INPUT_PULLUP);
}
void loop () {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
for(int i=0; i<27; i++){
tone(12,melody[i]);
delay(beat[i]);
noTone(12);
delay(20);
}
}
}
Seems you did not read my post…
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.