2 Programs, 2 different frequencies. Why?

Hello everyone,

I am struggling with my programming.

Problem:

I have got these two programs that should both be getting data at 10hz however one is receiving data at 1hz instead and I can't identify where the problem in my code is.

I know the problem is with the code because I can run the other program on the same hardware and gets data at 10hz.

Hardware:

Program 1: (data at 10hz)

#include "HX711.h"

HX711 scale;
float calibrationFactor = 4.7; // Enter the calibration factor from Program 1 here

void setup() {
  Serial.begin(9600);
  scale.begin(9, 10);
  scale.set_scale(calibrationFactor); // Set the calibration factor
  scale.tare(); // Zero the scale
}

void loop() {
  unsigned long startTime = millis();
  while (millis() - startTime < 100000) { // Run for 10 seconds
    if (scale.is_ready()) {
      float weight = scale.get_units(1); // Read weight with 10 decimal places
      Serial.println(weight);
    }
  }
  while (true) {
    // Do nothing, the program has finished
  }
}

Program 2: (data at 1hz)

#include "HX711.h"

int State;
HX711 scale;
float calibrationFactor = 4.7;

void setup() {
  Serial.begin(9600);
  pinMode(8, INPUT);
  scale.begin(9, 10);
  scale.set_scale(calibrationFactor);
  scale.tare();
}

void loop() {
  State = digitalRead(8);
    if(State==1){
    
  unsigned long startTime = millis();
    while (millis() - startTime < 100000) {
      if(scale.is_ready()) {
        scale.tare();
        float weight = scale.get_units(1);
        Serial.println(weight);
      }}}
    else {
      
    }
 }


I would like to get program 2 to gather data at 10hz.

Thank you for any help you can provide.

изображение

while (millis() - startTime < 100000UL) {

The second code includes this line in the while loop while the first code does not. How long does this method take to run?

3 Likes

Ah, I hadnt thought about how long the scale.tare function takes. I have deleted it and now my program works at 10hz.

Thank you very much Delta_G. :pray:

It isn't the rate difference, but 10 seconds is 10000 ms not 100000.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.