#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>



#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);
#define DEGREE_SIGN  char(248)

#define ONE_WIRE_BUS 4 
// DS18B20 Temperatur Sensor
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temperatursensor=21.55;


#define WATER_SENSOR_HIGH 2                                     // D2
#define WATER_SENSOR_LOW  3                                     // D3


// 'Aquarium-Final-Small', 72x64px
const unsigned char Aquarium [] PROGMEM = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x1c, 0x38, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x1c, 0x38, 0x00, 0x00, 0x00, 
	0x00, 0x03, 0xf0, 0x00, 0x1c, 0x38, 0x00, 0x00, 0x00, 0x00, 0x03, 0x38, 0x00, 0x1c, 0x38, 0x00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, 0x8c, 0x1e, 0x73, 0x9c, 0x38, 0x0c, 0x38, 0xe6, 0x0f, 
	0x1c, 0x7e, 0x3f, 0xdc, 0x38, 0x0c, 0x38, 0x67, 0x07, 0xdf, 0xfe, 0x1f, 0xdc, 0x3c, 0x0c, 0x38, 
	0x67, 0x03, 0xff, 0xe6, 0x07, 0xdc, 0x3f, 0x0e, 0x38, 0xe7, 0x00, 0xff, 0x8e, 0x00, 0x1c, 0x3f, 
	0xee, 0x38, 0xce, 0x00, 0x0f, 0xfc, 0x00, 0x1c, 0x39, 0xff, 0x39, 0xdc, 0x00, 0x01, 0xf8, 0x00, 
	0x1c, 0x38, 0x7f, 0x3b, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x38, 0x0f, 0xbb, 0xb8, 0x00, 0x00, 
	0x00, 0x00, 0x1c, 0x38, 0x01, 0xff, 0x70, 0x00, 0x00, 0x01, 0xff, 0xfc, 0x38, 0x01, 0xfe, 0xe0, 
	0x00, 0x00, 0x07, 0xff, 0xfc, 0x38, 0x00, 0xff, 0xff, 0x00, 0x00, 0x1f, 0x00, 0x1c, 0x38, 0xc0, 
	0x7f, 0xff, 0x80, 0x1f, 0xfc, 0x00, 0x1c, 0x38, 0xc0, 0x3f, 0xff, 0xc0, 0x7f, 0xf0, 0x00, 0x1c, 
	0x38, 0x00, 0x00, 0x00, 0xe0, 0x7f, 0xc0, 0x00, 0x1c, 0x38, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 
	0x00, 0x1c, 0x38, 0x00, 0x00, 0x70, 0x3e, 0x00, 0x00, 0x0c, 0x1c, 0x38, 0x0e, 0x00, 0x70, 0x1f, 
	0xc0, 0x00, 0x0c, 0x1c, 0x38, 0x0e, 0x00, 0x00, 0x07, 0xfc, 0x00, 0x00, 0x1c, 0x38, 0x00, 0x00, 
	0x00, 0x00, 0x7f, 0xc0, 0x00, 0x1c, 0x38, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xfc, 0x00, 0x1c, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

volatile uint8_t PINCHANGE=0;
void IRQ_PINCHANGE(){PINCHANGE++;}


void setup ()
{
	Serial.begin(115200);
	Serial.print("Booting ");
	pinMode(WATER_SENSOR_HIGH, INPUT_PULLUP);
  pinMode(WATER_SENSOR_LOW, INPUT_PULLUP);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.cp437(true);
  display.clearDisplay();
  display.drawBitmap(57, 0, Aquarium, 72, 64, WHITE);
  display.display();
	sensors.begin();    // DS18b20
	
  delay(1000);

  attachInterrupt(digitalPinToInterrupt(WATER_SENSOR_HIGH),IRQ_PINCHANGE,CHANGE);
  attachInterrupt(digitalPinToInterrupt(WATER_SENSOR_LOW),IRQ_PINCHANGE,CHANGE);
	sei();      //enable all interrupts
	displayHighAndLow();
	Serial.println("Done");
}

void loop ()
{
	readDS18B20TemperaturSensor();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 12);
  display.print(F("Temperatur\n"));
	display.setTextColor(BLACK);
//  display.fillRect(24, 0, 5, 7, BLACK); clear one Char
	display.setCursor(6, 22);
	display.fillRect(0,21, 58, 9, WHITE); 
  display.print(temperatursensor);
	display.print(DEGREE_SIGN);
  display.println(F("C"));

  //display.fillRect(0, 0, 10, 10, BLACK);
	display.display();

  if(PINCHANGE){
		displayHighAndLow();
		PINCHANGE=0;
		Serial.println(F("PINCHANGE"));
	}
  delay(100);

}

#define H_y 50
#define C_SIZE 6
void displayHighAndLow(){
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

	display.setCursor(0, H_y);
  display.print("H");
  display.setCursor(25, H_y);
  display.print("L");

  if(digitalRead(WATER_SENSOR_HIGH)==false)	{display.fillCircle(14, H_y+(C_SIZE/2), C_SIZE, WHITE);}
																				else{display.fillCircle(14, H_y+(C_SIZE/2), C_SIZE, BLACK); 
																						 display.drawCircle(14, H_y+(C_SIZE/2), C_SIZE, WHITE);}
	if(digitalRead(WATER_SENSOR_LOW)==false)	{display.fillCircle(39, H_y+(C_SIZE/2), C_SIZE, WHITE);}
																				else{display.fillCircle(39, H_y+(C_SIZE/2), C_SIZE, BLACK); 
			 																		 	 display.drawCircle(39, H_y+(C_SIZE/2), C_SIZE, WHITE);}

  display.display();
}



void readDS18B20TemperaturSensor(){
  sensors.requestTemperatures(); // Send the command to get temperatures
/*  temperatursensor = sensors.getTempCByIndex(0);
  if(temperatursensor == DEVICE_DISCONNECTED_C) {
    Serial.println("Error: Could not read temperature data");
  }else{
		Serial.print("Temperatur: ");
		Serial.print(temperatursensor);
		Serial.println(" C");
	}*/
	temperatursensor+=0.01;
}
Loading chip...chip-ds1820