Confused about Boolean and how to place

I am trying to write a scale code that triggers a relay once a specific weight range is attained. My confusion is I only want the relay to engage once (not continuous) until a remote controlled button is pressed and resets the whole thing. I am new to this and have built this code from libraries and research and figured that a Boolean was appropriate, just don't understand how to do it. I searched, read and attempted but I am just not getting it.

Here is the last code that worked, please help

#include "HX711.h"


// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

// Out relay circuit wiring
const int OUT_RELAY_PIN  = 5; // out relay pin
// In relay circuit wiring
const int RC_BUTTON_PIN = 7; // remote control button pin
const int IN_RELAY_PIN  = 6; // in relay pin


HX711 scale;

void setup() {
  Serial.begin(9600);
  Serial.println("Scale Startup");

  Serial.println("Initializing the scale");

  // Initialize library with data output pin, clock input pin and gain factor.
  // Channel selection is made by passing the appropriate gain:
  // - With a gain factor of 64 or 128, channel A is selected
  // - With a gain factor of 32, channel B is selected
  // By omitting the gain factor parameter, the library
  // default "128" (Channel A) is used here.
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  Serial.println("Before setting up the scale:");
  Serial.print("read: \t\t");
  Serial.println(scale.read());			// print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(10));  	// print the average of 10 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(3));		// print the average of 3 readings from the ADC minus the tare weight (not set yet)

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(3), 1);	// print the average of 3 readings from the ADC minus tare weight (not set) divided
						// by the SCALE parameter (not set yet)

  scale.set_scale(56885);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();				        // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(10));       // print the average of 10 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(3));		// print the average of 3 readings from the ADC minus the tare weight, set with tare()

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(3), 1);        // print the average of 3 readings from the ADC minus tare weight, divided
						// by the SCALE parameter set with set_scale

  Serial.println("Readings:");

    //relay setup
  pinMode(OUT_RELAY_PIN, OUTPUT);        // set out pin to output mode
    // reset relay setup
  pinMode(RC_BUTTON_PIN, INPUT_PULLUP); // set remote control pin to input pull-up mode
  pinMode(IN_RELAY_PIN, OUTPUT);        // set in pin to output mode
}

void loop() {
  Serial.print("one reading:\t");
  Serial.print(scale.get_units(), 1);
  Serial.print("\t| average:\t");
  Serial.println(scale.get_units(3), 1);

  int buttonState = digitalRead(RC_BUTTON_PIN); // read new state

  if (buttonState == LOW) {
    Serial.println("Reset initialized");
    digitalWrite(IN_RELAY_PIN, HIGH); // turn on in pin
    delay(5000); // time must match out pin delay
    digitalWrite(IN_RELAY_PIN, LOW);  // turn off in pin
  }
  else
  if (buttonState == HIGH) {
    Serial.println("Waiting reset");
    digitalWrite(IN_RELAY_PIN, LOW);  // turn off in pin
  }

  if (scale.get_units(3) > 1.9 && scale.get_units(3) < 2.29){
    Serial.println("The weight is correct");
    digitalWrite(OUT_RELAY_PIN, HIGH); // turn on out pin
    Serial.println("Turn on relay");
    delay(5000); // time must match in pin delay
    digitalWrite(OUT_RELAY_PIN, LOW);  // turn off out pin
    Serial.println("Turn off relay");
  }

  else
  if (scale.get_units(3) >=2.3){
    Serial.println("The weight is wrong");
    delay(100);
  }

  else
    Serial.println("The weight is incorrect");
  
  delay(100);

}

Thanks

Your "buttonState" is typed as an "int" but acts as a boolean. buttonState is "ON" or "OFF"

Will this keep the if statement from continuing to loop even if the weight has not been removed? this is the area that I only want to run once if the parameters are met.

  if (scale.get_units(3) > 1.9 && scale.get_units(3) < 2.29){
    Serial.println("The weight is correct");
    digitalWrite(OUT_RELAY_PIN, HIGH); // turn on out pin
    Serial.println("Turn on relay");
    delay(5000); // time must match in pin delay
    digitalWrite(OUT_RELAY_PIN, LOW);  // turn off out pin
    Serial.println("Turn off relay");

Assuming the very next line of your code chunk is a close brace "}" closing the "if" conditional statement, and if your code chunk is in "loop()", then it will continually do the relay calls.

Your question about a boolean value is useful here.

First, define a variable of type boolean, either inside the function, or globally, and give it a value:

boolean thisFlag = 0;

Next, inside your if conditional, test thisFlag (for true or false / 1 or 0 / HIGH or LOW)

if (scale.get_units(3) > 1.9 && scale.get_units(3) < 2.29 && thisFlag == 0 ){

Once you enter the "if" condition by having "thisFlag" cleared/reset/zero, immediately set thisFlag:

if (scale.get_units(3) > 1.9 && scale.get_units(3) < 2.29  && thisFlag == 0 ){
  thisFlag = 1;

With thisFlag set, the next time the "if" condition is tested, it will fail to the next program line after the "if" condition.

Let me know if this makes sense, and that you can make it work.

xfpd, I get what you are saying, so should it look like this?
the bool thisFlag = 0; i put before the setup

  if (scale.get_units(3) > 1.9 && scale.get_units(3) < 2.29 && thisFlag == 0 ){
    Serial.println("The weight is correct");
    digitalWrite(OUT_RELAY_PIN, HIGH); // turn on out pin
    Serial.println("Turn on relay");
    delay(5000); // time must match in pin delay
    digitalWrite(OUT_RELAY_PIN, LOW);  // turn off out pin
    Serial.println("Turn off relay");
  }
  
  if (scale.get_units(3) > 1.9 && scale.get_units(3) < 2.29){
  thisFlag = 1;
  }

  else
  if (scale.get_units(3) >=2.3){
    Serial.println("The weight is wrong");
    delay(100);
  }

Or did I miss the mark?

That code absolutely worked! Thanks!
Now for my next problem, how can I reset "thisFlag" with a push of a button?

Welcome! That is impossible as stated. you are saying button pressed relay energized (not really) until a remote button is pressed and resets the whole thing (How it was not energized).

The way I would do it is set a boolean value to true when button is pressed and set false when the reset button is pressed, then if true energize the relay, doing it bunches of times will not effect anything. Net result push button relay turns on, press the reset and the relay turns off, Lose power and the relay turns off.

Awesome. Next time you post code, please post your entire sketch. It helps clarify how the sketch works.

1 Like

Will do, thank you so much!

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