STOPWATCH ... Programming issues

I’m new here and would like to get some help debugging my code. This stopwatch should stop when PB is pressed and released, and then start again when pressed and released.

Thank you in advance.

Please help debug this code: Arduino stopwatch using pushbutton. It should start when pressing pushbutton and stop again when pushbutton is pressed.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int curMin = 0;
int curSec = 0;
int push_button1 = 8;
int pbState = LOW;
int newState = LOW;
bool watchRunning = false;

void setup()
{
pinMode(push_button1, INPUT);
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Ready!");
delay(1000);
lcd.clear();

lcd.print("Set!");
delay(1000);
lcd.clear();

lcd.print("Go!!!");
delay(1000);
lcd.clear();
  
lcd.setCursor(6,0);
lcd.print("00:00");
}

void runWatch()
{
lcd.setCursor(0,0);


if (curSec < 10){
lcd.setCursor (10,0);
lcd.print(curSec);
}

else if (curSec < 60){
lcd.setCursor (9,0);
lcd.print(curSec);
}

else {
curMin++;
curSec = 0;
lcd.setCursor (9,0);
lcd.print("00");
  
if (curMin < 10) {
lcd.setCursor (7,0);
lcd.print (curMin);
}
else if (curMin < 60){
lcd.setCursor(6,0);
lcd.print (curMin);
}
else {
curMin = 0;
curSec = 0;
lcd.setCursor(6,0);
lcd.print("Run! 00:00");
}
}
  
delay (1000);
curSec++;
}

void stopWatch()
{
lcd.setCursor(0,0);
lcd.print("Stop!");
}

void loop() {
  
newState = digitalRead(push_button1);
  
if (newState != pbState)
{
pbState = LOW;
}
  
if(newState == HIGH)
{
if (watchRunning == true)
{

stopWatch();
watchRunning = false;
  
}
else
{

runWatch();
watchRunning = true;
  
}
}

else {
if (watchRunning == false)
{
  
watchRunning = true;
runWatch();
  
  
}
else
{
  
watchRunning = false;
stopWatch();
  
  
}
  
  
}
}

/code]

(deleted)

This stopwatch should stop when PB is pressed and released, and then start again when pressed and released.

What does it do at the moment ?

Have you tried adding some Serial.print()s to show which sections of code are running and the value of pertinent variables at that time ?

How is the input wired ?
Do you have pull up or pull down resistors in place or is the input floating at an unknown voltage ?

My mistake. Well it starts with a screen "Stop! 00:00". I need it to start with "Run! 00:00" and then counting. Also, when PB is pressed it need to stop and displays "STOP" and then when is pressed again it needs to start over "Run! 00:00".
I hope this helps. Thank you!

Where in teh code does it display "Run!"

And the answers to the other questions ?

This should do what you want:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


bool WatchRunning = false;
unsigned long StartTime = 0;


const byte StartStopButtonPin = 8;
const boolean PRESSED = LOW;  // Active Low
const boolean RELEASED = !PRESSED;


void setup()
{
  pinMode(StartStopButtonPin, INPUT_PULLUP);


  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Ready!");
  delay(1000);
  lcd.clear();


  lcd.print("Set!");
  delay(1000);
  lcd.clear();


  lcd.print("Go!!!");
  delay(1000);
  lcd.clear();


  startWatch();
}


void startWatch()
{
  WatchRunning = true;
  StartTime = millis();
  lcd.setCursor(0, 0);
  lcd.print("Run!  ");
  displayTime();
}


void stopWatch()
{
  WatchRunning = false;
  lcd.setCursor(0, 0);
  lcd.print("Stop!");
}


void displayTime()
{
  static unsigned oldElapsedTime = 9999;


  unsigned elapsedTime = (millis() - StartTime) / 1000;
  unsigned elapsedMinutes = (elapsedTime / 60) % 100;
  unsigned elapsedSeconds = elapsedTime % 60;


  if (elapsedTime != oldElapsedTime)
  {
    // Time to update the display
    oldElapsedTime = elapsedTime;


    lcd.setCursor(6, 0);


    if (elapsedMinutes < 10)
      lcd.print(' ');
    lcd.print(elapsedMinutes);


    lcd.print(':');


    if (elapsedSeconds < 10)
      lcd.print('0');
    lcd.print(elapsedSeconds);
  }
}


void loop()
{
  boolean newState = digitalRead(StartStopButtonPin) == PRESSED;
  static boolean oldState = RELEASED;
  static unsigned long lastStateChangeTime = 0;


  if (WatchRunning)
    displayTime();


  // Check for new button state, debounced
  if (newState != oldState && millis() - lastStateChangeTime > 30)
  {
    // State has changed
    oldState = newState;
    lastStateChangeTime = millis();


    // If state now RELEASED...
    if (newState == RELEASED)
    {
      // Switch states
      if (WatchRunning)
        stopWatch();
      else
        startWatch();
    }
  }
}

Thanks for letting me know. Code was edited. I'm also attaching my circuit.

I found my error so thanks for the help and for your code.

ok. Now I need to add a second PB. PB1 needs to start the count and PB2 stops the count. My problem is that it only starts counting when I press down PB1. PB2 does stop the count. However I need to press PB1 and start the count without holding it down. Thanks

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int curMin = 0;
int curSec = 0;
int push_button1 = 8;
int push_button2 = 9;
int pbState = LOW;
int newState = LOW;
bool watchRunning;

void setup() 
{
pinMode(push_button1, INPUT);
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Ready!");
delay(1000);
lcd.clear();

lcd.print("Set!");
delay(1000);
lcd.clear();

lcd.print("Go!!!");
delay(1000);
lcd.clear();
  
lcd.setCursor(1,0);
lcd.print("Run! 00:00");
  }

 void runWatch()
 {
    lcd.setCursor(0,0);
      
    if (curSec < 10){
    lcd.setCursor (10,0);
    lcd.print(curSec);
  }

  else if (curSec < 60){
    lcd.setCursor (9,0);
    lcd.print(curSec);
  }

  else {
    curMin++;
    curSec = 0;
    lcd.setCursor (9,0);
    lcd.print("00");
    
    if (curMin < 10) {
      lcd.setCursor (7,0);
      lcd.print (curMin);       
    }
    else if (curMin < 60){
      lcd.setCursor(6,0);
      lcd.print (curMin);
    }
    else {
      curMin = 0;
      curSec = 0;
      lcd.setCursor(6,0);
      lcd.print("00:00");
    }
    }
  
    delay (1000);
    curSec++;
  }

void stopWatch()
  {
    lcd.setCursor(1,0);
    lcd.print("Stop!");
  }

void loop() {
  
  pbState = digitalRead(push_button1);
  newState = digitalRead(push_button2);
  

  if(pbState == HIGH)
  {
    if (watchRunning == true)
    {
     
      stopWatch();
      watchRunning = false;
     
    }
    else
    {
     
      watchRunning = false;
      lcd.setCursor(1,0);
      lcd.print("Run! 00:00");   
      runWatch();
  
    }
  }

   if(newState == HIGH)
   { 
    if (watchRunning == true)
    {
      runWatch();
      watchRunning = false;
      
    }
    else
    { 
      watchRunning = false;
      stopWatch();      
  
  }
}
}

I need to press PB1 and start the count without holding it down.

You need to detect when PB1 becomes pressed not when it is pressed. Look at the StateChangeDetection example in the IDE