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