How to stop the clock if i press the push button again?

Hello Everyone,
I new here. I simple created a project in which a simple clock with-out RTC module. When i press the button first time the clock start, but i am facing problem when i press the button clock will be stop but it is still running. How can i fix this problem?
Please help me
Thanks in advance.

Here is my code

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int Button1 = 8;
/*int lastButton1State;
  int currentButton1State;*/
boolean State = false;

//change the longest's value to change seconds.
int s = 00;

//change the middle's value to change minutes.
int m = 00;

//change the shortest's value to change hours.
int h = 00;

void setup() {
  lcd.begin(16, 2);
  //Time here is displayed in IST.
  lcd.print("Time -");

  pinMode(Button1, INPUT);
  /*  pinMode(Button2, OUTPUT);*/
  //currentButton1State = digitalRead(Button1);
}






void System()
{

  lcd.setCursor(0, 0);
  lcd.print("Time");
  lcd.setCursor(6, 0);
  if (h < 10)
    lcd.print("0");
  lcd.print(h);
  lcd.print(":");
  if (m < 10)
    lcd.print("0");
  lcd.print(m);
  lcd.print(":");
  if (s < 10)
    lcd.print("0");
  lcd.print(s);


  if (s < 60) {
    delay(987);
    s = s + 01;
  }
  else {
    s = 00;
  }

  if (s > 59) {
    m = m + 01;
  }
  else {
    m = m;
  }

  if (m < 60) {
    m = m;
    h = h;
  }
  else {
    m = 00;
    h = h + 01;
  }

  if (h < 24) {
    h = h;
  }
  else {
    h = 00;
  }




}

/*  else
  {

  }*/

void loop()
{
  while (State == false)
  {
    if (digitalRead(Button1) == HIGH)
    {
      State = true;
    }

  }

  while (State == true)
  {
    System();

    if (digitalRead(Button1) == HIGH)
    {
      State = false;
    }

  }

}



The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

Hi, you have not said how the buttons are connected, or how it it wired. I can see two POSSIBLE issues:

  1. For this case, you will need a resistor (say 10K) to connect the button input to ground. Otherwise the input will be floating and the arduino may read the incorrect value.
    If I were doing this, I would use the internal pull-up i.e. pinMode (Button1, INPUT_PULLUP), and connecting the button between the pin and GND. That is IMHO a much better way to do this.
    Otherwise make sure you have a 10k resistor between the input and gnd.

  2. Buttons "bounce". i.e. your code assumes that the arduino will read with button press as a single action. However, during a button press, the button's contact will "bounce" or "chatter", and thus you might be getting multiple readings on the pin.
    For your circuit, the easiest solution might be to a add a small delay to the button press.
    So, after your "if digitalRead(Button1) == HIGH {}" put delay(100); (try different values to see what works for your buttons).

It might also be worth adding a LED (a 5V LED or a LED with a 470r resistor) to another output pin and light that - or not - depending upon the value of the State variable. This will allow you to test how the circuit is running (i.e. how the button is operating).

Finally, and this is a very, very minor point - you use:
int Button1 = 8;
You could save yourself a tiny bit bit of RAM storage by saying instead:
#define Button1 = 8;

I hope this helps

good advice…

I personally use #define, but a few members correctly point out that const unit8_t or byte/char will help the compiler find errors for ill defined assignments.

Not a major deal.

Hello,
I used the 1st case. I connected button using pull down resistor.