Commit 73769e5f authored by Xavier Padullés's avatar Xavier Padullés

RaceAnalyzer. Fixed non constant pps

parent f9eabb89
...@@ -7,6 +7,7 @@ Adafruit_ADS1115 loadCell; ...@@ -7,6 +7,7 @@ Adafruit_ADS1115 loadCell;
int encoderPinA = 3; int encoderPinA = 3;
int encoderPinB = 4; int encoderPinB = 4;
volatile int encoderDisplacement = 0; volatile int encoderDisplacement = 0;
int lastEncoderDisplacement = 0;
volatile unsigned long changingTime = 0; volatile unsigned long changingTime = 0;
unsigned long elapsedTime = 0; unsigned long elapsedTime = 0;
unsigned long totalTime = 0; unsigned long totalTime = 0;
...@@ -25,6 +26,9 @@ float calibrationFactor = 0; ...@@ -25,6 +26,9 @@ float calibrationFactor = 0;
//Wether the sensor has to capture or not //Wether the sensor has to capture or not
boolean capturing = false; 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 //wether the tranmission is in binary format or not
boolean binaryFormat = false; boolean binaryFormat = false;
...@@ -48,24 +52,23 @@ void setup() { ...@@ -48,24 +52,23 @@ void setup() {
} }
void loop() { void loop() {
int offsettedData = 0;
long int total = 0; long int total = 0;
int nReadings = 0; int nReadings = 0;
int meanOffsettedData = 0; int offsettedData = 0;
if (capturing) if (capturing)
{ {
//With a diameter is of 160mm, each pulse is 2.513274mm. 4 pulses equals 1.00531cm //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); offsettedData = readOffsettedData(0);
total += offsettedData; total += offsettedData;
nReadings++; nReadings++;
} }
int lastEncoderDisplacement = encoderDisplacement; //Assigned to another variable for in the case that encoder displacement changes before printing it
unsigned long Time = changingTime; 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 //Managing the timer overflow
if (Time > lastTime) //No overflow if (Time > lastTime) //No overflow
...@@ -76,7 +79,7 @@ void loop() { ...@@ -76,7 +79,7 @@ void loop() {
elapsedTime = (4294967295 - lastTime) + Time; //Time from the last measure to the overflow event plus the changingTime elapsedTime = (4294967295 - lastTime) + Time; //Time from the last measure to the overflow event plus the changingTime
} }
totalTime += elapsedTime; totalTime += elapsedTime;
meanOffsettedData = total / nReadings; int meanOffsettedData = total / nReadings;
lastTime = Time; lastTime = Time;
//Sending in text mode //Sending in text mode
...@@ -86,6 +89,8 @@ void loop() { ...@@ -86,6 +89,8 @@ void loop() {
Serial.print(";"); Serial.print(";");
Serial.println(offsettedData); Serial.println(offsettedData);
processSample = false;
// //Sending in binary mode // //Sending in binary mode
// sendInt(lastEncoderDisplacement); // sendInt(lastEncoderDisplacement);
// sendInt(totalTime); // sendInt(totalTime);
...@@ -104,10 +109,15 @@ void changingA() { ...@@ -104,10 +109,15 @@ void changingA() {
changingTime = micros(); changingTime = micros();
if (digitalRead(encoderPinB) == HIGH) { if (digitalRead(encoderPinB) == HIGH) {
encoderDisplacement--; encoderDisplacement--;
digitalWrite(13, HIGH); //digitalWrite(13, HIGH);
} else { } else {
encoderDisplacement++; encoderDisplacement++;
digitalWrite(13, LOW); //digitalWrite(13, LOW);
}
if (abs(encoderDisplacement) >= pps){
lastEncoderDisplacement = encoderDisplacement;
encoderDisplacement = 0;
processSample = true;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment