Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <HX711_ADC.h>
- #include <EEPROM.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- #define button 8
- int btnState = 0;
- float newW = 0;
- float newZero = 0;
- float calo = 0;
- //pins:
- const int HX711_dout = 4; //mcu > HX711 dout pin
- const int HX711_sck = 5; //mcu > HX711 sck pin
- //HX711 constructor:
- HX711_ADC LoadCell(HX711_dout, HX711_sck);
- const int calVal_eepromAdress = 0;
- unsigned long t = 0;
- void setup() {
- pinMode(button, INPUT);
- Serial.begin(57600); delay(10);
- Serial.println();
- Serial.println("Starting...");
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("Rice Scale");
- LoadCell.begin();
- //LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
- float calibrationValue; // calibration value (see example file "Calibration.ino")
- //calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
- EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
- unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
- boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
- LoadCell.start(stabilizingtime, _tare);
- if (LoadCell.getTareTimeoutFlag()) {
- Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
- while (1);
- }
- else {
- LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
- Serial.println("Startup is complete");
- }
- }
- void loop() {
- btnState = digitalRead(button);
- static boolean newDataReady = 0;
- const int serialPrintInterval = 0; //increase value to slow down serial print activity
- // check for new data/start next conversion:
- if (LoadCell.update()) newDataReady = true;
- // get smoothed value from the dataset:
- if (newDataReady) {
- if (millis() > t + serialPrintInterval) {
- float i = LoadCell.getData();
- if (btnState == 1) {
- newZero = i;
- }
- newW = i - newZero;
- if (newW < 1) {
- newW = 0;
- }
- calo = 1.30 * newW;
- Serial.print("Load_cell output val: ");
- Serial.println(newW);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Weight:");
- lcd.print(newW);
- lcd.print(" g");
- lcd.setCursor(0, 1);
- lcd.print("Calories:");
- lcd.print(calo);
- lcd.print(" ");
- newDataReady = 0;
- t = millis();
- }
- }
- // receive command from serial terminal, send 't' to initiate tare operation:
- if (Serial.available() > 0) {
- char inByte = Serial.read();
- if (inByte == 't') LoadCell.tareNoDelay();
- }
- // check if last tare operation is complete:
- if (LoadCell.getTareStatus() == true) {
- Serial.println("Tare complete");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment