So I am trying to add a start/stop timer feature to a project I am working on. My problem is, I can not figure out how to properly program the button. I got the counting down and it displays fine on the lcd. But when I try to make it so all I have to do is press the button to start the counting, it will count once and then stop. I need help coming up with the loop to keep the timer going, and then to stop it once i press the button again. Thank you in advance.
My problem is, I can not figure out how to properly program the button.
I think you need to use a separate programmer for that.
Now, programming an Arduino to deal with a switch is a different story.
I know it's kinda messy
Messy is OK. You can use Tools + Auto format to correct that. Miles long is another matter.
Dump that sketch. Write some code that does nothing but read the switch(es), and turn on or off some LEDs to get a sense of what the switch is doing. Once you understand the basics, applying them to your program will be much easier.
For instance, you are not using the internal pullup resistors. This means that you need external pullup or pulldown resistors. Do you have them? How ARE the switches wired?
When you say "it didn't work", you aren't giving us anything to go on. "I expected this or that to happen. Instead, what happened was..." is much more useful information.
Yes I am using external resistors. And the reason it is miles long, is because it is part of a roast logger I am working on. But thank you for your pointers and i will work on trying to do it with just an led or something and once I figure it out, incorporate it into my logger. Thank you
Alright I redid the code so it is cleaner and shorter. But still having some trouble. I can have found out how to get the start aspect working. When I press the button the timer will turn on and start counting up by seconds. But when I can't not figure out how to make the timer stop where it is at when i press the same button again. I know i am stuck in a loop, but i can't think of a good exit plan
I think the code you have written is aimed at using a toggle switch instead of a push button.
With a toggle switch, you would look for a change that is different to the last value, as you have with :
if(timeState != lastTimeState) {
For a push button, you need to toggle the Boolean variable every time the button is pushed. I would use something like this :
WARNING : code not tested and most likely not the best way to write it, but it should give you the idea :
const int timeButton = 8;
int ButtonState = 0;
boolean ShowTimer = false;
void setup() {
pinMode(timeButton, INPUT);
digitalWrite(timeButton, HIGH);
//enables the internal pull-up.
//Button should connect Ground to input pin 8
//HIGH reading = button not pressed
//LOW reading = botton pressed
}
void loop() {
ButtonState = digitalRead(timeButton);
if(ButtonState == 0 then) { // button has been pressed
if(ShowTimer == true) ShowTimer = false; //switch the ShowTimer value
else ShowTimer = true;
delay(750); //add a delay so that the state does not keep changing until the button is released
}
if(ShowTimer == true){
// do what you want done when the ShowTimer is True
}
if(ShowTimer == false){
// do what you want done when the ShowTimer is False
}
}
Thank you DaveO. The other day I was thinking about writing the switch to the pull up state, but I forgot to go with it when I finally got to my computer to start coding. Once i get a chance I will use your input to work with! Thank you for helping me, and for not just giving me working code, but for giving me code that need works! It will help me in learning to be a better programmer!