[chronojump] ForceSensorLCD. Added RCA triggers in arduino code



commit ddcdec6ab76d405b070a0b85c14e3bed879dcd6d
Author: Xavier Padullés <x padulles gmail com>
Date:   Fri Jul 3 15:04:00 2020 +0200

    ForceSensorLCD. Added RCA triggers in arduino code

 arduino/ForceSensorLCD/ForceSensorLCD.ino | 148 +++++++++++++++++++-----------
 1 file changed, 94 insertions(+), 54 deletions(-)
---
diff --git a/arduino/ForceSensorLCD/ForceSensorLCD.ino b/arduino/ForceSensorLCD/ForceSensorLCD.ino
index d9821209..caa9f410 100644
--- a/arduino/ForceSensorLCD/ForceSensorLCD.ino
+++ b/arduino/ForceSensorLCD/ForceSensorLCD.ino
@@ -32,7 +32,7 @@
 #define CLK  4
 
 //Version number //it always need to start with: "Force_Sensor-"
-String version = "Force_Sensor-0.4";
+String version = "Force_Sensor-0.5";
 
 
 int tareAddress = 0;
@@ -74,8 +74,8 @@ unsigned long elapsedTime = 0;
 unsigned long totalTime = 0;
 unsigned int samples = 0;
 
-const int buttonPin = 6;
-int buttonState;
+const int button1Pin = 6;
+int button1State;
 float voltage;
 
 
@@ -88,13 +88,22 @@ float measured = scale.get_units();
 
 LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
 
+
+const int rcaPin = 3;
+
+unsigned long triggerTime = 0;
+bool rcaState = digitalRead(rcaPin);
+bool lastRcaState = rcaState;
+
 void setup() {
-  pinMode(buttonPin, INPUT);
+  pinMode(button1Pin, INPUT);
   analogWrite(6, 20);
   lcd.begin(16, 2);
   Serial.begin(115200);
 
-  if (buttonState == 0) {
+  attachInterrupt(digitalPinToInterrupt(rcaPin), changingRCA, CHANGE);
+
+  if (button1State == 0) {
     lcd.setCursor(2, 0);
     lcd.print("CHRONOJUMP");
     lcd.setCursor(2, 1);
@@ -128,60 +137,69 @@ void setup() {
 
 void loop()
 {
-  buttonState = digitalRead(buttonPin);
-  if (buttonState == 1) {
+  button1State = digitalRead(button1Pin);
+  if (button1State == 1) {
     start_capture();
     delay(1000);
   }
 
   if (capturing)
   {
-    currentTime = micros();
-
-    //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;
-    float measured = scale.get_units();
-
-    //RFD stuff start ------>
-    if (rfdDataPre2Ok) {
-      float rfdValue =  (measured - rfdMeasuredPre2) / ((elapsedTime + rfdTimePre) / 1000000.0);
-      rfdCalculed = true;
-      if (rfdValue > rfdValueMax) {
-        rfdValueMax = rfdValue;
+
+    //Checking the RCA state
+    if (rcaState != lastRcaState) {       //Event generated by the RCA
+      checkTimeOverflow();
+      Serial.print(totalTime);
+      Serial.print(";");
+
+      if (rcaState) {
+        Serial.println("R");
+      } else {
+        Serial.println("r");
       }
-    }
+      lastRcaState = rcaState;
 
-    if (rfdDataPreOk) {
-      rfdTimePre2 = rfdTimePre;
-      rfdMeasuredPre2 = rfdMeasuredPre;
-      rfdDataPre2Ok = true;
-    }
+    } else {                             //If no RCA event, read the force as usual
 
-    rfdTimePre = elapsedTime;
-    rfdMeasuredPre = measured;
-    rfdDataPreOk = true;
-    //<------- RFD stuff end
+      currentTime = micros();
+      checkTimeOverflow();
+      float measured = scale.get_units();
 
-    if (abs(measured) > abs(measuredLcdDelayMax)) {
-      measuredLcdDelayMax = measured;
-    }
-    if (abs(measured) > abs(measuredMax)) {
-      measuredMax = measured;
-    }
+      //RFD stuff start ------>
+      if (rfdDataPre2Ok) {
+        float rfdValue =  (measured - rfdMeasuredPre2) / ((elapsedTime + rfdTimePre) / 1000000.0);
+        rfdCalculed = true;
+        if (rfdValue > rfdValueMax) {
+          rfdValueMax = rfdValue;
+        }
+      }
+
+      if (rfdDataPreOk) {
+        rfdTimePre2 = rfdTimePre;
+        rfdMeasuredPre2 = rfdMeasuredPre;
+        rfdDataPre2Ok = true;
+      }
+
+      rfdTimePre = elapsedTime;
+      rfdMeasuredPre = measured;
+      rfdDataPreOk = true;
+      //<------- RFD stuff end
+
+      if (abs(measured) > abs(measuredLcdDelayMax)) {
+        measuredLcdDelayMax = measured;
+      }
+      if (abs(measured) > abs(measuredMax)) {
+        measuredMax = measured;
+      }
 
-    Serial.print(totalTime); Serial.print(";");
-    Serial.println(measured, 2); //scale.get_units() returns a float
+      Serial.print(totalTime); Serial.print(";");
+      Serial.println(measured, 2); //scale.get_units() returns a float
 
-    printOnLcd();
+      printOnLcd();
+    }
   }
 }
+
 void printOnLcd() {
   lcdCount = lcdCount + 1;
   if (lcdCount >= lcdDelay)
@@ -218,7 +236,6 @@ void printOnLcd() {
       measuredLcdDelayMax = 0;
       lcdCount = 0;
     }
-
   }
 }
 
@@ -226,21 +243,21 @@ void printOnLcd() {
 void printLcdMeu (float val, int xStart, int y, int decimal) {
 
   /*How many characters are to the left of the units number.
-   * Examples:
-   * 1.23   -> 0 charachters
-   * 12.34  -> 1 characters
-   * 123.45 -> 2 characters
-   */
- 
+     Examples:
+     1.23   -> 0 charachters
+     12.34  -> 1 characters
+     123.45 -> 2 characters
+  */
+
   int valLength = floor(log10(abs(val)));
-  
+
   // Adding the extra characters to the left
   if (valLength > 0) {
     xStart -= valLength;
   }
 
   // In negatives numbers the units are in the same position and the minus one position to the left
-  if (val < 0){
+  if (val < 0) {
     xStart--;
   }
   lcd.setCursor(xStart  , y);
@@ -435,3 +452,26 @@ void get_transmission_format()
     Serial.println("text");
   }
 }
+
+void changingRCA() {
+  //TODO: Check the overflow of the lastTriggerTime
+  detachInterrupt(digitalPinToInterrupt(rcaPin));
+  currentTime = micros();
+
+  rcaState = digitalRead(rcaPin);
+
+  attachInterrupt(digitalPinToInterrupt(rcaPin), changingRCA, CHANGE);
+}
+
+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]