# Push Button to trigger if statement once

**URL:** <https://forum.arduino.cc/t/push-button-to-trigger-if-statement-once/967564>\
**Category:** LEDs and Multiplexing\
**Created:** [March 9, 2022, 7:55pm UTC](https://forum.arduino.cc/t/push-button-to-trigger-if-statement-once/967564 "2022-03-09T19:55:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmespinal](https://avatars.discourse-cdn.com/v4/letter/v/d2c977/32.png) [@vmespinal](https://forum.arduino.cc/u/vmespinal)\
**Post date:** [March 9, 2022, 7:55pm UTC](https://forum.arduino.cc/t/push-button-to-trigger-if-statement-once/967564/1 "2022-03-09T19:55:03Z")

</div>

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.

```cpp
#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

    }
}
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [March 9, 2022, 8:04pm UTC](https://forum.arduino.cc/t/push-button-to-trigger-if-statement-once/967564/3 "2022-03-09T20:04:32Z")

</div>

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

> **[State Change Detection (Edge Detection) for pushbuttons](https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection/)**
>
> Open-source electronic prototyping platform enabling users to create interactive electronic objects.

---

<div class="post-metadata">

**Author:** ![vmespinal](https://avatars.discourse-cdn.com/v4/letter/v/d2c977/32.png) [@vmespinal](https://forum.arduino.cc/u/vmespinal)\
**Post date:** [March 14, 2022, 7:23pm UTC](https://forum.arduino.cc/t/push-button-to-trigger-if-statement-once/967564/4 "2022-03-14T19:23:33Z")

</div>

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

```cpp

#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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 10, 2022, 7:24pm UTC](https://forum.arduino.cc/t/push-button-to-trigger-if-statement-once/967564/5 "2022-09-10T19:24:11Z")

</div>

Closed on September 10, 2022, 7:24pm UTC

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