Button to start timer, then reset timer

I am writing code for a countdown timer, and I would like to have one button which when pressed will start the loop that counts down, and when I press it again it will reset the loop and be ready for when I press it again to start the loop again. Could anyone give me an example code to accomplish this?

An added bonus would be if someone could help me with having the Arduino sleep until I press the button to start the timer, then when I press the button it resets the program and puts the Arduino to sleep again.

Thanks so much. This is my first time coding.

perkinsandy0:
I am writing code for a countdown timer, and I would like to have one button which when pressed will start the loop that counts down, and when I press it again it will reset the loop and be ready for when I press it again to start the loop again. Could anyone give me an example code to accomplish this?

An added bonus would be if someone could help me with having the Arduino sleep until I press the button to start the timer, then when I press the button it resets the program and puts the Arduino to sleep again.

Thanks so much. This is my first time coding.

Welcome to the Arduino forum. Hope you are ready to begin the lesson!

Decide how fast you want the count down to occur and what number you want to start with. Do you want to display something about the time remaining on the timer? What do you want to display it on? What kind of "button" do you have?

What are you using to power the project?

And finally, which Arduino are you using and have you worked with all the sample programs available for the Arduino?

Paul

So I already have all the code for the timer, I just want to be able to press the button to stop it and then press the button and have it start again.
For the button, I am wanting just a momentary push button.
Right now I am using an arduino mega powered through usb.

Right now I am looking at an example program included with the Arduino IDE that shows how to read and test a button switch.

Look in examples, digital, button. That will show you what to add to your program.

Paul

So I have already looked at that code and understand with the button and an if() command I can get it to do stuff. I just am wondering what code I would use to push the button to have it start, and pushing it again to restart.

perkinsandy0:
So I have already looked at that code and understand with the button and an if() command I can get it to do stuff. I just am wondering what code I would use to push the button to have it start, and pushing it again to restart.

Add a boolean to your program, initially set to false. When the button is pushed and the boolean is false, then you know to start the timer and set the boolean to true.

Next time the button is pushed and the boolean is true, then you know to stop the timer and set the boolean to false.

Paul

Could you give me an example code for that?

perkinsandy0:
Could you give me an example code for that?

I gave you the logic. You have the switch example program. The rest is you homework assignment. Show us your results. Bet it will work right away.

Paul

Paul,
I thank you for your enthusiasm, but that still does not help me understand how to do that. I am asking for example code so I can dissect it and understand it so I can make it my own and apply it to my project.
I have looked for days on how to accomplish this and found nothing. Hence why I came to the forum, so I could ask someone with way more experience to help me. Because for them it would probably be like 5-10 simple lines of code, whereas for me I have tried forever to do it and availed to nothing.

If you would want to help me put your concept into code so I could understand it that would be great.

void setup() {
(Timer Setup); //All my setups


}

void loop() {

(Timer Code); // All of my code

}

Where would I put the booleans to make it so when I press the button the loop starts, and when I press it again the loop resets and waits till I press the button to start

perkinsandy0:
Paul,
I thank you for your enthusiasm, but that still does not help me understand how to do that. I am asking for example code so I can dissect it and understand it so I can make it my own and apply it to my project.
I have looked for days on how to accomplish this and found nothing. Hence why I came to the forum, so I could ask someone with way more experience to help me. Because for them it would probably be like 5-10 simple lines of code, whereas for me I have tried forever to do it and availed to nothing.

If you would want to help me put your concept into code so I could understand it that would be great.

void setup() {

(Timer Setup); //All my setups

}

void loop() {

(Timer Code); // All of my code

}




Where would I put the booleans to make it so when I press the button the loop starts, and when I press it again the loop resets and waits till I press the button to start

Did you study this: Gammon Forum : Electronics : Microprocessors : Switches tutorial

Paul

That would definitely help me if I am using a switch, that would give a solid 5v or 0v. But like I said earlier I am wanting to use a momentary switch button, which wouldn't give me a solid value. I am wanting to use a momentary switch because of aesthetics for my build.

I could be misunderstanding how a momentary switch works though, if that is why you keep suggesting this coding concept

Use the debounce example. Exatly What you
need.