Single Push button controlled stopwatch

Can anybody please help me with the sketch for a single button controlled stopwatch?! I have a stopwatch sketch that works and counts the time from the moment I press the reset button on arduino. I want to attach a separate push button that RESETS ONLY THE STOPWATCH (because I am using other functions along with stopwatch in the arduino) as soon as it is pressed. Thus, when I press the push button, the stopwatch starts counting..... and resets the value to 00:00:00 once the push button is pressed again (but continues counting from 00:00:00).
Also, if I simply use an increment function to increment the button count every time it is pressed, will it display the 'total number of times I pressed the button' at the last of function?!
Thanks for help!

I cannot see your code, so it is hard to help you and I'm not 100% sure what you are trying to do. A push button is quite simple, and you should be able to find multiple guides on that subject. The deal with the button resetting the stopwatch sometimes but not always is not understod.

So you have some code that does several things? One of them is a stopwatch function. You want to add a button to control the stopwatch function without interfering with any other functions?

Sounds simple enough. I'd probably do it with something like a "stopwatch running" flag. If the button is pressed and the stopwatch is not running start it. If the button is pressed when the stopwatch is running stop it.

So show us your code including what you have tried to get the button working and tell us exactly what it doesn't do that you want it to.

Steve

slipstick:
So you have some code that does several things? One of them is a stopwatch function. You want to add a button to control the stopwatch function without interfering with any other functions?

Sounds simple enough. I'd probably do it with something like a "stopwatch running" flag. If the button is pressed and the stopwatch is not running start it. If the button is pressed when the stopwatch is running stop it.

So show us your code including what you have tried to get the button working and tell us exactly what it doesn't do that you want it to.

Steve

Thank you so much I think you got my point.
I am running 2 hall sensors in a code.

#include<LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 8, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

volatile byte revolutions1;
volatile byte revolutions2;



 unsigned int revv1;
 unsigned int revv2;
 
 unsigned long timeold1;
 unsigned long timeold2;


void setup()
{

lcd.begin(16,2); 
attachInterrupt(0, function1, RISING);
attachInterrupt(1, function2, RISING);


  revolutions1 = 0;
  revolutions2 = 0;
  
   revv1 = 0;
   revv2 = 0;
 
   timeold1 = 0;
   timeold2 = 0;
lcd.setCursor(0,0);
lcd.print("1___");
lcd.setCursor(0,1);
lcd.print("2___");
}

void loop() {

   if (revolutions1 >= 2) 
   { 
     revv1 = 30*1000/(millis() - timeold1)*revolutions1;
     timeold1 = millis();
     revolutions1 = 0;
   }
   if (revolutions2 >= 2) 
   { 
     revv2 = 30*1000/(millis() - timeold2)*revolutions2;
     timeold2 = millis();
     revolutions2 = 0;
   }


lcd.setCursor(9,0);
lcd.print(     revv1);
lcd.setCursor(9,1);
lcd.print(     revv2);
}

void function1()
{
 revolutions1++;
}
void function2()
 {
   revolutions2++;
 }

Now I want to attach a stopwatch along this code that will be printing the time. But this stopwatch resets when I push the Arduino reset button, which I don't want to do because it will reset the Revv count also. SO basically I want to attach a push button that will reset the stopwatch timing without touching the revv readings.

Thanks for the help.

Where is the stopwatch displayed? How is the button connected to the arduino and what have you tried to read it?

Danois90:
Where is the stopwatch displayed?

Here's the code to stopwatch...

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sec=00;
int min=00;
int hr=00;

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

void loop() {

lcd.setCursor(0,0);
lcd.print(hr);
lcd.print(":");
lcd.print(min);
lcd.print(":");
lcd.print(sec);
sec=sec+1;
delay(1000);

if(sec == 60){
  sec=00;
  min=min+1; 
}else;
if(min == 60){
  min=0;
  hr=hr+1;
}else;

lcd.clear();
}

This displays the code on the LCD.

How is the button connected to the arduino

The button is connected in the same way as you connect to light up a LED (The only tutorial I saw everywhere) i.e. One pin to Vcc...one to Vdd and one output at digital pin 2.

what have you tried to read it

I tried the basic sketch to use push button state

buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)

There is no issue in this! The issue lies in combining all of above operations... "running and resetting the stopwatch on push button click"!!!

Thanks for any help!

You're going to need different "stopwatch" code. The delay(1000) in there completely stops the Arduino from doing anything else for a full second...i.e. it can't check for a button press, it can't do anything with the hall sensors etc. You need to use millis() for timing as you do in the other bit of code, like every other stopwatch code I've seen.

And normal pushbuttons only use 2 pins. The way you describe your wiring it's a miracle you haven't shorted out the power yet.

Steve

slipstick:
You're going to need different "stopwatch" code. The delay(1000) in there completely stops the Arduino from doing anything else for a full second...i.e. it can't check for a button press, it can't do anything with the hall sensors etc.

Steve

Okay I sorted out that problem without delay!

You need to use millis() for timing as you do in the other bit of code, like every other stopwatch code I've seen.

Yupp my current code uses millis.

And normal pushbuttons only use 2 pins. The way you describe your wiring it's a miracle you haven't shorted out the power yet.

Yep miracles do happen but most push button I've seen and worked on till now have 3 pins and maybe other projects that use 3 pin push buttons are working miraculously!

O.k. so now we don't know what hardware you're using or what your current code is so there's no chance of helping. I hope it's all working for you. Good luck.

Steve