// 29-5 fliptimer changed form 1000 to 3000, for loop added reportLevels

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>

#include <NewPing.h>

#define voltageFlipPin1 7
#define voltageFlipPin2 6
#define sensorPin 1

#define TRIGGER_PIN  2  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     3  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

const int relaySet = 8;
int counter = 0;
int flipTimer = 1000;
int tempPin = 0;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
File myFile;


void setup() {
  Serial.begin(9600);
  lcd.begin(20, 4);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");


  lcd.begin(20, 4);        // initialize the lcd for 20 chars 4 lines, turn on backlight
  pinMode(voltageFlipPin1, OUTPUT);
  pinMode(voltageFlipPin2, OUTPUT);
  pinMode(sensorPin, INPUT);
  pinMode(relaySet, OUTPUT);
}

void setSensorPolarity(boolean flip) {
  if (flip) {
    digitalWrite(voltageFlipPin1, HIGH);
    digitalWrite(voltageFlipPin2, LOW);
  } else {
    digitalWrite(voltageFlipPin1, LOW);
    digitalWrite(voltageFlipPin2, HIGH);
  }
}

void loop() {

  setSensorPolarity(true);
  delay(flipTimer);
  int val1a = analogRead(sensorPin);
  delay(flipTimer);
  lcd.setCursor(0, 3); lcd.print(val1a);

  setSensorPolarity(false);
  delay(flipTimer);
  int val1b = 1023 - analogRead(sensorPin); // invert the reading
  delay(flipTimer);
  lcd.setCursor(3, 3); lcd.print(val1b);

  setSensorPolarity(true);
  delay(flipTimer);
  int val2a = analogRead(sensorPin);
  delay(flipTimer);
  lcd.setCursor(6, 3); lcd.print(val2a);

  setSensorPolarity(false);
  delay(flipTimer);
  int val2b = 1023 - analogRead(sensorPin); // invert the reading
  delay(flipTimer);
  lcd.setCursor(9, 3); lcd.print(val2b);

  setSensorPolarity(true);
  delay(flipTimer);
  int val3a = analogRead(sensorPin);
  delay(flipTimer);
  lcd.setCursor(12, 3); lcd.print(val3a);

  setSensorPolarity(false);
  delay(flipTimer);
  int val3b = 1023 - analogRead(sensorPin); // invert the reading
  delay(flipTimer);
  lcd.setCursor(15, 3); lcd.print(val3b);


  int val1 = (val1a + val2a + val3a) / 3;
  int val2 = (val1b + val2b + val3b) / 3;

  Serial.print  (val1);
  Serial.println (val2);

  reportLevels(val1, val2);

  lcd.setCursor(10, 0);
  lcd.print("cnt:");
  lcd.setCursor(14, 0);
  lcd.print(counter);

  Serial.println(sonar.ping_cm());
  delay (500);
  lcd.setCursor(8, 1); lcd.print("ping:");
  lcd.setCursor(13, 1); lcd.print(sonar.ping_cm() );

  int reading = analogRead(tempPin);

}

void reportLevels(int val1, int val2) {

  int avg = (val1 + val2) / 2;
  //avg = avg + avg;
  delay (500);
  //return

  //avg = avg / 3;

  String msg = "avg: ";
  msg += avg;
  Serial.println(avg);


  myFile = SD.open("log.txt", FILE_WRITE);
  if (myFile) {
    //Serial.print("Writing to test.txt...");

    int reading = analogRead(tempPin);

    // converting that reading to voltage, for 3.3v arduino use 3.3
    float voltage = reading * 5.0;
    voltage /= 1024.0;

    float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
    lcd.setCursor(10, 2); lcd.print("tmp:");
    lcd.setCursor(14, 2); lcd.print(temperatureC);



    lcd.setCursor(0, 3); lcd.print("                   ");

    myFile.print(avg);
    myFile.print(",");
    myFile.print(temperatureC);
    myFile.print(",");
    myFile.print(sonar.ping_cm());
    myFile.print(",");
    myFile.println(counter);

    myFile.close(); // close the file:
    lcd.setCursor(0, 2); lcd.print("done...");

  }
  else {
    lcd.setCursor(0, 2);    // if the file didn't open, print an error:
    lcd.print("error");
    Serial.println("error opening test.txt");
  }

  lcd.setCursor(0, 0);
  lcd.print("avg=");
  lcd.setCursor(4, 0);
  lcd.print(avg);

  if (avg > 500)  {
    lcd.setCursor(0, 1);
    lcd.print("vochtig ");
    digitalWrite(relaySet, LOW);
  }
  else  {
    lcd.setCursor(0, 1);
    lcd.print("te droog");
    delay (500);
    digitalWrite(relaySet, HIGH);
    delay (15000);   //waiting 15 sec
    digitalWrite(relaySet, LOW);

    counter = counter + 1;
   
  }
  delay(60000); //waiting 60 sec
}