Dual stopwatch help

hi im trying to build a dual stopwatch the timers to start together with one switch then stop with two. i am kinda new to this and cant get the code right it ether wont start or they both stop together. here is may last code i tried.

#include <LiquidCrystal.h>

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

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

Serial.begin(9600);

pinMode(8, INPUT);//start button
digitalWrite(8, HIGH);
pinMode(9, INPUT);//stop button left
digitalWrite(9, HIGH);
pinMode(10, INPUT);//stop button right
digitalWrite(10, HIGH);
}
double a = millis();//start time
double b1 ;//left stop time
double b2 ;//right stop time
double c1 = 0;//left et
double c2 = 0;//right et
void loop()
{
lcd.clear();
lcd.print("press start");
delay(100);

if(digitalRead(8) == LOW)
{a = millis();
while(digitalRead(9) == HIGH)
{b1 = millis();
b2 = millis();
c1 = (b1 - a) / 1000;
c2 = (b2 - a) / 1000;
 lcd.clear();
 lcd.print(c1);
 lcd.setCursor(12,0);
 lcd.print("Left");
 lcd.setCursor(0,1);
 lcd.print(c2);
 lcd.setCursor(11,1);
 lcd.print("Right");
 lcd.setCursor(0,0);
 Serial.println(b1);
 Serial.println(b2);
 Serial.println(a);
 Serial.println(c1);
 Serial.println(c2);
 Serial.println("......");
 delay(100);}
 
 if(digitalRead(9) == LOW)
 if(digitalRead(10) == LOW)
 

 {while(digitalRead(8) == HIGH);}}
 }

any help would be appreciated.

I'm not even going to try to work out what a program full of unnamed pins and uncommented single character variables is trying to do. If you know what is connected to pins 8, 9 and 10 and what the values of a, b1, b2 etc mean then you're the only one who can sort it out.

Steve

I updated and named everything

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

@slipstick means like this, the variable names are self explanatory.

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int startbutPin = 8;
int stopbutleftPin = 9;
int stopbutrightPin = 10;

double starttime = millis();
double leftstoptime ;
double rightstoptme ;
double c1 = 0;//left et
double c2 = 0;//right et

void setup()
{
 lcd.begin(16, 2);
 lcd.clear();
 
 Serial.begin(9600);
 
 pinMode(startbutPin, INPUT);
 digitalWrite(startbutPin, HIGH);
 pinMode(stopbutleftPin, INPUT);
 digitalWrite(stopbutleftPin, HIGH);
 pinMode(stopbutrightPin, INPUT);
 digitalWrite(stopbutrightPin, HIGH);
}

Tom... :slight_smile:

Is this possible with one Arduino or will two have to be used

A few things.

  1. fix your formatting. It's a mess. Basic indentation goes a long way to understanding what's going on. Curly braces on their own lines (opening braces at the end of the line where the block starts is fine). No more than one curly brace on a line. Then press ctrl-T in the IDE and it'll look a lot nicer.
  2. use proper names. Name your pins, name your variables properly. That makes life so much easier (and your code much more maintainable than having to hunt down all instances of pin 10).
  3. look up variable types, and how they work. Double is a floating point type, and the wrong type to store a large integer like you get from millis(). The way a float is stored may also cause problems when you try to work with those times. You need an unsigned long for that. By the way, double and float are aliases on Arduinos.
    When you follow those basic practices you have a much better chance of understanding what you're doing.

Mikeh23:
Is this possible with one Arduino or will two have to be used

A single Arduino will be basically idle >99% of the time with the work load of two stop watches and an LCD screen.

i made some changes that was said and this is what i have it will start and stop on the right side but i dont know how to code it to put the left stop in when i do it ether want start or wont stop

#include <LiquidCrystal.h>

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

int startbutPin = 8;//start button
int leftstopbutPin = 9;//left stop button
int rightstopbutPin = 10;//right stop button

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

  Serial.begin(9600);

  pinMode(startbutPin, INPUT);
  digitalWrite(startbutPin, HIGH);
  pinMode(leftstopbutPin, INPUT);
  digitalWrite(leftstopbutPin, HIGH);
  pinMode(rightstopbutPin, INPUT);
  digitalWrite(rightstopbutPin, HIGH);
}
double starttime = millis();
double leftet = 0;
double rightet = 0;
double leftstoptime;
double rightstoptime;

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

  if (digitalRead(startbutPin) == LOW)
  {
    starttime = millis();
    while (digitalRead(rightstopbutPin) == HIGH)
    {
      leftstoptime = millis();
      rightstoptime = millis();
      leftet = (leftstoptime - starttime) / 1000;
      rightet = (leftstoptime - starttime) / 1000;
      lcd.clear();
      lcd.print(leftet);
      lcd.setCursor(12, 0);
      lcd.print("left");
      lcd.setCursor(0, 1);
      lcd.print(rightet);
      lcd.setCursor(11, 1);
      lcd.print("right");
      Serial.println(leftstoptime);
      Serial.println(starttime);
      Serial.println(leftet);
      Serial.println(rightstoptime);
      Serial.println(starttime);
      Serial.println(rightet);
      Serial.println("......");
      delay(100);
    }

    if (digitalRead(rightstopbutPin) == LOW)
    {
      while (digitalRead(startbutPin) == HIGH);
    }
  }
}

any idea on how to do this

Looks like you need a "state machine" :slight_smile:

Maybe something like this:

// Dual stop watch with state machine
// output on serial monitor
// blinking led while running

const byte startPin     = 8;
const byte stopLeftPin  = 9;
const byte stopRightPin = 10;
const byte ledPin       = 13;

boolean buttonStartState     = HIGH;
boolean buttonStopLeftState  = HIGH;
boolean buttonStopRightState = HIGH;
boolean leftIsIn             = false;
boolean rightIsIn            = false;

byte state = 0;     // state machine states: 0 ... 3

unsigned long startTime;
unsigned long stopLeft;
unsigned long stopRight;
unsigned long raceTimeLeft;
unsigned long raceTimeRight;


void setup() {
  Serial.begin(9600);

  pinMode(startPin, INPUT_PULLUP);
  pinMode(stopLeftPin, INPUT_PULLUP);
  pinMode(stopRightPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}


void loop() {
  // read input pins:
  buttonStartState     = digitalRead(startPin);
  buttonStopLeftState  = digitalRead(stopLeftPin);
  buttonStopRightState = digitalRead(stopRightPin);

  switch (state) {
    case 0:        // make ready for start
      Serial.println("press start button");
      state++;
      break;

    case 1:       // waiting for start button press
      if (buttonStartState == LOW) {
        startTime = millis();
        Serial.println("START");
        state++;
      }
      break;

    case 2:      // running - waiting for left and/or right stop button press
      digitalWrite(ledPin, millis() / 128 % 2); // blink the led
      if ( (buttonStopLeftState == LOW) && (leftIsIn == false) ) {
        stopLeft = millis();
        raceTimeLeft = stopLeft - startTime;
        Serial.print("Left is in:  ");
        Serial.println(raceTimeLeft);
        leftIsIn = true;
      }
      if ( (buttonStopRightState == LOW) && (rightIsIn == false) ) {
        stopRight = millis();
        raceTimeRight = stopRight - startTime;
        Serial.print("Right is in: ");
        Serial.println(raceTimeRight);
        rightIsIn = true;
      }
      if ( (leftIsIn == true) && (rightIsIn == true) ) {
        digitalWrite(ledPin, LOW);
        leftIsIn  = false;
        rightIsIn = false;
        state++;
      }
      break;

    case 3:    // print the winner
      if (raceTimeLeft < raceTimeRight) {
        Serial.println("LEFT was first!");
      }
      else {
        Serial.println("RIGHT was first!");
      }
      state = 0;
      break;
  }

}

No LCD, just serial output, so you need to adapt it for your needs.

    while (digitalRead(rightstopbutPin) == HIGH)

Here is one of your problems (some others I pointed out but you ignored).

The code will be stuck in this loop waiting for rightstopbutPin to be pressed but never looks at the other button.

Ok I'll try that later today and see what happens

Thanks that works I have my lcd working to but how do I make it show the time in a decimal form like 1.08 for the 1080 it shows in Mills I tried to divide the Mills by 1000 but then it just shows me 1

Divide by 1000.0 instead (this forces the compiler to use floating point math instead if integer math).

I tried that to I finally got it by using/1000.0, 3