control a stopwatch with two buttons

Hi guys, I want to make a system ( controlling a stopwatch using two push buttons) the system that I want is…

let say button 1 is for START button, and button 2 is for STOP button. My idea now is to make two loops.
Literally, button 1 is in loop 1 and button 2 is in loop 2. So, to make that I think it is good to use do_while loop. I have tried to make the program… but it didn’t go so well. One more thing, between ARDUINO uno and my stopwatch… i use transistor’s switch control and I’d done the experiment on my circuit and nothing is wrong. so I think my problem now is in the program.

I will be very appreciate if u guys help me.

here is my program sketch

do_while.ino (933 Bytes)

I'm sorry about earlier message... this is my sketch. Thank you for your time :)

int SW1 = 9; // button 1 int SW2 = 6; // button 2 int ST = 13; // stopwatch

void setup() { pinMode (SW1, INPUT); // button 1 as INPUT pinMode (SW2, INPUT); // button 2 as INPUT pinMode (ST, OUTPUT); // stopwatch as OUTPUT // put your setup code here, to run once:

}

void loop() { //first loop

do { if (digitalRead (SW1) == HIGH){ //if button 1 pushed digitalWrite (ST, HIGH);} //transistor's switch control turn ON, } while (digitalRead (SW1) == LOW); //current flow into the stopwatch (START) digitalWrite (ST, LOW); // if not pushed TR switch control remain OFF

do { if (digitalRead (SW2) == HIGH){ //if button 2 pushed digitalWrite (ST, HIGH);} //TR switch control turn ON }while (digitalRead (SW2) == LOW); //current flow into the stopwatch (STOP) digitalWrite (ST, LOW);

}

Look at the first do/while loop

  do
  {
    if (digitalRead (SW1) == HIGH)  //if button 1 pushed
    {
      digitalWrite (ST, HIGH);
    }            //transistor's switch control turn ON,
  }
  while (digitalRead (SW1) == LOW);    //current flow into the stopwatch (START)

How likely is it that SW1 will be HIGH while SW1 is LOW ?

A stopwatch using 2 buttons should be easy. When the start button becomes pressed save the value of millis(). This is the start time When the stop button becomes pressed save the value of millis(). This is the stop time Subtract start time from stop time to get the elapsed time

in my understanding OP has an external stopwatch

OK

If the start button becomes pressed turn off the stop transistor and turn on the start transistor If the stop button becomes pressed turn off the start transistor and turn on the stop transistor

No need to mess with do/while loops when the loop() function will enable you to monitor inputs several thousands of times per second.

thank you for your reply. and sorry for my late reply

UKHeliBob: Look at the first do/while loop

  do
  {
    if (digitalRead (SW1) == HIGH)  //if button 1 pushed
    {
      digitalWrite (ST, HIGH);
    }            //transistor's switch control turn ON,
  }
  while (digitalRead (SW1) == LOW);    //current flow into the stopwatch (START)

How likely is it that SW1 will be HIGH while SW1 is LOW ?

what i think about this code is. when SW1 == LOW, the program will repeat the loop 1. and then when I turn SW1 == HIGH, the program will jump of from loop 1 to loop 2 (the second do_while loop). is it wrong? I am sorry because I am not used with programmig.

[/quote] A stopwatch using 2 buttons should be easy. When the start button becomes pressed save the value of millis(). This is the start time When the stop button becomes pressed save the value of millis(). This is the stop time Subtract start time from stop time to get the elapsed time

[/quote]

in here do you mean by using arduino's time measurement?

UKHeliBob: OK

If the start button becomes pressed turn off the stop transistor and turn on the start transistor If the stop button becomes pressed turn off the start transistor and turn on the stop transistor

No need to mess with do/while loops when the loop() function will enable you to monitor inputs several thousands of times per second.

do you mean like changing my code into this?

void loop() {

if (digitalRead (SW1) == HIGH){ digitalWrite (ST, HIGH);} else digitalWrite (ST, LOW);

if (digitalRead (SW2) == HIGH){ digitalWrite (ST, HIGH);} else digitalWrite (ST, LOW);

}

Juraj: in my understanding OP has an external stopwatch

do you mean OP amp?

in here do you mean by using arduino's time measurement?

That is what I meant but as was pointed out you want to use an external stopwatch.

do you mean like changing my code into this?

Not quite. For instance your proposed code write to the ST pin for both start and stop. Is that what you want/need to do or are the switches separate on the stopwatch ? A description of the stopwatch and how it works would be helpful.

in my understanding OP has an external stopwatch

In this context OP refers to the Original Poster, ie you, and not an OP amp

Thank you for your answer. down here is my actual circuit and stopwatch sorry if you cant see it clearly.
I am so sorry because I don’t know how to put image in this reply box accept attach the image file.

in this picture the circle mark I wrote is the button.

UKHeliBob: Not quite. For instance your proposed code write to the ST pin for both start and stop. Is that what you want/need to do or are the switches separate on the stopwatch ? A description of the stopwatch and how it works would be helpful.

yes the switches is saperated from the stopwatch. How I want it to work is to make the SW1 as the start button and SW2 as the stop button, how actually this circuit move is. when push the SW1, the transistor Switch circuit ( circuit between SW1 and SW2 in the picture)will turn ON and then, at the moment the positive pole and negative pole of the stopwatch will be connected mean that getting an electric signal (for this moment the stopwatch will start running). after releasing the button the transistor switch circuit will turn OFF and the stopwatch not getting any signal so the time will continue running.

when SW2 pushed, same as when pushing the SW1 button. the difference is just when the stopwatch detect any electric signal the stopwatch will stop running.

sorry if my describe is not so good.

have you any programming experience? write a simple stopwatch program in some IDE where you can run it and debug.

Juraj: have you any programming experience? write a simple stopwatch program in some IDE where you can run it and debug.

unfortunately i'm not, I am very new to programming. never learnt it in school

but I learnt arduino's basic programming by myself a bit. because my FYP need to use arduino.

void loop() {
  
   if (digitalRead (SW1) == HIGH){ 
  digitalWrite (ST, HIGH);}          
   else 
   digitalWrite (ST, LOW);           
  
    if (digitalRead (SW2) == HIGH){
     digitalWrite (ST, HIGH);}            
    else
  digitalWrite (ST, LOW);               
   
}

From your description the code above needs to be changed to write to a different output pin when SW2 goes HIGH than when SW1 goes HIGH, not the same pin.

As a matter of interest, why do you want to use an Arduino for this application when simple switches would do the job ?

UKHeliBob: From your description the code above needs to be changed to write to a different output pin when SW2 goes HIGH than when SW1 goes HIGH, not the same pin.

As a matter of interest, why do you want to use an Arduino for this application when simple switches would do the job ?

do you mean that I need to make two pins of ST pin?

actually my project is to make a sensor in a track field event such as 100m and so on. at the goal line I will put IR sensor to measure the time of the running. so as my final target is i want to replace SW1 with a starting gun and replace SW2 with the IR sensor. for now my IR sensor, starting gun and transistor switch circuit works like I want. and now I just need to make a program to run this system. to make my experiment simpler i just used these two switches.

do you mean that I need to make two pins of ST pin?

If the stopwatch has separate start and stop switches then, yes, you need to use separate outputs from the Arduino to control them. One to start the stopwatch and one to stop the stopwatch.

It is difficult to see from your photo, but have you currently got the stop and start working with mechanical external switches and now you want to move to using Arduino outputs to do the same ?

UKHeliBob: If the stopwatch has separate start and stop switches then, yes, you need to use separate outputs from the Arduino to control them. One to start the stopwatch and one to stop the stopwatch.

It is difficult to see from your photo, but have you currently got the stop and start working with mechanical external switches and now you want to move to using Arduino outputs to do the same ?

yes, i want to do it the same. I'll try try it tomorrow (here japan is night right now). Thanks for the sharing :)

when you said I need to use 2 pins instead of 1 ST pin, I don't know where to put the second pin in this circuit. here is the circuit's picture. and I think I have make it clearer than yesterday. Sorry for taking your time again....

here is the circuit's picture.

Looks up Looks down Looks left Looks right Looks forward Looks back

No picture that I can see