/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-oled
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 6 // Pin Arduino a cui colleghiamo il pin DQ del sensore
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
int relay = 7;
OneWire oneWire(ONE_WIRE_BUS); // Imposta la connessione OneWire
DallasTemperature sensore(&oneWire); // Dichiarazione dell'oggetto sensore
float celsius = 0.0;
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int encoderPin1 = 2;
int encoderPin2 = 3;
int buttonPin = 4;
int buttonState = 0; // variable for reading the pushbutton status
int preveButtonState = 0;
int started = 0;
int modeButtonPin = 5;
int prevModeButtonState = 0;
int modeButtonState = 0;
int mode = 0; // 0 regola temperatura 1 regola tempo
volatile int ultima_temperatura_richiesta = 0;
volatile int temperatura_richiesta = 70;
volatile int ultimo_tempo_richiesto = 0;
volatile int tempo_richiesto = 0;
long tempo_richiesto_sec = 0*60;
int finito_tempo = 0;
int lastMSB = 0;
int lastLSB = 0;
int temp;
float start = 0;
String res = "02:00:00";
String r = "00:00:10";
//#include <ezBuzzer.h> // ezBuzzer library
//const int BUZZER_PIN = 8;
//ezBuzzer buzzer(BUZZER_PIN); // create ezBuzzer object that attach to a pin;
//
//// notes in the melody:
//int melody[] = {
// NOTE_E5, NOTE_E5, NOTE_E5,
// NOTE_E5, NOTE_E5, NOTE_E5,
// NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
// NOTE_E5,
// NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
// NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
// NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
// NOTE_D5, NOTE_G5
//};
//
//// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:
//int noteDurations[] = {
// 8, 8, 4,
// 8, 8, 4,
// 8, 8, 8, 8,
// 2,
// 8, 8, 8, 8,
// 8, 8, 8, 16, 16,
// 8, 8, 8, 8,
// 4, 4
//};
void setup() {
sensore.begin();
pinMode(relay, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(encoderPin1, INPUT_PULLUP);
pinMode(encoderPin2, INPUT_PULLUP);
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
start = millis();
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(1000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
oled.println("Hello World!"); // text to display
oled.display(); // show on OL}
}
void temperatura() {
sensore.requestTemperatures(); // richiesta lettura temperatura
// Restituzione della temperatura letta
// in gradi Celsius
// temperatura in Celsius
celsius = sensore.getTempCByIndex(0);
if (celsius<10){celsius=10;}
if (celsius>99){celsius=99;}
// Visualizzazione delle letture
// della temperatura sulla Serial monitor
//Serial.print("C:");
//Serial.println(celsius);
}
void interfaccia(){
oled.clearDisplay();
if (mode==0){
oled.drawRect(2, 1, 70, 15, 2);
}
oled.setCursor(5, 5);
oled.println("Temp. ric"); // text to display
if (mode==1){
oled.drawRect(2, 16, 70, 15, 2);
}
oled.setCursor(5, 20);
oled.println("Durata ric"); // text to display
oled.setCursor(100, 5);
oled.println(String(temperatura_richiesta)+" C"); // text to display
oled.setCursor(78, 20);
oled.println(r); // text to display
oled.setCursor(5, 35);
oled.println("Temperatura"); // text to display
String celsius_string;
int celsius_length;
celsius_string = String(celsius)+" C";
celsius_length = celsius_string.length() * 4;
oled.setCursor(110-celsius_length, 35);
oled.println(celsius_string); // text to display
oled.setCursor(5, 50);
oled.println("Timer"); // text to display
oled.setCursor(78, 50);
oled.println(res); // text to display
oled.display(); // show on OLED
}
int log_ultimo_tempo_richiesto;
void loop() {
//buzzer.loop();
if(finito_tempo==1){
tone(8, 262, 500);
//int length = sizeof(noteDurations) / sizeof(int);
//buzzer.playMelody(melody, noteDurations, length);
}else{
noTone(8);
//buzzer.stop();
}
temperatura();
if (celsius < temperatura_richiesta && started == 1 && finito_tempo==0){
digitalWrite(relay, HIGH);
}else if (digitalRead(relay)==HIGH){
digitalWrite(relay, LOW);
}
buttonState = digitalRead(buttonPin);
modeButtonState = digitalRead(modeButtonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH && buttonState != preveButtonState) {
if(started==0) {
started = 1;
start = millis();
} else {
started = 0;
}
if(finito_tempo==1){
finito_tempo=0;
started=0;
}
// turn LED on:
Serial.println("TEST");
Serial.println(started);
Serial.println(finito_tempo);
preveButtonState = buttonState;
}else if (buttonState != preveButtonState){
preveButtonState = buttonState;
}
if (modeButtonState == HIGH && modeButtonState != prevModeButtonState) {
if(mode==0) {
mode = 1;
} else {
mode = 0;
}
// turn LED on:
Serial.println("TEST");
Serial.println(started);
prevModeButtonState = modeButtonState;
}else if (modeButtonState != prevModeButtonState){
prevModeButtonState = modeButtonState;
}
if (started==1){
secondsToHMS();
}
secondsToHMS2();
interfaccia();
}
void updateEncoder() {
if(mode==0){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) | LSB; //converting the 2 pin value to single number
int sum = (ultima_temperatura_richiesta << 2) | encoded; //adding it to the previous encoded value
if (sum == 0b1000) temperatura_richiesta ++;
if (sum == 0b0010) temperatura_richiesta --;
if (temperatura_richiesta<0) temperatura_richiesta = 0;
if (temperatura_richiesta>99) temperatura_richiesta = 100;
ultima_temperatura_richiesta = encoded; //store this value for next time
}else{
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) | LSB; //converting the 2 pin value to single number
int sum = (ultimo_tempo_richiesto << 2) | encoded; //adding it to the previous encoded value
if (sum == 0b1000) tempo_richiesto ++;
if (sum == 0b0010) tempo_richiesto --;
if (tempo_richiesto<0) tempo_richiesto = 0;
ultimo_tempo_richiesto = encoded; //store this value for next time
finito_tempo=0;
tempo_richiesto_sec = tempo_richiesto*60;
}
}
char secondsToHMS( )
{
int t;
int s;
int m;
int h;
float ss;
ss = tempo_richiesto_sec-(millis()-start)/1000;
if (ss<=0){
ss=0;
finito_tempo=1;
start = millis();
started=0;
}
t = ss;
s = t % 60;
t = (t - s)/60;
m = t % 60;
t = (t - m)/60;
h = t;
String h_str = "0";
if(h>9){h_str="";}
String s_str = "0";
if(s>9){s_str="";}
String m_str = "0";
if(m>9){m_str="";}
res = h_str + String(h,DEC) + ":" + m_str + String(m,DEC) + ":" + s_str + String(s,DEC);
};
char secondsToHMS2()
{
int t;
int s;
int m;
int h;
float ss;
t = tempo_richiesto*60;
s = t % 60;
t = (t - s)/60;
m = t % 60;
t = (t - m)/60;
h = t;
String h_str = "0";
if(h>9){h_str="";}
String s_str = "0";
if(s>9){s_str="";}
String m_str = "0";
if(m>9){m_str="";}
r = h_str + String(h,DEC) + ":" + m_str + String(m,DEC) + ":" + s_str + String(s,DEC);
if(started==0 and finito_tempo==0){
res=r;
}
}