Start/stop button

Hello again,

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.

Hard to help out without seeing a code listing.
Use the # button when you post it.

Sorry about that.

Here is everything so far, I know it’s kinda messy but what i am asking for help with is the first block in the void loop

There is no control statement now. I deleted it trying to find something that would work

Setting up start stop button

#include <LiquidCrystal.h>
#include <Time.h>

time_t elapsedTime;

// initialize lcd
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

// in temp pins and variables
const int temp1Pin = A0;
const int temp2Pin = A1;
int temp1Reading;
int temp2Reading;
//int start/stop button and variables
const int timeButtonPin = 8;
int timerButtonState;
int lastTimerState = 0;
int sCount = 0;
int mCount = 0;
// int airflow buttons and variables for counting
const int airflowButton = 9;
int airflowCounter = 0;
int airflowButtonState = 0;
int lastAirflowState = 0;

void setup(void) {

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  //define pinmodes
  pinMode(timeButtonPin, INPUT);
  pinMode(airflowButton, INPUT); 

void loop() {
  //Timer display
  timerButtonState = digitalRead(timeButtonPin);

    if (sCount>59){
    if (sCount<60){
      lcd.print(" ");

  //count airflow
  airflowButtonState = digitalRead(airflowButton);
  if (airflowButtonState != lastAirflowState){
    if (airflowButtonState == HIGH){
  lastAirflowState = airflowButtonState;
  //display temps
  temp1Reading = analogRead(temp1Pin);
  temp2Reading = analogRead(temp2Pin);
  int c1 = ( 5.0 * temp1Reading * 100.0) / 1024.0;
  int c2 = ( 5.0 * temp2Reading * 100.0) / 1024.0;
  int f1 = (((c1 * 9.0) / 5.0) + 32.0);
  int f2 = (((c2 * 9.0) / 5.0) + 32.0);

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

#include <LiquidCrystal.h>

#include <Time.h>

const int timeButton = 8;
const int airButton = 9;

int timeState; 
int lastTimeState; 
boolean timer = false;

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup() {
  pinMode(timeButton, INPUT);

void loop() {
  timeState = digitalRead(timeButton);
  if(timeState != lastTimeState) {
    if(timeState == HIGH) {
      if(timer == true) timer = false;
      else timer = true;
  lastTimeState = timeState;
  while(timer == true) timeDisplay();
void mainScreen() {

void timeDisplay() {
  lcd.print(" ");

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!