#include <Wire.h>
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Fonts/FreeSansBold9pt7b.h>
//#include <Fonts/FreeSansBold12pt7b.h>
OneWire oneWire(A0);
DallasTemperature ds(&oneWire);
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RTC_DS1307 rtc;
int mode = 0; // bp mode
int p = 0; // prog de chauffage
int haut = 0; // bp haut
int plus = 0; // bp plus
int moins = 0; // bp moins
int bas = 0; // bp bas
int heure = 0;
int minute = 0;
int seconde = 0;
int hhmm = 0;
long ttsec = 0; // temps total jour en s
long cumch = 0; // cumul temps chauffe jour en s
float tregh = 20.5; // temper haute
float tregb = 19.5;
float tecoh = 18.5;
float tecob = 17.5;
float tinsuf = 0;
float tbasse = tregb;
float thaute = tregh;
int aa = 0;
int ledch = 0;
float mini = 0;
float maxi = 0;
int tauxch = 0;
int lum = A3;
int plage = 0;
int alarme = 0;
int comp = 0;
float temperature;
float ecartemp = 0;
float ecarregh = 0;
float ecarregb = 0;
float ecarecoh = 0;
float ecarecob = 0;
int ok = 0;
int dminu = 0;
int uminu = 0;
void setup() {
Serial.begin(9600);
ds.begin(); // sonde activée
rtc.begin();
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(13, INPUT);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
oled.display();
delay(300);
oled.clearDisplay(); // clear display
oled.setFont(&FreeSansBold9pt7b);
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE);
// oled.setCursor(3, 3);
// int bpplus = digitalRead(7);
// int bpok = digitalRead(4);
// if (bpplus == 1) {
// heure ++ ; }
// oled.print("heure = ");
// oled.print(heure);
// oled.display();
while (ok == 0)
{
oled.clearDisplay();
oled.setCursor(0, 20);
oled.print("heure = ");
oled.setCursor(93, 20);
oled.print(heure);
oled.setCursor(60, 62);
oled.print("< midi >");
oled.display();
delay(200);
int bpplus = digitalRead(7);
int bpmoins = digitalRead(6);
int bpok = digitalRead(4);
if (bpplus == 1) { heure ++; }
if (heure > 23) { heure = 23; }
if (bpmoins == 1) { heure --; }
if (heure < 0) { heure = 0; }
if (bpok == 1) {ok ++;}
}
ok = 0;
while (ok == 0)
{
oled.clearDisplay();
oled.setCursor(0, 20);
oled.print("minute = ");
oled.setCursor(93, 20);
oled.print(dminu);
oled.setCursor(110, 20);
oled.print(uminu);
oled.setCursor(45, 62);
oled.print("< mode >");
oled.display();
delay(200);
int bpplus = digitalRead(7);
int bpmoins = digitalRead(6);
int bpmode = digitalRead(9);
if (bpplus == 1) { dminu ++; }
if (dminu > 5) { dminu = 5; }
if (bpmoins == 1) { dminu --; }
if (dminu < 0) { dminu = 0; }
if (bpmode == 1) {ok ++;}
}
ok = 0;
while (ok == 0)
{
oled.clearDisplay();
oled.setCursor(0, 20);
oled.print("minute = ");
oled.setCursor(93, 20);
oled.print(dminu);
oled.setCursor(110, 20);
oled.print(uminu);
oled.setCursor(60, 62);
oled.print("< midi >");
oled.display();
delay(200);
int bpplus = digitalRead(7);
int bpmoins = digitalRead(6);
int bpok = digitalRead(4);
if (bpplus == 1) { uminu ++; }
if (uminu > 9) { uminu = 9; }
if (bpmoins == 1) { uminu --; }
if (uminu < 0) { uminu = 0; }
if (bpok == 1) {ok ++;}
}
ok = 0;
minute = (dminu * 10) + uminu;
}
void loop() {
// lecture temperature
ds.requestTemperatures();
float lectemp = ds.getTempCByIndex(0);
temperature = lectemp + ecartemp;
// temperature insuffisante
if (temperature < tinsuf) {
alarme = 1;
}
if(temperature > tinsuf) {
alarme = 0;
}
//
if (temperature > tbasse) { aa = 0; }
if (temperature >= thaute) { // coupure CH
ledch = 0;
plage = 0;
if (maxi >= thaute) {
}
else {
digitalWrite(2, HIGH); // cde arret ch
delay(1000);
digitalWrite(2, LOW); // fin cde arret ch
maxi = temperature;
ledch = 0;
}
}
else if (temperature >= tbasse) { // plage regul
plage = 1;
mini = temperature;
maxi = temperature;
}
else { // allumage CH
ledch = 1;
plage = 0;
if (temperature < tbasse) {
if (aa >= 2) {
}
else {
digitalWrite(3, HIGH); // cde allum ch
delay(1000);
digitalWrite(3, LOW); // fin cde allum ch
maxi = temperature;
mini = temperature;
aa = 2;
}
}
else {
digitalWrite(3, HIGH); // cde allum ch
delay(1000);
digitalWrite(3, LOW); // fin cde allum ch
maxi = temperature;
mini = temperature;
aa = 2;
}
}
// valide bp mode
int bpmode = digitalRead(9);
if (bpmode == 1){
mode ++;
if (mode > 4) { mode = 0; }
}
int bpok = digitalRead(4);
if (bpok== 1) { mode = 0; }
// modes de fonctionnement P0 a P4
// fonctionnement sous-sol - simulation avec P0
// detection lumiere
lum = analogRead(A3);
if (lum < 300) {
// digitalWrite(9, HIGH); // > oled tem lum allumee
thaute = tregh;
tbasse = tregb;
tinsuf = tregb - 1;
}
if (lum > 300) {
// digitalWrite(9, LOW); // > oled tem lum eteinte
thaute = tecoh;
tbasse = tecob;
tinsuf = tecob - 1;
}
// fonctionnement RdC - simulation avec detection lumiere allumee
// mode f(temps)
if (p == 0) { // mode sous-sol eco
thaute = tecoh;
tbasse = tecob;
tinsuf = tecob - 1;
}
if (p == 1) { // mode jour
thaute = tregh;
tbasse = tregb;
tinsuf = tregb - 1;
}
if (p == 2) { // mode jour + eco nuit f(heure)
if (hhmm == 1) {
thaute = tecoh;
tbasse = tecob;
tinsuf = tecob - 1;
}
if (hhmm == 2) {
thaute = tregh;
tbasse = tregb;
tinsuf = tregb - 1;
}
if (hhmm == 1200) {
thaute = tecoh;
tbasse = tecob;
tinsuf = tecob - 1;
}
if (hhmm == 1201) {
thaute = tregh;
tbasse = tregb;
tinsuf = tregb - 1;
}
}
if (p == 3) { // mode jour + eco nuit + arret 1/2 journee
if (hhmm == 2300) {
thaute = tecoh;
tbasse = tecob;
tinsuf = tecob - 1;
}
if (hhmm == 800) {
thaute = tregh;
tbasse = tregb;
tinsuf = tregb - 1;
}
if (hhmm == 900) {
thaute = 0;
tbasse = 0;
tinsuf = tecob - 1;
}
if (hhmm == 1200) {
thaute = tregh;
tbasse = tregb;
tinsuf = tregb - 1;
}
if (hhmm == 1400) {
thaute = 0;
tbasse = 0;
tinsuf = tecob - 1;
}
if (hhmm == 1800) {
thaute = tregh;
tbasse = tregb;
tinsuf = tregb - 1;
}
}
if (p == 4) { // mode jour + eco nuit + arret journee
if (hhmm == 2300) {
thaute = tecoh;
tbasse = tecob;
tinsuf = tecob - 1;
}
if (hhmm == 800) {
thaute = tregh;
tbasse = tregb;
tinsuf = tregb - 1;
}
if (hhmm == 900) {
thaute = 0;
tbasse = 0;
tinsuf = tecob - 1;
}
if (hhmm == 1800) {
thaute = tregh;
tbasse = tregb;
tinsuf = tregb - 1;
}
}
if (mode == 1) { // modif P
oled.setTextColor(WHITE);
oled.setCursor(62, 2);
oled.print((char)15);
oled.display();
delay(100);
int bpplus = digitalRead(7);
if (bpplus == 1){
p = p + 1;
if (p > 4) { p = 4; }
}
int bpmoins = digitalRead(6);
if (bpmoins == 1) {
p = p - 1;
if (p < 0) { p = 0; }
}
if (mode != 1) {
oled.setCursor(62, 2);
oled.setTextColor(BLACK);
oled.print((char)15);
oled.setTextColor(WHITE);
oled.display();
}
}
if (mode == 4) { // modif compensation sonde temperature
oled.setCursor(62, 2);
oled.print((char)15);
oled.display();
delay(100);
int bpplus = digitalRead(7);
if (bpplus == 1){
ecartemp = ecartemp + 0.1;
}
int bpmoins = digitalRead(6);
if (bpmoins == 1) {
ecartemp = ecartemp - 0.1;
}
if (mode != 4) {
oled.setCursor(62, 2);
oled.setTextColor(BLACK);
oled.print((char)15);
oled.setTextColor(WHITE);
oled.display();
}
}
if (mode == 3) { // modif temperature maxi
oled.setCursor(62, 2);
oled.print((char)15);
oled.display();
delay(100);
int bpplus = digitalRead(7);
if (bpplus == 1){
if (thaute == tregh) { tregh = tregh + 0.1; }
if (thaute == tecoh) { tecoh = tecoh + 0.1; }
}
int bpmoins = digitalRead(6);
if (bpmoins == 1) {
if (thaute == tregh) { tregh = tregh - 0.1; }
if (thaute == tecoh) { tecoh = tecoh - 0.1; }
}
if (mode != 3) {
oled.setCursor(62, 2);
oled.setTextColor(BLACK);
oled.print((char)15);
oled.setTextColor(WHITE);
oled.display();
}
}
if (mode == 2) { // mode temperature mini
oled.setCursor(62, 2);
oled.print((char)15);
oled.display();
delay(100);
int bpplus = digitalRead(7);
if (bpplus == 1){
if (tbasse == tregb) { tregb = tregb + 0.1; }
if (tbasse == tecob) { tecob = tecob + 0.1; }
}
int bpmoins = digitalRead(6);
if (bpmoins == 1) {
if (tbasse == tregb) { tregb = tregb - 0.1; }
if (tbasse == tecob) { tecob = tecob - 0.1; }
}
if (mode != 2) {
oled.setCursor(62, 2);
oled.setTextColor(BLACK);
oled.print((char)15);
oled.setTextColor(WHITE);
oled.display();
}
}
// calcul de la periode de chauffage / temps en %
if (ttsec == 0) { ttsec = 1; }
if (ledch == 1) { cumch = cumch + 1; }
tauxch = (cumch * 100 / ttsec * 100) / 100;
// affichages
// page 1
oled.clearDisplay(); // clear display
//oled.setFont(&FreeSansBold12pt7b);
oled.setTextSize(2); // set text size
oled.setTextColor(WHITE); // set text color
// oled.drawRoundRect(0,0,53,13,1,WHITE);
// oled.drawRoundRect(75,0,53,13,1,WHITE);
// oled.drawRoundRect(105,51,23,13,1,WHITE);
oled.setCursor(0, 30);
if (mode == 4) { oled.setTextColor(SSD1306_BLACK, SSD1306_WHITE); }
if (temperature < 10) { oled.print(" "); }
oled.print(temperature);
oled.print(" ");
oled.print((char)247);
oled.print("C");
// oled.setFont(&FreeSansBold9pt7b);
oled.setTextSize(1);
DateTime now = rtc.now();
heure = now.hour();
minute = now.minute();
seconde = now.second();
ttsec = (heure * 3600.0) + (minute * 60.0) + seconde;
hhmm = (heure * 100) + minute;
oled.setCursor(30, 60); // set position to display (x,y)
if (now.hour() < 10) { oled.print("0"); }
oled.setTextColor(WHITE);
oled.print(now.hour(), DEC);
oled.print(":");
if (now.minute() < 10) { oled.print("0"); }
// oled.print(minu);
oled.print(now.minute(), DEC);
oled.print(":");
if (now.second() < 10) { oled.print("0"); }
oled.print(now.second(), DEC);
oled.display();
delay(2000);
// page 2
oled.clearDisplay();
oled.setCursor(30, 25);
oled.setTextColor(WHITE);
if (mode == 2) { oled.setTextColor(SSD1306_BLACK, SSD1306_WHITE); }
oled.print("min ");
oled.print(tbasse, 1);
oled.setCursor(63, 30);
if (plage == 1) {
oled.print((char)220);
}
if (plage == 0) {
oled.setTextColor(BLACK);
oled.print((char)220);
oled.setTextColor(WHITE);
// oled.print(" ");
}
oled.setCursor(30, 50);
if (mode == 3) { oled.setTextColor(SSD1306_BLACK, SSD1306_WHITE); }
oled.print("max ");
oled.print(thaute, 1);
oled.display();
delay(2000);
// page 3
oled.clearDisplay();
//oled.setCursor(3, 30);
oled.setCursor(50, 17);
oled.setTextColor(WHITE);
oled.print("CH: ");
if (ledch == 0) { oled.drawCircle(29, 57, 5, WHITE); }
if (alarme == 1) {
oled.fillCircle(29, 57, 5, WHITE);
oled.setCursor(27, 54);
oled.setTextColor(BLACK);
oled.print("!");
}
else if (ledch == 1) { oled.fillCircle(29, 57, 5, WHITE); }
oled.setCursor(50, 40);
oled.setTextColor(WHITE);
if (tauxch < 100) { oled.print(" "); }
if (tauxch < 10) { oled.print(" "); }
oled.print(tauxch);
oled.print(" % ");
oled.setCursor(50, 63);
if (mode == 1) { oled.setTextColor(SSD1306_BLACK, SSD1306_WHITE); }
oled.print("P : "); // set text
oled.print(p);
oled.display(); // display on OLED
delay(2000);
}