Push Button to Run Program Once

Hello, I have seen a couple threads on this topic, but I haven'y been able to get a clear answer for my project.

I have programed two sets of Christmas lights to flicker on and off to music (Jingle Bells). This program works fine with no issues.

What I want to do now is attached a button to actuate the program.

Attempt 1) I used a switch (similar to a light switch). This worked, and the program repeated as long as the switch was actuated. The program ended when I switched to off. Although it worked, I wanted to use a button to actuate it.

Attempt 2) I used a push button (not sure what it was called), this also worked, but the program would only run if I manually held the button. As soon as I let go, the program stopped.

What I want is to press a button once, have the program run and play the Jingle Bell song one time, then end. This way if I want to hear Jingle Bells again, all I have to do is press the button once (not having to hold it). This would be similar to any other Christmas decoration you can buy is stores.

I apologize, I am new and am not familiar with the button typed and terminology. I also apologize I do not have my past code to reference, so any general help would be appreciated.

Thank you!

Nobody here can help you if you do not post the code with which you want help.

You need to do something like this:

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    playJingleBells();
  }
}

void playJingleBells() {
  // As per function name.
}

it sounds like you have a momentary button. it only makes contact as long as it is held.

there is an example progrm in the IDE about switch.

you have to press once to change state. and the state remains changed until you press again.

play with that.

if you change state and go high
you then play your music.
once the song has completed, not sure how you know this, maybe time out ?
then the end of the song would make the switch go low.

the trick is to use what we call a flag.

if the button is pressed
then the flag goes high

if the flag is high
then play the song

notice there is no way for the flag to go low.

if the button is pressed
then the flag goes high

if the button is released
there is nothing that changes the flag, flag remains high

when the song ends
flag goes low

wvmarle did a nice job, very simple.
The key is to have a second void to play the song.
with my flag version, it offers a tiny bit more in the way of options.
(Built on wvmarle’s work.)

Code: [Select]

int FlagPlaySong ;   // global value

void setup () {
// all your setup stuff here
FlagPlaySong = 4 ;  // set it so it does not play when power on.
}

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    FlagPlaySong = 0 ;   // sets the value to 0 
  }

if FlagPlaySong <= 3{   //  plays up to multiple times
   playJingleBells();
   }
}

void playJingleBells() {     // your function name.
     FlagPlaySong ++ ;   // adds 1 to the count on the variable.
      // all your song stuff goes here

}

Yes, I kept it very simple.
A counter like that would also restart if you press the button during say the second iteration, and play four more jingle bells after that (the currently playing iteration would’ve been counted already).

Why add the complication of counting how many times the song has played ?

The requirement was stated as

What I want is to press a button once, have the program run and play the Jingle Bell song one time, then end.

The answer is in reply #2

Of course, we don't know how the input is wired and without a pullup resistor it could be LOW without the button being pressed.

Thank you all for the help! I will try this code the next time I am able. :-)

Why not use a push button toggle switch instead of a momentary push button switch?

cutlass1972: Why not use a push button toggle switch instead of a momentary push button switch?

I don't immediately see how that would help

UKHeliBob:
I don’t immediately see how that would help

Maybe I am misunderstanding the issue? He said that if he used a switch like a light switch his code works when he turns it on, and stops when he turns it off. He also said that he tried using a push button switch and the music played as long as he held the button down but stopped when he let go. A push button toggle switch would do the same as the lever type toggle switch he was using originally. Push once = on push again = off.

cutlass1972:
Maybe I am misunderstanding the issue?

I think you are.

What he said he wants is

What I want is to press a button once, have the program run and play the Jingle Bell song one time, then end

Simply put all the code in the setup function and use the reset push button to run it once. You can wire an external reset button between the reset input and ground on the Arduino if you want the button to be remote.

Hello all, I seem to still be having issues. Please see my code below. The push button is pin 9, I have a buzzer in pin 8, and the lights are pin 6.

I want to press the button, pin 9, run the program (I omitted much of the song as it is quite long), then when the song is over, turn everything off until the button is pressed again.

With the code below, the button does not seem to be doing anything, and the program continuously plays.

Any suggestions?

int tree1 = 6; //tree

void setup(){

int buttonPin = 9;

pinMode(8, OUTPUT); //buzzer pin
pinMode(tree1, OUTPUT);

digitalWrite(tree1,HIGH);
delay(1000);
digitalWrite(tree1,LOW);
Serial.begin(9600);

}

void loop()

{

if (digitalRead(9) == HIGH) {
start = millis();

//Joy
tone(8,C5,F);
digitalWrite(tree1, HIGH);
delay(1+F);

//to
tone(8,B4,DQ);
digitalWrite(tree1, LOW);
delay(1+DQ);

//the
tone(8,LA4,E);
digitalWrite(tree1, HIGH);
delay(1+E);

//world
tone(8,G4,DH);
digitalWrite(tree1, LOW);
delay(1+DH);

delay(20000);
end = millis();
time = end - start;
Serial.println(time);
}

}

I would not expect you to understand the depth of the code posted, but between post #2 and #3 we gave you all you needed.

I was going to make one more try, but you have some fundamental issues.

1) please read how to use this forum, specifically #7

2) you need to read up on declaring variables

3) you need to understand the concept of a switch, read up on momentary, normally open and normally closed. also SPST and SPDT.

I did not try to see if this will compile, I just put some of the bits in places to give you an idea about what was offered in posts #2 and #3.

int tree1 = 6; //tree
int buttonPin = 9;

void setup(){
  
//  int buttonPin = 9;

pinMode(8, OUTPUT);   //buzzer pin
pinMode(tree1, OUTPUT);

  digitalWrite(tree1,HIGH);
  delay(1000);
  digitalWrite(tree1,LOW);
  Serial.begin(9600);

}

void JOY()
{

//  if (digitalRead(9) == HIGH) {  // useless it blocks when not pressed
  start = millis();

//Joy
tone(8,C5,F);
  digitalWrite(tree1, HIGH);
delay(1+F);

//to
tone(8,B4,DQ);
  digitalWrite(tree1, LOW);
delay(1+DQ);

//the
tone(8,LA4,E);
  digitalWrite(tree1, HIGH);
delay(1+E);

//world
tone(8,G4,DH);
  digitalWrite(tree1, LOW);
delay(1+DH);

 // delay(20000);   never use delay() if possible  
  end = millis();  // one is supposed to declare variables
  time = end - start;
  Serial.println(time);
}

void loop() {
if (digitalRead(9) == HIGH) 
    { 
     playJingleBells();
    }
  }

[/code]

I do appreciate the feedback dave-in-nj. I am aware of my inexperience as an Arduino programmer, which is way I'm asking questions in an attempt to learn.

And though I may not fully understand how to use this forum yet as you pointed out, I am grateful that this forum exists to help individuals such as myself.

Thank you.

First off you left out so much code that it will not compile so we can't try it ourselves.

However the basic structure of looking at the state of the input button and depending on the result running or not running the code is correct. Which suggests you have not wired up your button correctly. But as you give us no clue as to how it is wired up we can't tell if it is fundamentally wrong or you have not implemented the idea correctly.

Your other very BIG problem is you are trying to run before you can even crawl. Like many beginners you are trying to do too much with too little knowledge. As a result you are incapable of understanding any of the answers you are getting here. You do not write code in the way you have done, one day you will be very ashamed of what you wrote.

You need to know so much more stuff before you can embark on such a project. Here are the things you NEED to know about. How to use this forum Inputs Arrays

Hint:- when you write the same or nearly the same line or set of lines of code you are doing things wrong, code it there to prevent you needing to do that. You need to use for loops and arrays.

Grumpy_Mike: First off you left out so much code that it will not compile so we can't try it ourselves.

for your test program, you left off all the bits about duration for tone. GrumpyMike is correct is that your fundamentals are at a beginner stage, but your desire for a sketch is more advanced. And, yes, that is why we want to help point you in the right direction. I would ask that you post a link to the site where you got the sketch. code is written in three parts. the declaration area where you list libraries, fix any global variables, etc the setup() where you initialize most of the things that need it. and the loop(), or run functions. as you might see what vwmarle posted was setup() songcode() loop() and loop just was calling the songcode() function when the button was pressed. you could have many of those, pick whatever name you like. and then in loop, you look for that moment that the button is pressed and once it has been pressed, you jump to that songcode() and play the song. in your post, you have tone() and list duration as letters, but, your code never defines them. hence, there are bunch of bits that will come up as errors. what you apparently have taken out is the references to the notes and the duration of them.

tone(8,C5,F);

calls for pin 8 to play a frequency. that frequency is referenced by C5 but you never declare that

then, tone() is to hold that note for F F is the duration, but you do not declare that

the delay of (1+F) is a brief pause between notes.

without the original, or whole sketch, we cannot really do much more than point you on where to learn how to program.

But, do not worry too much, I think 99.99% of noobies learn by copying a sketch and then trying to figure out how it works.

please go to the main forum, there is a sticky post at the top of every one called how to use this forum, read #7 on how to post code.

since you said that you got it to play, you are only a few steps from getting what you wanted in your first post.

then, tone() is to hold that note for F F is the duration, but you do not declare that

That is in an "#include" libiary that he missed out.

But, do not worry too much, I think 99.99% of noobies learn by copying a sketch and then trying to figure out how it works.

Problem is when I learned code their was no code to copy, so I had to learn by reading the language definitions. It is still a good place to start, use the help menu and pull up the reference page. That will tell you how 98% of the statements you will see in code works.

It looks like

#include "pitches.h"

has gone missing from the code somewhere along the line.

A look at tone melody example would seem to be a good idea.