/*MIT - Copyright (c) 2023 SpiritXIV

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files in this Wokwi Project, to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/


/*A0(red) and A1(black) are the electrode pins, you can make an
electrode by attaching wire to the listede pins then about one centameter way
from the listed pins should be the corrasponding color of the power cords*/

#include <LiquidCrystal.h>

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

const int electrode1Pin = A0;
const int electrode2Pin = A1;

const int minValue = 0;
const int maxValue = 1023;
const float saltConcentrationAtMin = 0.0;
const float saltConcentrationAtMax = 100.0;

const float baselineThreshold = 5.0;

const int numIterations = 10;

const int resetThreshold = 4;

const float molarMass = 58.44;

void displayLoadingBar(int progress) {
  lcd.setCursor(0, 1);
  lcd.print("Loading:");
  lcd.setCursor(9, 1);
  lcd.print("[");
  lcd.setCursor(10, 1);
  for (int i = 0; i < 5; i++) {
    if (i < progress) {
      lcd.print("=");
    } else {
      lcd.print(" ");
    }
  }

  lcd.print("]");
}

//Start up Essentals
void startup() {
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Salt Project");
  lcd.setCursor(0, 1);
  for (int i = 0; i <= 16; i++) {
    displayLoadingBar(i);
    delay(100);
  }
  lcd.setCursor(0, 1);
  lcd.print("Name: 3          ");
  delay(1000);
}

void setup() {
  startup();
  delay(1000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("g/mol: ");
  lcd.setCursor(7, 0);
  lcd.print("Rec| Avg");

  lcd.setCursor(0, 1);
  lcd.print("V  : ");
  lcd.setCursor(7, 1);
  lcd.print("Rec| Avg");
  delay(2000);
  lcd.clear();

}

void loop() {
  float totalGramsOfSalt = 0.0;
  float totalVoltage = 0.0;
  int consecutiveZeroReadings = 0;

  lcd.setCursor(0, 0);
  lcd.print("g/m: ");

  lcd.setCursor(0, 1);
  lcd.print("V  : ");

  for (int i = 0; i < numIterations; ++i) {
    int conductivityValue = analogRead(electrode1Pin) - analogRead(electrode2Pin);

    float saltConcentration = map(conductivityValue, minValue, maxValue, saltConcentrationAtMin, saltConcentrationAtMax);

    if (saltConcentration < baselineThreshold) {
      saltConcentration = 0.0;
      consecutiveZeroReadings++;
    } else {
      consecutiveZeroReadings = 0;
    }

    float gramsOfSalt = 0.1 * saltConcentration;
    float gramsPerMol = gramsOfSalt / molarMass;

    float voltage = map(conductivityValue, minValue, maxValue, 0.0, 5.0);

    totalGramsOfSalt += gramsPerMol;
    totalVoltage += voltage;

    lcd.setCursor(5, 0);
    lcd.print(gramsPerMol, 2);

    lcd.setCursor(5, 1);
    lcd.print(voltage, 2);

    delay(100);
  }

  float averageGramsOfSalt = totalGramsOfSalt / numIterations;
  float averageVoltage = totalVoltage / numIterations;

  lcd.setCursor(9, 0);
  lcd.print(" | ");
  lcd.print(averageGramsOfSalt, 2);

  lcd.setCursor(9, 1);
  lcd.print(" | ");
  lcd.print(averageVoltage, 2);

  if (consecutiveZeroReadings >= resetThreshold) {
    lcd.setCursor(0, 0);
    lcd.setCursor(5, 0);
    lcd.print("-.-- | -.--");
    lcd.setCursor(5, 1);
    lcd.print("-.-- | -.--");
    delay(20);
    totalGramsOfSalt = 0.0;
    totalVoltage = 0.0;
  }

  delay(1000);
}
$abcdeabcde151015202530354045505560fghijfghij