//
//    FILE: endless-selzer.ino
//  AUTHOR: Ian Charnas
// VERSION: 0.1.0
// PURPOSE: Display remaining CO2
//
// HISTORY:
// 0.1.0    2021-07-31 initial version
//

#include "HX711.h"
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"

// Connect LCD via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);

HX711 scale1;
HX711 scale2;

uint8_t dataPin1 = 14;
uint8_t clockPin1 = 12;

uint8_t dataPin2 = 13;
uint8_t clockPin2 = 15;

float calibration_factor1 = -882000;
float zero_factor1 = -32861;

float calibration_factor2 = -882000;
long zero_factor2 = 660518;

float days_per_pound = 50.444444;

float empirical_offset = 0; // difference between bathroom scale and our scale (due to only having 2 active sensors not 4)

void setup()
{
  Serial.begin(115200);
  Serial.println("ENDLESS SELTZER!");

  // Connect to the signal amplifier for load cell 1 (using values found using sparkfun's calibration example code)
  scale1.begin(dataPin1, clockPin1);
  scale1.set_scale(calibration_factor1);
  scale1.set_offset(zero_factor1);

  // Connect to the signal amplifier for load cell 2 (using values found using sparkfun's calibration example code)
  scale2.begin(dataPin2, clockPin2);
  scale2.set_scale(calibration_factor2);
  scale2.set_offset(zero_factor2);

  // Setup the LCD's number of rows and columns
  lcd.begin(16,2);
  lcd.setBacklight(HIGH);
}

void loop()
{
  float weight1, weight2, total;
  float remaining_days, remaining_months, remaining_years;

  // Pretend that using 2 active load cells and 2 dummy load cells (not connected because we didn't
  // have enough signal amplifiers) and then multiplying their sum by 2 is equivalent to 
  // actually measuring all 4 load cells. Use a fudge factor ("empirical_offset") to make the scale
  // then read the same as a bathroom scale read.
  weight1 = -scale1.get_units(10);
  weight2 = -scale2.get_units(10);
  total = (weight1 + weight2) * 2.0 + empirical_offset;

  // Print out the results to the USB serial port
  Serial.print("Scale 1: ");
  Serial.print(weight1, 1);
  Serial.print(" lbs, Scale2: ");
  Serial.print(weight2, 1);
  Serial.print(" lbs, Total: ");
  Serial.print(total);
  Serial.print(" lbs ");

  // Use the days_per_pound hard-coded measured value to calculate
  // how many years, months, and days of CO2 are remaining in the tank
  remaining_years = 0;
  remaining_months = 0;
  remaining_days = days_per_pound * total;
  while (remaining_days >= 365.0) {
    remaining_years += 1.0;
    remaining_days -= 365.0;
  }
  while (remaining_days >= 30.416) {
    remaining_months += 1.0;
    remaining_days -= 30.416;
  }

  // Print out the remaining years, months, and days to the USB serial port
  Serial.print("==> Remaining: ");
  Serial.print(remaining_years, 0);
  Serial.print("Years, ");
  Serial.print(remaining_months, 0);
  Serial.print("Months, ");
  Serial.print(remaining_days, 0);
  Serial.println("Days");

  // Display a condensed version of the output to the 16x2 character LCD
  lcd.setCursor(0,0);
  lcd.print(total,1);
  lcd.print(" lbs CO2");
  lcd.setCursor(0,1);
  lcd.print(remaining_years,0);
  lcd.print("Y, ");
  lcd.print(remaining_months,0);
  lcd.print("M, ");
  lcd.print(remaining_days,0);
  lcd.print("D");

  delay(250);
}

// END OF FILE
