#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <HX711.h>
#define SW1 A2
HX711 loadcell;
//#include <HX711_ADC.h>
//HX711_ADC LoadCell(3, 1);
const long LOADCELL_OFFSET = 50682624;
const long LOADCELL_DIVIDER = 5895655;
int running = false;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSerif9pt7b.h>
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void intro() {
oled.ssd1306_command(SSD1306_DISPLAYON);
oled.clearDisplay();
//oled.on();
oled.setFont(&FreeSansBold12pt7b);
oled.setCursor(10, 30);
oled.print("Digital");
oled.setCursor(10, 60);
oled.print("Scale");
oled.display();
delay(1000);
oled.clearDisplay();
oled.display();
}
void setup() {
Serial.begin(115200);
Serial.println("starting");
// put your setup code here, to run once:
pinMode(SW1, INPUT_PULLUP);
if (!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
}
//oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
oled.clearDisplay();
oled.setTextSize(1); // Draw 2X-scale text
oled.setTextColor(SSD1306_WHITE);
loadcell.begin(A1, A0);
loadcell.set_scale(0.42);
//loadcell.set_offset(2100);
loadcell.tare();
loadcell.power_down();
//LoadCell.begin();
//LoadCell.start(1000);
//LoadCell.setCalFactor(0.42);
start();
}
void start() {
running = true;
intro();
loadcell.power_up();
delay(300);
loadcell.tare();
}
int lm = 0;
bool btnon = false;
int laston = 0;
int pressed = 0;
void loop() {
// put your main code here, to run repeatedly:
if (running) {
if (lm > 20) {
oled.setCursor(30, 40);
//LoadCell.update();
int d = loadcell.get_units(10);
//oled.print(millis());
oled.print(d);
oled.print(" g");
oled.display();
lm = 0;
}
lm++;
if (!btnon && !digitalRead(SW1)) {
btnon = true;
//laston = millis();
loadcell.tare();
lm = 21;
pressed = millis();
}
if (btnon && digitalRead(SW1)) {
//if (millis() - laston > 2) {
btnon = false;
loadcell.tare();
lm = 21;
return;
/*} else {
laston = millis();
}*/
}
if (btnon && (millis() - pressed) > 4000) {
//oled.off();
oled.ssd1306_command(SSD1306_DISPLAYOFF);
loadcell.power_down();
running = false;
btnon = false;
delay(1000);
}
delay(10);
oled.clearDisplay();
} else {
if (!digitalRead(SW1)) {
//delay(100);
//if (digitalRead(4)) return;
start();
} else {
//delay(1000);
}
}
}