Adding a stopwatch

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 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

  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");
  }

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.

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

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

not empty

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

1 Like

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

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

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

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

Then where you check the condition add this line:

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

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

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.

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.

It most certainly did.

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."

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

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.

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

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

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");
  }
 
}

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]

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