Push Button to trigger if statement once

for my code, i am having issue with some coding.

for my program, i want relay to always be off until I press push button(not hold) and relay will turn on once weight(wt) has been meet then relay will turn off then wait until press push button again to cycle again.

I can only get code to work if push button is held, i want to relay to stay on until wt have been meet.

#include "HX711.h"

#define calibration_factor -9620.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define DOUT  3
#define CLK  2


const int buttonPin = 4; // pin assign to push button
int buttonState = 0;

HX711 scale;

void setup() {
  pinMode(12, OUTPUT); // pin assign to relay module.


  pinMode(buttonPin, INPUT_PULLUP);

  Serial.begin(9600);
  Serial.println("HX711 scale demo");

  scale.begin(DOUT, CLK);
  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:");

  digitalWrite(12, LOW); turn relay off
}

void loop() {

  buttonState = digitalRead(buttonPin);
  float i = scale.get_units();
  float wt = (float)i;


  if (buttonState == HIGH) { //if push button is not pressed
    digitalWrite(12, LOW); // turn relay off
  }


  if (buttonState == LOW){ // if push button is pressed

    Serial.print(scale.get_units(), 3); //scale.get_units() returns a float with 3 decimcal place
    Serial.println();

    if (wt < 10)
    {
      digitalWrite(12, HIGH);   // turn relay on
      Serial.print(" LBS IS LESS THAN 10 LBS"); //You can change this to kg but you'll need to refactor the calibration_factor
      Serial.println();

    }
    if (wt >= 10) {
      Serial.print("LBS IS GREATER THAN 10LBS: ");
      Serial.println();

      digitalWrite(12, LOW);   // turn relay off

    }
}
}

Suggest you always look if a switch has changed state rather than when a switch is at a level.

I figured it out below is code the works for me.



#include "HX711.h"
#define calibration_factor -9620.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define DOUT  3
#define CLK  2

// Instance of the button.

const int buttonPin = 4; // pin assign to push button
int buttonState = 0;         // current state of the button

HX711 scale;
int counter = 0;
void setup() {

  pinMode(12, OUTPUT); // pin assign to relay module.
  pinMode(buttonPin, INPUT_PULLUP); //pin assigned to push button

  Serial.begin(9600);
  Serial.println("HX711 scale demo");
  scale.begin(DOUT, CLK);
  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:");
  digitalWrite(12, LOW); //turn relay off
}

void loop() {

  buttonState = digitalRead(buttonPin);
  float i = scale.get_units();
  float wt = (float)i;

  Serial.print(scale.get_units(), 3); //scale.get_units() returns a float with 3 decimcal place
  Serial.println();

  if (buttonState == LOW) // if push button is pressed
  {
    counter++;
    delay(150); //150 deflaut delay once push button is pressed
  }

  if (counter == 0) {
    digitalWrite(12, LOW);
  }

  else if (counter == 1 && 2 <= wt && wt <= 10.6) { //2.16lb pail + 8.44lbs adamixture + 10.6lbs limit. Turn on program without bucket, To turn on relay bucket must be on scale and press push button at same time.
    
    digitalWrite(12, HIGH);
  }
  else
  {
    counter = 0;
  }
}


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