Skip to content

Instantly share code, notes, and snippets.

@mcrmfc
Last active November 24, 2016 10:31
Show Gist options
  • Save mcrmfc/8f724d9891c0d4d5abc8a38fb37f87ad to your computer and use it in GitHub Desktop.
Save mcrmfc/8f724d9891c0d4d5abc8a38fb37f87ad to your computer and use it in GitHub Desktop.
#include "./HX711.h"
#include <stdlib.h> /* abs */
/********************************/
// include the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*********************************************************/
HX711 scale;
int ssrPin = 11;
int ledPin = 10;
int buttonPin = 2;
unsigned long time;
long previousmillis = 0;
long interval = 300;
long startTime = 0;
bool started = false;
bool firstRun = true;
int reading;
void setup() {
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
lcd.setCursor ( 0, 0 ); // go to the top left corner
lcd.print(" Espresso Scale "); // write this string on the top row
lcd.setCursor ( 0, 2 ); // go to the top left corner
lcd.print("Press button to brew"); // write this string on the top row
Serial.begin(38400);
Serial.println("Initializing the scale");
pinMode(ssrPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); //logic inversed as we use internal pullup to avoid needing resistor and hence many more cables
preparescale();
}
void preparescale() {
// parameter "gain" is ommited; the default value 128 is used by the library
// HX711.DOUT - pin #A1
// HX711.PD_SCK - pin #A0
scale.begin(A1, A0);
scale.set_scale(-1573);
}
void coffeeloop() {
if (firstRun == true) {
startTime = millis();
firstRun = false;
Serial.println("first run");
Serial.println(startTime);
}
// handle current time
time = millis() - startTime;
char timebuff[20];
lcd.setCursor(0, 2);
sprintf(timebuff, " %lus ", time / 1000);
Serial.println(timebuff); // why do I need this to stop time freezing at 10s?
lcd.print(timebuff);
// handle weight
float x = scale.get_units(5);
x = abs(x);
lcd.setCursor ( 0, 1 ); // go to the top left corner
char buffer[20];
char output[30];
dtostrf(x, 2, 0, buffer);
sprintf(output, " %s%s ", buffer, "g");
lcd.print(output);
// stop if target exceeded (allowance for overrun of pump after switching ssr
if (x > 35.0) {
digitalWrite(ssrPin, LOW);
digitalWrite(ledPin, LOW);
lcd.setCursor(0, 3);
lcd.print(" Brewing stopped ");
exit(0);
}
}
void loop() {
reading = digitalRead(buttonPin);
if (started == true) {
coffeeloop();
} else if (reading == LOW && started == false) {
started = true;
scale.tare();
digitalWrite(ssrPin, HIGH);
digitalWrite(ledPin, HIGH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment