#include <EEPROM.h>
/*
#include "LC.h"

LC LC;
*/

namespace  LoadCell  {
    //forward declarations
    void begin(uint8_t dataPin, uint8_t clockPin);
    void readLoad();
    void calibrate();
    void tare();
    
    //variables
    int test = 0;
    int calibrationLoads[5] = {250, 500, 750, 1000, 1500};
    int loadValues[5] = {0, 0, 0, 0, 0};
    int loadValue = 0;
    boolean loadUpdated = false;
    uint8_t pinTare = 13;
  
    //functions
    void begin(uint8_t dataPin, uint8_t clockPin) {
        pinMode(dataPin, INPUT);
        pinMode(dataPin, INPUT);
    }

    void readLoad() {
        //variables for function
        static unsigned long lastCheck = 0;

        
        // check if elapsed time is long enough
        if (millis() > lastCheck + 500){
        
            //read value from board
            int currValue = 0;
            
            //add value to array
            unsigned int sum = 0;
            for (int i = 0; i < 5; i++) {
                
                if (i == 4)
                {
                    sum += currValue;
                    loadValues[i] = currValue;
                }
                else
                {
                    sum += loadValues[i + 1];
                    loadValues[i] = loadValues[i + 1]; 
                }
            }
            int avgValue = sum / 5;
            avgValue = (avgValue / 5 + (avgValue % 5 > 2)) * 5;  //round to nearest 5
            if (avgValue != loadValue){
                loadValue = avgValue;
                Serial.println("New load applied");
            }
            lastCheck = millis();
            loadUpdated = true;
        }
    }

    void calibrate() {
        
        while( millis() < 1000) {     //wait  1 sec after power on
            if(digitalRead(13)) {//PINB & (1 << PB5)) {   //if button pressed, start calibration
                delay(50);
                while(digitalRead(13)) { Serial.println("Waiting..."); }
                delay(50);
                uint8_t calibrationLoadIndex = 0;
                bool showLoad = true;
                while( calibrationLoadIndex < 5)  {  //loop through all 5 cal load steps
                  if (showLoad) {
                    Serial.println(calibrationLoads[calibrationLoadIndex]);
                    showLoad = false;
                  }

                  if(digitalRead(13)) {   //if button pressed, record current value
                    delay(50);
                    while(digitalRead(13)) { Serial.println("Waiting"); }
                    delay(50);
                    readLoad();
                    calibrationLoadIndex++;
                    showLoad = true;
                  }
                }
                
            }
        }
        Serial.println("Calibrated!");

        
    }

    void tare() {
        if(PINB & (1 << PB5))  // button pressed
        {
            
        }
    }
}

void setup() 
{
  Serial.begin(9600);
  pinMode(9, INPUT);
  
  LoadCell::begin(A2, A3);

  LoadCell::calibrate();
 
}

void loop() 
{


}