safwan092

Untitled

May 17th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <HX711_ADC.h>
  2. #include <EEPROM.h>
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5.  
  6. LiquidCrystal_I2C lcd(0x27, 16, 2);
  7.  
  8. #define button 8
  9. int btnState = 0;
  10. float newW = 0;
  11. float newZero = 0;
  12. float calo = 0;
  13. //pins:
  14. const int HX711_dout = 4; //mcu > HX711 dout pin
  15. const int HX711_sck = 5; //mcu > HX711 sck pin
  16.  
  17. //HX711 constructor:
  18. HX711_ADC LoadCell(HX711_dout, HX711_sck);
  19.  
  20. const int calVal_eepromAdress = 0;
  21. unsigned long t = 0;
  22.  
  23. void setup() {
  24. pinMode(button, INPUT);
  25. Serial.begin(57600); delay(10);
  26. Serial.println();
  27. Serial.println("Starting...");
  28. lcd.init();
  29. lcd.backlight();
  30. lcd.setCursor(0, 0);
  31. lcd.print("Rice Scale");
  32.  
  33. LoadCell.begin();
  34. //LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
  35. float calibrationValue; // calibration value (see example file "Calibration.ino")
  36. //calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
  37. EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
  38.  
  39. unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  40. boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
  41. LoadCell.start(stabilizingtime, _tare);
  42. if (LoadCell.getTareTimeoutFlag()) {
  43. Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
  44. while (1);
  45. }
  46. else {
  47. LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
  48. Serial.println("Startup is complete");
  49. }
  50. }
  51.  
  52. void loop() {
  53. btnState = digitalRead(button);
  54. static boolean newDataReady = 0;
  55. const int serialPrintInterval = 0; //increase value to slow down serial print activity
  56.  
  57. // check for new data/start next conversion:
  58. if (LoadCell.update()) newDataReady = true;
  59.  
  60. // get smoothed value from the dataset:
  61. if (newDataReady) {
  62. if (millis() > t + serialPrintInterval) {
  63. float i = LoadCell.getData();
  64. if (btnState == 1) {
  65. newZero = i;
  66. }
  67. newW = i - newZero;
  68. if (newW < 1) {
  69. newW = 0;
  70. }
  71. calo = 1.30 * newW;
  72. Serial.print("Load_cell output val: ");
  73. Serial.println(newW);
  74. lcd.clear();
  75. lcd.setCursor(0, 0);
  76. lcd.print("Weight:");
  77. lcd.print(newW);
  78. lcd.print(" g");
  79. lcd.setCursor(0, 1);
  80. lcd.print("Calories:");
  81. lcd.print(calo);
  82. lcd.print(" ");
  83. newDataReady = 0;
  84. t = millis();
  85. }
  86. }
  87.  
  88. // receive command from serial terminal, send 't' to initiate tare operation:
  89. if (Serial.available() > 0) {
  90. char inByte = Serial.read();
  91. if (inByte == 't') LoadCell.tareNoDelay();
  92. }
  93.  
  94. // check if last tare operation is complete:
  95. if (LoadCell.getTareStatus() == true) {
  96. Serial.println("Tare complete");
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment