#include #include #include #include const int buttonPin = 2; int buttonState = HIGH; int lastState = LOW; unsigned long lastTime = 0; unsigned long BounceDelay = 50; bool tared = false; #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); //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_calVal_eepromAdress = 0; unsigned long t = 0; void setup() { Serial.begin(115200); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } delay(2000); display.clearDisplay(); display.setTextColor(WHITE); float calibrationValue; // calibration value calibrationValue = 21.28; LoadCell.begin(); unsigned long stabilizingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilizing time boolean _tare = false; //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"); } else { LoadCell.setCalFactor(calibrationValue); // set calibration factor (float) Serial.println("Startup is complete"); } LoadCell.tareNoDelay(); delay(1000); } void loop() { tareCheck(); static boolean newDataReady = 0; const int serialPrintInterval = 500; //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(tared){ tared = false; i = 0; } Serial.print("Load_cell output val: "); Serial.println(i); if(i < 0) i = 0; i *= 100; if(int(i)%100 > 50) i = ceil(i/100); else i = floor(i/100); newDataReady = 0; t = millis(); //clear display display.clearDisplay(); // display weight display.setTextSize(1); display.setCursor(0,0); display.print("Weight: "); display.setTextSize(4); display.setCursor(0,8); display.print(int(i)); display.print(" "); display.setTextSize(4); display.print("g"); display.display(); } } } void tareCheck(){ int reading = digitalRead(buttonPin); if(reading != lastState) lastTime = millis(); if((millis() - lastTime) > BounceDelay){ if(reading != buttonState) buttonState = reading; if(buttonState==HIGH){ tared = true; display.clearDisplay(); display.setTextSize(2); display.setCursor(0,0); display.print("Taring..."); display.display(); delay(5000); LoadCell.tareNoDelay(); } } lastState = reading; }