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