#include #include #include LiquidCrystal_I2C lcd(0x27, 20, 4); //sometimes the LCD adress is not 0x3f. Change to 0x27 if it dosn't work. const byte hx711_data_pin = 3; //Data pin from HX711 const byte hx711_clock_pin = 2; //Clock pin from HX711 Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711 int tara_button = 8; //Tara button int count_button = 12; //Count button int mode_button = 11; //Mode change button float y1 = 822.0; // MDF plate total weight 1003 long x1 = 0L; long x0 = 0L; float avg_size = 10.0; // amount of averages for each mass measurement float tara = 0; bool tara_pushed = false; bool count_pushed = false; bool mode_pushed = false; int mode = 0; int count = 0; float oz_conversion = 0.035274; void setup() { Serial.begin(9600); // prepare serial port PCICR |= (1 << PCIE0); //enable PCMSK0 scan PCMSK0 |= (1 << PCINT0); //Set pin D8 trigger an interrupt on state change. PCMSK0 |= (1 << PCINT4); //Set pin D12 trigger an interrupt on state change. PCMSK0 |= (1 << PCINT3); //Set pin D11 trigger an interrupt on state change. pinMode(tara_button, INPUT_PULLUP); pinMode(mode_button, INPUT_PULLUP); pinMode(count_button, INPUT_PULLUP); lcd.init(); //Init the LCD lcd.backlight(); //Activate backlight delay(1000); // allow load cell and hx711 to settle // tare procedure for (int ii = 0; ii < int(avg_size); ii++) { delay(10); x0 += hx711.read(); } x0 /= long(avg_size); lcd.clear(); lcd.setCursor(0, 0); lcd.print(" Calibrating "); lcd.setCursor(0, 1); lcd.print(" .... "); int ii = 1; while (true) { if (hx711.read() < x0 + 10000) { //do nothing... } else { ii++; delay(2000); for (int jj = 0; jj < int(avg_size); jj++) { x1 += hx711.read(); } x1 /= long(avg_size); break; } } lcd.clear(); lcd.setCursor(0, 0); lcd.print(" All set!!! "); } void loop() { long reading = 0; for (int jj = 0; jj < int(avg_size); jj++) { reading += hx711.read(); } reading /= long(avg_size); float ratio_1 = (float) (reading - x0); float ratio_2 = (float) (x1 - x0); float ratio = ratio_1 / ratio_2; float mass = y1 * ratio; Serial.print(mass - tara); Serial.print("|"); Serial.print(count); //Serial.print("|"); delay(300); if (tara_pushed) { tara = mass; tara_pushed = false; lcd.setCursor(0, 0); lcd.print(" TARA "); lcd.setCursor(0, 1); lcd.print(" . "); delay(300); //Serial.print("."); lcd.setCursor(0, 1); lcd.print(" .. "); delay(300); //Serial.println("."); lcd.setCursor(0, 1); lcd.print(" ... "); delay(300); } if (count_pushed) { mode = 2; count_pushed = false; delay(300); } if (mode_pushed) { mode = 1; mode_pushed = false; delay(300); } if (mode == 0) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(" TOTAL WEIGHT "); lcd.setCursor(0, 1); lcd.print(mass - tara); lcd.print(" g"); } else if (mode == 1) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(" TALLY COUNTER "); lcd.setCursor(0, 1); lcd.print(count); lcd.print(" crops"); delay(1000); mode = 0; } else if (mode == 2) { count = count + 1; lcd.clear(); lcd.setCursor(0, 0); lcd.print(" TALLY COUNTER "); lcd.setCursor(0, 1); lcd.print(count); lcd.print(" crops"); mode = 0; } } //interruption to detect buttons ISR(PCINT0_vect) { if (!(PINB & B00000001)) { tara_pushed = true; //Tara button was pushed } if (!(PINB & B00000100)) { count_pushed = true; //Count button was pushed } if (!(PINB & B00001000)) { mode_pushed = true; //Mode button was pushed } }