Go Down

Topic: Read 8 samples, do the average and print the value. (Read 182 times) previous topic - next topic

aiss

Hi folks, I'm using Arduino Uno and the HX711 to collect data  from the load cells. I've just changed the sample rate of the HX711 from 10Hz to 80Hz. Now I want to read the sensor output , do the average of 8 samples and print just 1 value (the average one) every 8 samples. How can I do this? Please find attached the code I'm using to read.



#include <EEPROM.h>
#define CLK 2     // PIN MAPPING: https://www.arduino.cc/en/Hacking/PinMapping168
const int DataIn [] = {4,7,8,12};

String message;

long S[4];            // used to read measurements
float Force[4];       // forces (in grams) to be output
long Soff[4];         // used to store calibration offsets in EEPROM
float Gain[4];         // used to store calibration gains in EEPROM       
int eeaddr_Soff[4];   // eeprom address for offsets array
int eeaddr_Gain[4];   // eeprom address for gains array

void setup() {
  // code adapted from: http://forum.hobbycomponents.com/viewtopic.php?f=73&t=1763
 
  Serial.begin(9600);

  pinMode(CLK, OUTPUT);
  for(int ch=0; ch<4; ch++){
    pinMode(DataIn[ch], INPUT);
  }
  pinMode(LED_BUILTIN, OUTPUT);
  // ........................define the EEPROM ADDRESSES
  int SIZE_ESTIMATE = max( sizeof(long), sizeof(float));  // to be able change variable types without overwriting
  for (int i=0; i<4; i++) {   
     eeaddr_Soff =  2*i    * SIZE_ESTIMATE;
     eeaddr_Gain = (2*i+1) * SIZE_ESTIMATE;
  }

  for(int ch=0; ch<4; ch++){
    EEPROM.get(eeaddr_Soff[ch], Soff[ch]);  // retrieves values from EEPROM at designated addresses
    EEPROM.get(eeaddr_Gain[ch], Gain[ch]);  // retrieves values from EEPROM at designated addresses
  }

}

void loop() {
  float Force[4];
  float X;
  float Y;
  float SUM_F;
   if (Serial.available())
   { char c = Serial.read();
      if (c=='\n') {
        fn_ParseMessage(message);
        message="";
      } 
      else
        message += c;
   }

  fn_Read4channels();
  for(int ch=0; ch<4; ch++){
    Force[ch] = (float)(S[ch]-Soff[ch])*Gain[ch];
//  Force[ch] = (float)(S[ch]);
  }
  SUM_F = Force[0] + Force[1] + Force[2] + Force[3];
  X = 65*(-Force[0] - Force[1] + Force[2] + Force[3]) / (SUM_F) ;
  Y = 100*(Force[0] + Force[3] - Force[2] - Force[1]) / (SUM_F) ;
 
  Serial.print(Force[0]);
  Serial.print(", ");
  Serial.print(Force[1]);
  Serial.print(", "); 
  Serial.print(Force[2]);
  Serial.print(", "); 
  Serial.print(Force[3]);
  Serial.print(", "); 
  Serial.print(X);
  Serial.print(", "); 
  Serial.print(Y);
 Serial.println("\n");



And this is the function that I use read channels:
void fn_Read4channels(){
//    while (digitalRead(DataIn[0])) ;    // ***** TO DO: check that all channels are ready ******
      while (digitalRead(DataIn[0])||digitalRead(DataIn[1])||digitalRead(DataIn[2])||digitalRead(DataIn[3])) ;

    for(int ch=0; ch<4; ch++){
       S[ch]=0L;   // 'L' to force the constant into a long data format
    }
  for (int i = 0; i < 24; i++) {     
    digitalWrite(CLK, HIGH);
    for(int ch=0; ch<4; ch++){
      S[ch] = (S[ch] << 1) | digitalRead(DataIn[ch])  ;
    }
    digitalWrite(CLK, LOW);
  }

  digitalWrite(CLK, HIGH);
  digitalWrite(CLK, LOW);
    for(int ch=0; ch<4; ch++){
      /* Values are returned as a 24bit 2's complement
         and need to be converted to 32-bit 2's complement*/
      if (S[ch] >= 0x800000)  S[ch] = S[ch] | 0xFF000000L;
    }
}
Thanks

PaulS

Quote
Please find attached the code I'm using to read.
I tried. You FAILED to post ALL of your code.
The art of getting good answers lies in asking good questions.

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AWOL

...and that's why we ask you to use code tags.
Please edit your posts and add them.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up