Need guidance in merging these two function together

This is my first post and I am a beginner in this field , so it will be immensely helpful if someone can tell how to achieve this task. My task is to create a smart attendance system.
I have two separate program running on different Arduino UNO.
The first is 16x2 display , hx711 and is calibrated to measure the weight of the placed on the load cell.



#include <HX711_ADC.h> // need to install 
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // need to install

HX711_ADC LoadCell(6, 7); // parameters: dt pin 6, sck pin 7;
LiquidCrystal_I2C lcd(0x27, 16,2); // 0x27 is the i2c address might different;you can check with Scanner

void setup() 
{
  LoadCell.begin(); // start connection to HX711
  LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
  LoadCell.setCalFactor(1000.0); // calibration factor for load cell => dependent on your individual setup
  lcd.init(); 
  lcd.backlight();
}

void loop() {
  LoadCell.update(); // retrieves data from the load cell
  float i = LoadCell.getData(); // get output value
  lcd.setCursor(0, 0); // set cursor to first row
  lcd.print("Weight[g]:"); // print out to LCD
  lcd.setCursor(0, 1); // set cursor to second row
  lcd.print(i); // print out the retrieved value to the second row
}

The other uno is supposed to show true or false every 5 min whether the weight is placed on load cell or not. At the end of 60 min it will display the maximum amount of true or false obtained. If max amount are true then the student is present and if its false then student is absent.

#include "HX711.h"

#define DOUT  3
#define CLK  2

HX711 scale;

unsigned long previousMillis = 0;
const long interval = 5 * 60 * 1000; // 5 minutes in milliseconds
unsigned long startTime = 0;
int trueCount = 0;
int falseCount = 0;
bool report = false;

void setup() {
  Serial.begin(9600);
  scale.begin(DOUT, CLK);
}

bool is_weight_present() {
  // Read the weight from the load cell
  float weight = scale.get_units();
  
  // Adjust this threshold according to your setup
  if (weight > 0.1) {
    return true;
  } else {
    return false;
  }
}

void loop() {
  unsigned long currentMillis = millis();

  // Measure weight every 5 minutes
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    bool weight_present = is_weight_present();
    if (weight_present) {
      trueCount++;
    } else {
      falseCount++;
    }
  }

  // After 60 minutes, display the maximum count of true or false
  if (currentMillis - startTime >= 60 * 60 * 1000 && !report) {
    report = true;
    Serial.print("Maximum true count obtained in 60 minutes: ");
    Serial.println(trueCount);
    Serial.print("Maximum false count obtained in 60 minutes: ");
    Serial.println(falseCount);
  }

  delay(100); // Adjust delay according to your needs
} />

Also I had a doubt that can't these two functions be both run on a single arduino.

@rk8619
In the IDE on the Menu under EDIT select "Copy for Forum" then paste your code here.
It will make it easier for us to read and analyze.

Search for "Merging codes". It's a common question that handled numerous times here.

Well, although that sentence doesn't make sense, if you're actually saying

"I doubt these two functions can both be run on a single Arduino",

it's good that you asked. Yes they can. Now please, post your code in two code blocks so that anyone can read and copy them for use.
While you're at it, correct 2 errors in the second code.

const long interval = 5 * 60 * 1000; // 5 minutes in milliseconds
...
if (currentMillis - startTime >= 60 * 60 * 1000 && !report) {

both lines contain calculations that will overflow. If you compiled this with compiler warnings turned on, you would know this, so I know you either didn't try to compile it, or don't have compiler warnings enabled(see File|Preferences in the IDE).

Finally, the two routines utilize different 711 libraries, so you'll have to choose one. I'll leave that to you.

In this line, when millis() passes 60 * 60 * 1000, the condition will always be true, as the startTime variable is never updated, and is always 0 (Zero).

Try this code:


#include <HX711_ADC.h> // need to install
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // need to install
int trueCount = 0;
int falseCount = 0;
unsigned long previousMillis = 0;
const unsigned long interval = 5UL * 60UL * 1000UL; // 5 minutes in milliseconds
//const unsigned long interval = 5UL * 60UL * 10UL ; //    for debug
unsigned long startTime = 0;
bool report = false;

HX711_ADC LoadCell(6, 7); // parameters: dt pin 6, sck pin 7;
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 is the i2c address might different;you can check with Scanner
//----------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  LoadCell.begin(); // start connection to HX711
  LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
  LoadCell.setCalFactor(1000.0); // calibration factor for load cell => dependent on your individual setup
  lcd.init();
  lcd.backlight();
}
//----------------------------------------------------------------------
void loop() {
  LoadCell.update(); // retrieves data from the load cell
  float i = LoadCell.getData(); // get output value
  lcd.setCursor(0, 0); // set cursor to first row
  lcd.print("Weight[g]:"); // print out to LCD
  lcd.setCursor(0, 1); // set cursor to second row
  lcd.print(i); // print out the retrieved value to the second row
  lcd.setCursor(8, 1);
  lcd.print(trueCount);
  lcd.setCursor(11, 1);
  lcd.print(falseCount);
  myTime();
}
//----------------------------------------------------------------------
bool is_weight_present() {
  float weight = LoadCell.getData();  // Read the weight from the load cell
  if (weight > 0.1) {   // Adjust this threshold according to your setup
    return true;
  }
  else {
    return false;
  }
}
//----------------------------------------------------------------------
void myTime() {
  // After 60 minutes, display the maximum count of true or false
  if (millis() - startTime >= 60UL * 60UL * 1000UL) {
  //if (millis() - startTime >= 60UL * 60UL * 5UL ) { // for debug
    if (!report) {
      report = true;
      Serial.print("Maximum true count obtained in 60 minutes: ");
      Serial.println(trueCount);
      Serial.print("Maximum false count obtained in 60 minutes: ");
      Serial.println(falseCount);
    }
  }
  else {
    if (millis() - previousMillis >= interval) {    // Measure weight every 5 minutes
      previousMillis = millis();
      bool weight_present = is_weight_present();
      if (weight_present) {
        trueCount++;
      }
      else {
        falseCount++;
      }
    }
  }
  delay(100); // Adjust delay according to your needs
}

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