# Adding a stopwatch

**URL:** <https://forum.arduino.cc/t/adding-a-stopwatch/1369632>\
**Category:** Programming\
**Created:** [April 1, 2025, 10:59pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632 "2025-04-01T22:59:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![thetruecthulhu9](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/thetruecthulhu9/32/1201691_2.png) [@thetruecthulhu9](https://forum.arduino.cc/u/thetruecthulhu9)\
**Post date:** [April 1, 2025, 10:59pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/1 "2025-04-01T22:59:00Z")

</div>

heyo im trying to add a stopwatch aspect right at the end of my code, automatically starting when contact0 != contact1. ive really got no clue what i should be doing and videos are confusing me more

```int
int reading0; // the analog reading from the FSR resistor divider
int voltage0; // the analog reading converted to voltage
unsigned long resistance0; // The voltage converted to resistance, can be very big so make "long"
unsigned long conductance0;
long force0; // Finally, the resistance converted to force
long weight0;
int contact0;

int fsr1 = 1; // the FSR and 10K pulldown are connected to a1
int reading1; // the analog reading from the FSR resistor divider
int voltage1; // the analog reading converted to voltage
unsigned long resistance1; // The voltage converted to resistance, can be very big so make "long"
unsigned long conductance1;
long force1; // Finally, the resistance converted to force
long weight1;
int contact1;

int diff; //how balanced the user is 
int alarm = 13; // buzzer or alarm setup

void setup(void) {
  Serial.begin(9600); // We'll send debugging information via the Serial monitor
}

void loop(void) {
  reading0 = analogRead(fsr0);
  
  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
  voltage0 = map(reading0, 0, 1023, 0, 5000);

  if (voltage0 == 0) {
    Serial.println("No pressure");
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V yay math!
    resistance0 = 5000 - voltage0; // voltage0 is in millivolts so 5V = 5000mV
    resistance0 *= 10000; // 10K resistor
    resistance0 /= voltage0;

    conductance0 = 1000000; // we measure in micromhos so
    conductance0 /= resistance0;

    // Use the two FSR guide graphs to approximate the force
    if (conductance0 <= 1000) {
      force0 = conductance0 / 80;
      weight0 = force0 / 9.81;
      Serial.print("weight0 in kilograms: ");
      Serial.println(weight0);
    } else {
      force0 = conductance0 - 1000;
      force0 /= 30;
      weight0 = force0 / 9.81;
      Serial.print("weight0 in kilograms: ");
      Serial.println(weight0);
    }
  }

  reading1 = analogRead(fsr1);

  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
  voltage1 = map(reading1, 0, 1023, 0, 5000);

  if (voltage1 == 0) {
    Serial.println("No pressure");
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V yay math!
    resistance1 = 5000 - voltage1; // voltage1 is in millivolts so 5V = 5000mV
    resistance1 *= 10000; // 10K resistor
    resistance1 /= voltage1;

    conductance1 = 1000000; // we measure in micromhos so
    conductance1 /= resistance1;
    

    // Use the two FSR guide graphs to approximate the force
    if (conductance1 <= 1000) {
      force1 = conductance1 / 80;
      weight1 = force1 / 9.81;
      Serial.print("weight1 in kilograms: ");
      Serial.println(weight1);
    } else {
      force1 = conductance1 - 1000;
      force1 /= 30;
      weight1 = force1 / 9.81;
      Serial.print("weight1 in kilograms: ");
      Serial.println(weight1);
   }
  }
  if (weight1>weight0) { 
     diff = (weight1-weight0);
} else if (weight0>weight1) {
     diff = (weight0-weight1);
} else if (weight1==weight0) {
    diff = 0;
}
  if (diff > 2) {
    Serial.println("redistribute weight");
  }

  if (reading0 = 0) {
    contact0 = 0;
  } else if (reading0 > 0) {
    contact0 = 1;
  }
  if (reading1 = 0) {
    contact1 = 0;
  } else if (reading1 > 0) {
    contact1 = 1;
  }
 int count = 0;
  start = time.time();
  if (contact0 != contact1){
      count = count + 1;
      if (count < 200);
        Serial.println ("realine gait");
  }

  Serial.println("--------------------");
  delay(1000);

}

```

this is the specific bit of code im looking at fiddling with

```arduino
  if (reading0 = 0) {
    contact0 = 0;
  } else if (reading0 > 0) {
    contact0 = 1;
  }
  if (reading1 = 0) {
    contact1 = 0;
  } else if (reading1 > 0) {
    contact1 = 1;
  }
 int count = 0;
  start = time.time();
  if (contact0 != contact1){
      count = count + 1;
      if (count < 200);
        Serial.println ("realine gait");
  }

```

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [April 1, 2025, 11:33pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/2 "2025-04-01T23:33:04Z")

</div>

> [@thetruecthulhu9](#):
>
> heyo im trying to add a stopwatch aspect right at the end of my code,

Do you mean at the end of the text part of loop(), or do you mean when the code stops executing?

If you mean at the end of the written text of loop(), then it will be executed every time loop() starts over. If you mean at the point in time when your code stops being executed, that will be when you turn the power off.

---

<div class="post-metadata">

**Author:** ![thetruecthulhu9](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/thetruecthulhu9/32/1201691_2.png) [@thetruecthulhu9](https://forum.arduino.cc/u/thetruecthulhu9)\
**Post date:** [April 2, 2025, 12:04am UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/3 "2025-04-02T00:04:19Z")

</div>

im looking at the end of the loop, rn ive got everything functioning in that loop

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [April 2, 2025, 12:10am UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/4 "2025-04-02T00:10:47Z")

</div>

So you are wanting the stopwatch to start over each time loop() loops, right?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 2, 2025, 4:50am UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/5 "2025-04-02T04:50:53Z")

</div>

> [@thetruecthulhu9](#):
>
> ```arduino
> if (contact0 != contact1){
> Serial.print(millis()); // if you divide millis() by 1000, you get seconds elapsed
> count = count + 1;
> if (count < 200);
> Serial.println ("realine gait");
> }
> 
> ```

not empty

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [April 2, 2025, 6:48am UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/6 "2025-04-02T06:48:03Z")

</div>

> [@thetruecthulhu9](#):
>
> `if (reading0 = 0) {`

Note that a single '=' is an assignment, to test an equality you must use a double '==' instead.

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [April 2, 2025, 7:39am UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/7 "2025-04-02T07:39:32Z")

</div>

> [@thetruecthulhu9](#):
>
> add a stopwatch aspect right at the end of my code, automatically starting when contact0 != contact1.

Would you want to restart the stopwatch each time (contact0 != contact1), or just start the stopwatch the first time that happens?  
You could declare a global variabile to hold millis() return value when (contact0 != contact1) then when you want to know how long it's been running, subtract that variable from the current millis() value

---

<div class="post-metadata">

**Author:** ![thetruecthulhu9](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/thetruecthulhu9/32/1201691_2.png) [@thetruecthulhu9](https://forum.arduino.cc/u/thetruecthulhu9)\
**Post date:** [April 2, 2025, 8:30am UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/8 "2025-04-02T08:30:17Z")

</div>

sorry no, i want the stopwatch to restart each time contact0 !=contact1, id thought that loop was in refrence to the displayed information not how often it was checked

---

<div class="post-metadata">

**Author:** ![thetruecthulhu9](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/thetruecthulhu9/32/1201691_2.png) [@thetruecthulhu9](https://forum.arduino.cc/u/thetruecthulhu9)\
**Post date:** [April 2, 2025, 8:35am UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/9 "2025-04-02T08:35:14Z")

</div>

i want it to restart each time (contact0 != contact1)

youve kinda lost me with your explaination, sorry u might have to treat me like an idiot cos i dont have the foundational knowledge

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [April 2, 2025, 8:57am UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/10 "2025-04-02T08:57:51Z")

</div>

Add a global variable like this at the top of your code

> [@thetruecthulhu9](#):
>
> ```arduino
> unsigned long stopwatch; // <-- add this line
> int reading0; // the analog reading from the FSR resistor divider
> int voltage0; // the analog reading converted to voltage
> 
> ```

Then where you check the condition add this line:

> [@thetruecthulhu9](#):
>
> ```arduino
> if (contact0 != contact1){
> stopwatch = millis(); // <- add this line
> count = count + 1;
> if (count < 200);
> Serial.println ("realine gait");
> }
> 
> ```

If stopwatch == 0 it means the stopwatch has never been started.

When you want to know how long the stopwatch has been running use millis()-stopwatch.

Note that the stopwatch will keep being reset every time contact0 != contact1. If you wanted to only reset it when that condition changes from false to true, it would require further code modification

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 3, 2025, 4:21pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/11 "2025-04-03T16:21:22Z")

</div>

> [@thetruecthulhu9](#):
>
> stopwatch to restart each time contact0 !=contact1,

What about Post #5 do you not consider a stopwatch? (lap/split time)

---

<div class="post-metadata">

**Author:** ![thetruecthulhu9](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/thetruecthulhu9/32/1201691_2.png) [@thetruecthulhu9](https://forum.arduino.cc/u/thetruecthulhu9)\
**Post date:** [April 3, 2025, 5:15pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/12 "2025-04-03T17:15:50Z")

</div>

it didnt work with what i was atempting and also ive got no clue what im looking at, im functionally a novice and throwing code at me with no explanation wasnt helpful

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [April 3, 2025, 5:31pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/13 "2025-04-03T17:31:32Z")

</div>

> [@thetruecthulhu9](#):
>
> it didnt work with what i was atempting and also ive got no clue what im looking at, im functionally a novice and throwing code at me with no explanation wasnt helpful

Who are you saying that to?  
If someone here shows you some code, the person showing it to you wants to help you. If you don't understand some code that is offered to you, then you aren't going to learn if you don't understand it _and_ don't ask for more information on how it works.

---

<div class="post-metadata">

**Author:** ![thetruecthulhu9](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/thetruecthulhu9/32/1201691_2.png) [@thetruecthulhu9](https://forum.arduino.cc/u/thetruecthulhu9)\
**Post date:** [April 3, 2025, 5:36pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/14 "2025-04-03T17:36:54Z")

</div>

sorry that was a reply to xfpd. at the time i didnt reply becauyse there were replies like yours that were more helpful to me, like yours, dave.

speaking of, ive added your code to my work and mostly doing what i need, only in reverse. im not sure if its a wider issue or not.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 3, 2025, 5:38pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/15 "2025-04-03T17:38:27Z")

</div>

> [@thetruecthulhu9](#):
>
> it didnt work with what i was atempting

It most certainly did.

> [@thetruecthulhu9](#):
>
> throwing code at me with no explanation wasnt helpful

The explanation most certainly WAS/IS in my response. Lay off the attitude. What you mean with your response is "do my homework for me."

> [@thetruecthulhu9](#):
>
> ive got no clue what im looking at

Numbers. In particular a "stopwatch" measured in milliseconds or seconds. (but, because you spent zero time reading it, you "got no clue"). Try reading

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [April 3, 2025, 5:41pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/16 "2025-04-03T17:41:38Z")

</div>

> [@thetruecthulhu9](#):
>
> ive added your code to my work and mostly doing what i need, only in reverse.

If you can explain in words what you want it to do, and how what the code I provided is doing the reverse, then I may be able to help you to change it so that it does what you want it to do.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 3, 2025, 5:44pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/17 "2025-04-03T17:44:02Z")

</div>

Same subject (stopwatch, time difference) in a different topic:

> [@Recording time difference between input0 and input1](https://forum.arduino.cc/t/recording-time-difference-between-input0-and-input1/1367126):
>
> heya, im trying to build a setup that registers a force difference between two FSRs and the time between inputs, where it alerts you when the time is greater than half a second and the weight is more than 5kg. Using an arduino uno, I have my code recording weight but im really not sure where to go from here int fsr0 = 0; // the FSR and 10K pulldown are connected to a0 int reading0; // the analog reading from the FSR resistor divider int voltage0; // th…

---

<div class="post-metadata">

**Author:** ![thetruecthulhu9](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/thetruecthulhu9/32/1201691_2.png) [@thetruecthulhu9](https://forum.arduino.cc/u/thetruecthulhu9)\
**Post date:** [April 3, 2025, 6:03pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/18 "2025-04-03T18:03:56Z")

</div>

yeah ofc, ive got two FSRs set up, what i want is to read when one is reciving an input and measure the time until the other also recives an input. if that time difference is over a second i want it to give an alert, which is the "realign gait" part.

having added your code it presents the text when one of the FSR's has pressure through it, and looses it when the pressure is released, but it doesnt funtion for the other FSR, despite that one getting all its weight and weight distribution readings

```unsigned
int fsr0 = 0; // the FSR and 10K pulldown are connected to a0
int reading0; // the analog reading from the FSR resistor divider
int voltage0; // the analog reading converted to voltage
unsigned long resistance0; // The voltage converted to resistance, can be very big so make "long"
unsigned long conductance0;
long force0; // Finally, the resistance converted to force
long weight0;
int contact0;

int fsr1 = 1; // the FSR and 10K pulldown are connected to a1
int reading1; // the analog reading from the FSR resistor divider
int voltage1; // the analog reading converted to voltage
unsigned long resistance1; // The voltage converted to resistance, can be very big so make "long"
unsigned long conductance1;
long force1; // Finally, the resistance converted to force
long weight1;
int contact1;

int diff; //how balanced the user is 
int alarm = 13; // buzzer or alarm setup

long millisec; // guage if timer is running
long startTime; //
int contDiff; 
int count;

void setup() {
  Serial.begin(9600); // We'll send debugging information via the Serial monitor
}
void loop(){
   mass();
   stopwatch1();
     Serial.println("--------------------");
  delay(1000);
}

void mass() {
  reading0 = analogRead(fsr0);
  
  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
  voltage0 = map(reading0, 0, 1023, 0, 5000);

  if (voltage0 == 0) {
    Serial.println("No pressure");
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V yay math!
    resistance0 = 5000 - voltage0; // voltage0 is in millivolts so 5V = 5000mV
    resistance0 *= 10000; // 10K resistor
    resistance0 /= voltage0;

    conductance0 = 1000000; // we measure in micromhos so
    conductance0 /= resistance0;

    // Use the two FSR guide graphs to approximate the force
    if (conductance0 <= 1000) {
      force0 = conductance0 / 80;
      weight0 = force0 / 9.81;
      Serial.print("weight0 in kilograms: ");
      Serial.println(weight0);
    } else {
      force0 = conductance0 - 1000;
      force0 /= 30;
      weight0 = force0 / 9.81;
      Serial.print("weight0 in kilograms: ");
      Serial.println(weight0);
    }
  }

  reading1 = analogRead(fsr1);

  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
  voltage1 = map(reading1, 0, 1023, 0, 5000);

  if (voltage1 == 0) {
    Serial.println("No pressure");
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V yay math!
    resistance1 = 5000 - voltage1; // voltage1 is in millivolts so 5V = 5000mV
    resistance1 *= 10000; // 10K resistor
    resistance1 /= voltage1;

    conductance1 = 1000000; // we measure in micromhos so
    conductance1 /= resistance1;
    

    // Use the two FSR guide graphs to approximate the force
    if (conductance1 <= 1000) {
      force1 = conductance1 / 80;
      weight1 = force1 / 9.81;
      Serial.print("weight1 in kilograms: ");
      Serial.println(weight1);
    } else {
      force1 = conductance1 - 1000;
      force1 /= 30;
      weight1 = force1 / 9.81;
      Serial.print("weight1 in kilograms: ");
      Serial.println(weight1);
   }
  }
  if (weight1 > weight0) { 
     diff = (weight1 - weight0);
} else if (weight0 > weight1) {
     diff = (weight0 - weight1);
} else if (weight1 == weight0) {
    diff = 0;
}
  if (diff > 2) {
    Serial.println("redistribute weight");
  }
   if (voltage0 == 0) {
    contact0 = 0;
  } else {
    contact0 = 1;
  }
  if (voltage1 == 0) {
    contact1 = 0;
  } else {
    contact1 = 1;
  }
if (contact0 = contact1) {
  contDiff = false;
} else {
  contDiff = true;
}

}

void stopwatch1(){
  if (contDiff == false){
  stopwatch = millis(); 
      count = count + 1;
      if (count > 1000);
        Serial.println ("realine gait");
  }
 
}

```

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [April 3, 2025, 6:11pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/19 "2025-04-03T18:11:41Z")

</div>

I'm not going to have time to look at that today. I'll try and find time to work it out tomorrow (if someone else hasn't told you why it doesn't work in the mean time)  
[Edit, just saw post #17. It's considered bad to have duplicate posts. You will upset people by doing that]

---

<div class="post-metadata">

**Author:** ![thetruecthulhu9](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/thetruecthulhu9/32/1201691_2.png) [@thetruecthulhu9](https://forum.arduino.cc/u/thetruecthulhu9)\
**Post date:** [April 3, 2025, 6:13pm UTC](https://forum.arduino.cc/t/adding-a-stopwatch/1369632/20 "2025-04-03T18:13:50Z")

</div>

no worries, ive got other non-coding projects that i need to get on with in the meantime, thanks for the help sofar

[Next page](https://forum.arduino.cc/t/adding-a-stopwatch/1369632.md?page=2)
