Help with modifying stopwatch code

Hello!

I need some help with modifying a existing code for my projekt.

This code works fine as it is but i ned to have it working with only one button.

When the button is pushed in and helld the stopwatch shuld start and run, when the button is relesed it shuld stop and reset.

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
 
void setup()
{
lcd.begin(16, 2);
lcd.clear();
 
Serial.begin(9600);
 
pinMode(8, INPUT);
digitalWrite(8, HIGH);
pinMode(9, INPUT);
digitalWrite(9, HIGH);
}
double i = 0;
double a = millis();
double c ;
 
void loop()
{
lcd.clear();
lcd.print("press start");
delay(100);
 
if(digitalRead(8) == LOW)
{
 
lcd.clear();
a = millis();
while(digitalRead(9) == HIGH)
{
 
c = millis();
i = (c - a) / 1000;
lcd.print(i);
lcd.setCursor(7,0);
lcd.print("Sec's");
lcd.setCursor(0,0);
Serial.println(c);
Serial.println(a);
Serial.println(i);
Serial.println("......");
delay(100);
}
 
if(digitalRead(9) == LOW)
{
while(digitalRead(8) == HIGH)
{
lcd.setCursor(0,0);
lcd.print(i);
lcd.setCursor(11,0);
lcd.print("");
lcd.setCursor(0,0);
delay(100);
}
}
}
}

Hope you outh there whit greater experiance can help me out!

Sorry for the bad english,, hope its readable.

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

void setup()
{
   lcd.begin(16, 2);
   lcd.clear();

   Serial.begin(9600);

   pinMode(8, INPUT);
   digitalWrite(8, HIGH);
   pinMode(9, INPUT);
   digitalWrite(9, HIGH);
}
double i = 0;
double a = millis();
double c ;

void loop()
{
   lcd.clear();
   lcd.print("press start");
   delay(100);

   if (digitalRead(8) == LOW)
   {

      lcd.clear();
      a = millis();
      while (digitalRead(9) == HIGH)
      {

         c = millis();
         i = (c - a) / 1000;
         lcd.print(i);
         lcd.setCursor(7, 0);
         lcd.print("Sec's");
         lcd.setCursor(0, 0);
         Serial.println(c);
         Serial.println(a);
         Serial.println(i);
         Serial.println("......");
         delay(100);
      }

      if (digitalRead(9) == LOW)
      {
         while (digitalRead(8) == HIGH)   //  will this ever execute?  (pin 8 low to enter first if)
         {
            lcd.setCursor(0, 0);
            lcd.print(i);
            lcd.setCursor(11, 0);
            lcd.print("");
            lcd.setCursor(0, 0);
            delay(100);
         }
      }
   }
}

I used the IDE's autoformat tool (ctrl-t or Tools, Auto format) to indent your code.

Tnx :slight_smile: I learn every day!

If somone here have another idea how i can solve my issue it will be helpful.

A link to another similar code or somthing like thet.

The funktion i ned is a counter thet counts when a button is helld in and stops/resets when the button is released.

Take a look at the State Change Detection example in the IDE (File->Examples->02.Digital->StateChangeDetection) and it will show you how to detect when your button changes state (HIGH -> LOW or LOW -> HIGH).

When one of these events is detected, you can react:

HIGH -> LOW = start so record the state time and set a variable to indicate the stopwatch is running
LOW -> HIGH = button released so stop the stopwatch, clear the display and ....

If neither event has happened, then look at your stopwatch running variable to see if you need to update the display with elapsed time or just do nothing

Give it a try. This forum is about people helping people fix their code, not writing code for them.

I've just replied to a tutorial you will find useful

if buttonPressed and if buttonReleased function example!