[chronojump] Force sensor using elapsed time library and fixed trigger interfering force values



commit da88f6784c4623e493baa5ad9176ec6619c9ab41
Author: xpadulles <x padulles gmail com>
Date:   Wed Mar 9 11:52:47 2022 +0100

    Force sensor using elapsed time library and fixed trigger interfering force values

 .../ForceSensor-0.5/ForceSensor-0.5.ino            | 43 ++++------------------
 1 file changed, 7 insertions(+), 36 deletions(-)
---
diff --git a/arduino/ForceSensor/ForceSensor-0.5/ForceSensor-0.5.ino 
b/arduino/ForceSensor/ForceSensor-0.5/ForceSensor-0.5.ino
index 7544fbc52..282470816 100644
--- a/arduino/ForceSensor/ForceSensor-0.5/ForceSensor-0.5.ino
+++ b/arduino/ForceSensor/ForceSensor-0.5/ForceSensor-0.5.ino
@@ -24,6 +24,7 @@
 
 #include <HX711.h>
 #include <EEPROM.h>
+#include <elapsedMillis.h>
 
 #define DOUT  5
 #define CLK  4
@@ -52,10 +53,7 @@ boolean capturing = false;
 //wether the tranmission is in binary format or not
 boolean binaryFormat = false;
 
-unsigned long lastTime = 0;
-unsigned long currentTime = 0;
-unsigned long elapsedTime = 0;
-unsigned long totalTime = 0;
+elapsedMicros totalTime = 0;
 
 const int rcaPin = 3;
 
@@ -94,8 +92,7 @@ void loop() {
   if (capturing)
   {
     if (rcaState != lastRcaState) {       //Event generated by the RCA
-      checkTimeOverflow();
-      Serial.print(totalTime);
+      Serial.print(triggerTime);
       Serial.print(";");
 
       if (rcaState) {
@@ -106,12 +103,12 @@ void loop() {
       lastRcaState = rcaState;
 
     } else {                             //If no RCA event, read the force as usual
-      currentTime = micros();
-
-      checkTimeOverflow();
       Serial.print(totalTime);
       Serial.print(";");
+      detachInterrupt(digitalPinToInterrupt(rcaPin));
       Serial.println(scale.get_units(), 2); //scale.get_units() returns a float
+      attachInterrupt(digitalPinToInterrupt(rcaPin), changingRCA, CHANGE);
+      rcaState = digitalRead(rcaPin);
     }
   }
 
@@ -122,13 +119,8 @@ void loop() {
 }
 
 void changingRCA() {
-  //TODO: Check the overflow of the lastTriggerTime
-  detachInterrupt(digitalPinToInterrupt(rcaPin));
-  currentTime = micros();
-
+  triggerTime = totalTime;
   rcaState = digitalRead(rcaPin);
-
-  attachInterrupt(digitalPinToInterrupt(rcaPin), changingRCA, CHANGE);
 }
 
 //In old version SerialEvent() was used but the nano every don't support it
@@ -136,13 +128,6 @@ void processSerial()
 {
   String inputString = Serial.readString();
   String commandString = inputString.substring(0, inputString.lastIndexOf(":"));
-  //  while (Serial.available())
-  //  {
-  //    char inChar = (char)Serial.read();
-  //    inputString += inChar;
-  //    if (inChar == '\n') {
-  //       commandString = inputString.substring(0, inputString.lastIndexOf(":"));
-  //    }
 
   if (commandString == "start_capture") {
     start_capture();
@@ -175,7 +160,6 @@ void start_capture()
 {
   Serial.println("Starting capture...");
   totalTime = 0;
-  lastTime = micros();
   capturing = true;
 }
 
@@ -267,16 +251,3 @@ void get_transmission_format()
     Serial.println("text");
   }
 }
-
-void checkTimeOverflow() {
-
-  //Managing the timer overflow
-  if (currentTime > lastTime)     //No overflow
-    elapsedTime = currentTime - lastTime;
-  else  if (currentTime <= lastTime) //Overflow
-    elapsedTime = (4294967295 - lastTime) + currentTime; //Time from the last measure to the overflow event 
plus the currentTime
-
-  //calculations
-  totalTime += elapsedTime;
-  lastTime = currentTime;
-}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]