#include <BH1750.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
RTC_DS1307 rtc;
BH1750 lightMeter(0x23);
Adafruit_BME680 bme;
#define SEALEVELPRESSURE_HPA (1013.25)
String var, command;
char first, second, third, c_array[5];
int address = 0;
float temp, humid, press, gas, lux;
String dataType[5] = {"temp.txt","press.txt","humid.txt","gas.txt","lux.txt"};
#include <SD.h>
File myFile;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int btn1 = 1;
int btn2 = 2;
int btn3 = 3;
int lastButtonState1 = LOW;
int lastButtonState2 = LOW;
int lastButtonState3 = LOW;
int menuVal = 0;
String menu[] = {"temp","press","humid","gas","lux"};
int val1,val2,val3;
int timeHour,timeMin,timeSec;



void setup() {
	// put your setup code here, to run once:
 Serial.begin(9600);
 Serial.println("Starting");
  //SDI-12
  Serial1.begin(1200, SERIAL_7E1);      //SDI-12 UART, configures serial port for 7 data bits, even parity, and 1 stop bit
  pinMode(7, OUTPUT);                   //DIRO Pin
  //DIRO Pin LOW to Send to SDI-12
  digitalWrite(7, LOW);
  delay(100);
  Serial1.println("HelloWorld");
	delay(100);
  //HIGH to Receive from SDI-12
  digitalWrite(7, HIGH);
  Wire.begin();
  lightMeter.begin();
  Serial.println(F("BH1750 Test begin"));
  Serial.println(F("BME680 test"));
 /* if (!bme.begin(0x76)) {
  	Serial.println("Could not find a valid BME680 sensor, check wiring!");
  	while (1);
  }
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms 
*/
	  if (!SD.begin(53)) {
    Serial.println("Card initialization failed!");
    while (true);
  }
	  File textFile = SD.open("data.txt");
  if (textFile) {
    Serial.print("data.txt: ");
    while (textFile.available()) {
      Serial.write(textFile.read());
    }
    textFile.close();
  } else {
    Serial.println("error opening data.txt!");
  }
   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
   pinMode(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
  pinMode(btn3, INPUT_PULLUP);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
    saveData(10.0, '+',0);saveData(0.001, '+',0);saveData(32.231, '+',0);saveData(9.87, '+',0);;saveData(11.10, '!',0);
readData();
}

void loop() {
}
void saveData(float data, char mark, int type)
{
  myFile = SD.open(dataType[type], FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    myFile.println(data);

    // close the file:
    myFile.close();
  } 
  myFile = SD.open("data.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to data.txt...");
    myFile.println(data);
    myFile.println(mark);
    // close the file:
    myFile.close();
  }
}
void readData()
{ 
  
  int i = 0;

  myFile = SD.open("data.txt");
  if (myFile) {
    Serial.println("data.txt:");
    while (myFile.available()) {
      String list = myFile.readStringUntil('+');
        Serial.println(list);
        i += 1;
      
    }
    myFile.close();
  } else {
    Serial.println("error opening data.txt");
  }
}
GND5VSDASCLSQWRTCDS1307+