arduino stopwatch

somebody help me please. i have 2 push button A and B. if i push A, stopwatch will start. if i push B stopwatch will stop. i've already searching in youtube and can't understand. :frowning: :frowning: :frowning: :frowning:

You can't understand what? How to determine that a switch has changed state from not-pressed to pressed? The state change detection example shows how to do that.

What is the "stopwatch" counting up (or down) to? How are you determining how much time to add/subtract? When are you adding/subtracting time?

i've made the program. if i press A, increment will start. but i dont know how to stop the increment if I press B.

i've made the program

But, you didn't post it.

here is my code

#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,10,11,12,13);

#define button1 0
#define button2 1

int time=0;
int code;

void setup() 
{
 lcd.begin(16,2);
 lcd.setCursor(5,0);
 lcd.print("STOPWATCH");
 lcd.setCursor(5,2);
 lcd.print(time);
 lcd.print(" sekon");
 pinMode(button1,INPUT);
 pinMode(button2,INPUT);
}

void loop() 
{
 if(digitalRead(button1)==HIGH&&code==0)
 {
  code=1;
 }
 else if(digitalRead(button1)==HIGH&&code==1)
 {
  code=0; 
 }

 if(code==1)
 { 
  time++;
  delay(10);
  lcd.setCursor(5,2);
  lcd.print(time);
  lcd.print(" sekon");  
 }
 
}
#define button1 0
#define button2 1

Get your switches off of the hardware serial port pins. Use them, and Serial, to talk to the PC, to debug your program.

 pinMode(button1,INPUT);
 pinMode(button2,INPUT);

How are the switches wired? Why are you using external resistors (you are using external resistors, aren't you?) instead of the internal pullup resistors?

You are not reading the second switch. You don't need two, actually. You could use one switch as a toggle - press it once, the first thing happens (timing starts); press it again, the second thing happens (timing stops).

The second switch, if you have one, would be a reset switch, to set the time back to 0.

The state change detection example bears looking at. You want to start, or stop, timing when the switch BECOMES pressed, not when the switch IS pressed.

Incrementing time is dependent on code. You are changing the value of code depending on its current value and the state of the switch. It appears, if the switches are wired properly, that the one switch should start, and stop, incrementing time.

What does the program actually do? How does that differ from what you want?

You are not reading the second switch. You don't need two, actually.

actually I need two button. first button to start and second button to stop

You want to start, or stop, timing when the switch BECOMES pressed, not when the switch IS pressed.

i dont understand what you mean. :grin: and what's wrong with my state change detection???

What does the program actually do?

button A represent sensor1 to start the timer counter and button B represent sensor2 to stop the timer counter

actually I need two button. first button to start and second button to stop

So, create a boolean variable with a meaningful name. code does not mean anything (to me). timerRunning does.

When one switch is pressed, set timerRunning to true. When the other switch is pressed, set timerRunning to false.

Make changes to the time variable only is timerRunning is true.

i dont understand what you mean.

Suppose you press a momentary contact push button switch. At some time, the switch is not pressed. At a later time, the switch is pressed. At a later time, the switch is not pressed. You can read that the switch IS pressed hundreds or thousands of time per second. You can read that the switch is not pressed hundreds or thousands of times, per second.

But, there will be, in that time, only two passes through loop() where the current state of the switch is not the same as the previous state (when the switch becomes pressed and when the switch becomes released).

It is the "switch becomes pressed" event that is of interest to you.

and what's wrong with my state change detection???

You don't have any. You have code to detect the current state. You do not store that state as the previous state so that later you can see if the current state is, or is not, the same as the previous state.

button A represent sensor1 to start the timer counter and button B represent sensor2 to stop the timer counter

It is IMPOSSIBLE that this is what the code currently does, since you do NOT read the state of switch B. Try again. What does the code ACTUALLY do?

int prevbuttonstate=buttonstate;
if(digitalRead(button1)==HIGH)
{
buttonstate=1;
}
else
{
buttonstate=0;
}

if(buttonstate==1&&prevbuttonstate==0)
{
//lcd counter timer
}

now, if button2 pressed, stop the counter. how the command should be written???

 if(digitalRead(button1)==HIGH)
 {
  buttonstate=1;
 }
 else
 {
  buttonstate=0;
 }

8 lines of code to do this:

buttonstate = digitalRead(button1);

Why?

prevbuttonstate (why are you not using the camelCase naming convention?) needs to be global or static.

now, if button2 pressed, stop the counter. how the command should be written???

I'll consider answering the question when you consider using ONE question mark!!!

Check the examples here - https://github.com/RobTillaart/Arduino/tree/master/libraries/StopWatch