#include <Arduino.h>
#include "HX711.h"
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 12;
const int LOADCELL_SCK_PIN = 13;
// const int trigPin = 5;
// const int echoPin = 18;
//define sound velocity in cm/uS
#define SOUND_VELOCITY 0.034
#define CM_TO_INCH 0.393701
// long duration;
// float distanceCm;
// float distanceInch;
HX711 scale;
void loadcellSetup(){
Serial.println("HX711 Demo");
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(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t"); Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t"); Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(416.0);
//scale.set_scale(-471.497);
// 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(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1);
// print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void setup() {
Serial.begin(115200);
loadcellSetup();
// pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
// pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop() {
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 5);
scale.power_down(); // put the ADC in sleep mode
delay(1000);
scale.power_up();
// // Clears the trigPin
// digitalWrite(trigPin, LOW);
// delayMicroseconds(2);
// // Sets the trigPin on HIGH state for 10 micro seconds
// digitalWrite(trigPin, HIGH);
// delayMicroseconds(10);
// digitalWrite(trigPin, LOW);
// // Reads the echoPin, returns the sound wave travel time in microseconds
// duration = pulseIn(echoPin, HIGH);
// // Calculate the distance
// distanceCm = duration * SOUND_VELOCITY/2;
// // Convert to inches
// distanceInch = distanceCm * CM_TO_INCH;
// Prints the distance on the Serial Monitor
// Serial.print("Distance (cm): ");
// Serial.println(distanceCm);
// Serial.print("Distance (inch): ");
// Serial.println(distanceInch);
delay(1000);
}