Need some help with building my project

Hey guys, I didn’t know if this was the correct category but I was just looking for a little guidance on how I should start with this coding. Basically, I have been making a signal that triggers a text message whenever there is force on a load cell. Right now, I have it so it triggers a SMS message whenever there is over 5 lbs read on the scale. In order for me to make it more realistic for my project idea, I am going to have to make it so it triggers the message when the scale is reading x > weight for y = however many seconds. I saw there is a timer library SPARKINTERVALTIMER that I’m sure I could use to keep the time. I am just not sure how I could combine these two values to set off my flag when the conditions are met.

Here is my code (any recommendations on how I could make it better just let me know):

// This #include statement was automatically added by the Particle IDE.
#include "HX711ADC.h"

// This #include statement was automatically added by the Particle IDE.
#include "Ubidots.h"

/****************************************
 * Define Constants
 ****************************************/

#define DOUT  D0
#define CLK  D1
#define "  // My Ubidots TOKEN

Ubidots ubidots(TOKEN);

HX711ADC scale(DOUT, CLK);

float calibration_factor = -7050; //from sparkfun example (May be incorrect)

/****************************************
 * Main Functions
 ****************************************/

void setup() {
    Serial.begin(9600);
    scale.set_scale(calibration_factor); //This value is obtained by using the   SparkFun_HX711_Calibration sketch
    scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
    Serial.println("Readings:");
}

void loop() {
    float weight = scale.get_units();
    Serial.print("Reading: ");
    Serial.print(weight, 1);
    Serial.print(" lbs");
    Serial.println();
    if (weight > 5) {
        int flag = 1;
        ubidots.add("flag", flag);  
        ubidots.sendAll();
        delay(5000);
        flag = 0;
        ubidots.add("flag", flag);
        ubidots.sendAll();
        delay(5000)
     }
}

You could just have a short delay in your loop (like 100 ms), and count the number of times loop runs while the weight remains above your set point. For example, if you want the weight to be above 5 lbs. for 3 seconds, you could do something like this,

int secondsOverSetPoint = 3;
int counter = 0;

void setup() {
    Serial.begin(9600);
    scale.set_scale(calibration_factor); //This value is obtained by using the   SparkFun_HX711_Calibration sketch
    scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
    Serial.println("Readings:");
}

void loop() {
    float weight = scale.get_units();
    Serial.printlnf("Reading: %.1f lbs", weight);
    
    if (weight > 5) {
        counter++;
    }else{
        counter = 0; // start over if the weight drops below the set point
    }
    
    delay(100);
    
    if (counter == 10 * secondsOverSetPoint) {
        counter = 0;
        // send SMS message
    }
}

This won’t be exact, since the loop takes some finite amount of time to run, but I think it wold probably be accurate enough for your purpose.

1 Like

Thank you for such a quick response! Looks like that will do the trick.