[chronojump] RaceAnalyzer. Fixed non constant pps



commit 73769e5fe9ce59570c065f5660e94b51d1e6c3ba
Author: Xavier Padullés <x padulles gmail com>
Date:   Wed Feb 20 16:25:23 2019 +0100

    RaceAnalyzer. Fixed non constant pps

 arduino/raceAnalyzer/raceAnalyzer.ino | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
---
diff --git a/arduino/raceAnalyzer/raceAnalyzer.ino b/arduino/raceAnalyzer/raceAnalyzer.ino
index 47dc6b72..c2571163 100644
--- a/arduino/raceAnalyzer/raceAnalyzer.ino
+++ b/arduino/raceAnalyzer/raceAnalyzer.ino
@@ -7,6 +7,7 @@ Adafruit_ADS1115 loadCell;
 int encoderPinA = 3;
 int encoderPinB = 4;
 volatile int encoderDisplacement = 0;
+int lastEncoderDisplacement = 0;
 volatile unsigned long changingTime = 0;
 unsigned long elapsedTime = 0;
 unsigned long totalTime = 0;
@@ -25,6 +26,9 @@ float calibrationFactor = 0;
 //Wether the sensor has to capture or not
 boolean capturing = false;
 
+//Wether the encoder has reached the number of pulses per sample or not
+boolean processSample = false;
+
 //wether the tranmission is in binary format or not
 boolean binaryFormat = false;
 
@@ -48,24 +52,23 @@ void setup() {
 }
 
 void loop() {
-  int offsettedData = 0;
   long int total = 0;
   int nReadings = 0;
-  int meanOffsettedData = 0;
+  int offsettedData = 0;
+  
 
   if (capturing)
   {
 
     //With a diameter is of 160mm, each pulse is 2.513274mm. 4 pulses equals 1.00531cm
-    while (abs(encoderDisplacement) < pps ) {
+    while (!processSample) {
       offsettedData = readOffsettedData(0);
       total += offsettedData;
       nReadings++;
     }
 
-    int lastEncoderDisplacement = encoderDisplacement; //Assigned to another variable for in the case that 
encoder displacement changes before printing it
     unsigned long Time = changingTime;
-    encoderDisplacement = 0;
+    //int lastEncoderDisplacement = encoderDisplacement; //Assigned to another variable for in the case that 
encoder displacement changes before printing it
 
     //Managing the timer overflow
     if (Time > lastTime)      //No overflow
@@ -76,7 +79,7 @@ void loop() {
       elapsedTime = (4294967295 - lastTime) + Time; //Time from the last measure to the overflow event plus 
the changingTime
     }
     totalTime += elapsedTime;
-    meanOffsettedData = total / nReadings;
+    int meanOffsettedData = total / nReadings;
     lastTime = Time;
     
       //Sending in text mode
@@ -86,6 +89,8 @@ void loop() {
     Serial.print(";");
     Serial.println(offsettedData);
 
+    processSample = false;
+
 //    //Sending in binary mode
 //    sendInt(lastEncoderDisplacement);
 //    sendInt(totalTime);
@@ -104,10 +109,15 @@ void changingA() {
   changingTime = micros();
   if (digitalRead(encoderPinB) == HIGH) {
     encoderDisplacement--;
-    digitalWrite(13, HIGH);
+    //digitalWrite(13, HIGH);
   } else {
     encoderDisplacement++;
-    digitalWrite(13, LOW);
+    //digitalWrite(13, LOW);
+  }
+  if (abs(encoderDisplacement) >= pps){
+    lastEncoderDisplacement = encoderDisplacement;
+    encoderDisplacement = 0;
+    processSample = true;
   }
 }
 


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