// # include <RotaryEncoder.h>
// #include <LiquidCrystal.h>
// #include <OneWire.h>
// // ds18b20
// OneWire ds(7);
// byte data[9];
// byte addr[8];
// float celsius, fahrenheit;
// #define ROTARYSTEPS 2
// #define ROTARYMIN -1
// #define ROTARYMAX 1
// // Setup a RoraryEncoder for pins A2 and A3:
// RotaryEncoder encoder(A2, A3);
// // Last known rotary position.
// int lastPos = 1;
// // ////////////Setup a LCD ////////////////////////
// LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
// // ///////////////////////////////
// bool sw_state = HIGH;
// int sw=3;
// int sel=1 ;
// int var=0 ;
// bool up= false;
// bool down= false;
// bool set1 =false;
// bool set2 =false;
// bool screen =false;
// bool same_state = false;
// String dir ="";
// byte lastButton;
// unsigned long lasbutStat=0;
// unsigned long lasbutStat_h=0;
// unsigned long deb =50;
// unsigned long prev = 0;
// unsigned long last_read;
// // /////////////////////////////////////////////////
// void setup() {
// lcd.begin(16,2);
// pinMode(sw,INPUT_PULLUP);
// lastButton =digitalRead(sw);
// encoder.setPosition(10 / ROTARYSTEPS); // start with the value of 10.
// // You may have to modify the next 2 lines if using other pins than A2 and A3
// PCICR |= (1 << PCIE1); // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
// PCMSK1 |= (1 << PCINT10) | (1 << PCINT11); // This enables the interrupt for pin 2 and 3 of Port C.
// }
// ISR(PCINT1_vect) {
// encoder.tick(); // just call tick() to check the state.
// }
// void loop() {
// if (millis() - last_read > 250)
// {
// ds18b20();
// last_read = millis();
// }
// debounce();
// enco();
// Screen();
// // ds18b20();
// }
// void lcd_print( String Value, int row , int col ) {
// lcd.setCursor(col,row);
// lcd.print(Value);
// }
// void Screen(){
// if (screen){
// // ds18b20();
// // lcd_print("NEW SCREEN", 0, 0);
// lcd_print(String (celsius), 0, 0);
// //
// // lcd.print((char)223); // print ° character
// // lcd.print("C");
// lcd_print(String (fahrenheit ), 1, 0);
// // lcd.setCursor(0, 1); // start to print at the first row
// // lcd.print(celsius); // print the temperature in Celsius
// // lcd_print(String ((char)223), 10, 0);
// }
// if(!screen){
// if (up && set1==false && set2== false){
// sel=1;
// up = false;
// }
// if (down && set1==false && set2== false ) {
// sel=2;
// down = false;
// }
// if (set1){
// if (up){
// var++;
// up=false;
// }
// if (down){
// var--;
// down=false;
// }
// }
// if (set2){
// if (up){
// dir ="CW ";
// up=false;
// }
// if (down){
// dir ="ACW";
// down=false;
// }
// }
// if(millis()-prev >100){
// lcd_print("Pos: ", 0 , 2 );
// lcd_print("Dir: ", 1, 2);
// if(sel ==1 ){
// lcd_print(">",0,0);
// lcd_print(" ",1,0);
// }
// lcd_print(String (var),0,10);
// lcd.print(" ");
// if(sel ==2 ){
// lcd_print(">",1,0);
// lcd_print(" ",0,0);
// }
// lcd_print((dir),1,10);
// prev=millis();
// }
// }
// }
// void enco(){
// int newPos = encoder.getPosition() * ROTARYSTEPS;
// if (newPos < ROTARYMIN) {
// encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
// newPos = ROTARYMIN;
// }
// else if (newPos > ROTARYMAX) {
// encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
// newPos = ROTARYMAX;
// }
// //
// if (lastPos != newPos) {
// lastPos = newPos;
// if (lastPos>0){
// up =true;
// }
// if (lastPos<0){
// down =true;
// }
// }
// }
// void debounce(){
// if (millis() - lasbutStat >= deb) {
// byte buttonState = digitalRead(sw);
// if (buttonState == LOW && lastButton == HIGH) // raising edge
// {
// if (sel == 1) set1 = !set1;
// if (sel == 2) set2 = !set2;
// lasbutStat_h =millis();
// }
// else if (buttonState == LOW && lastButton == LOW) // same
// {
// if (millis() - lasbutStat_h>=1000 && same_state==false) {
// same_state = true;
// screen=!screen;
// Serial.println(screen);
// lcd.clear();
// }
// }
// else if (buttonState == HIGH && lastButton == LOW) // failling edge
// {
// same_state = false;
// }
// lasbutStat = millis();
// lastButton =buttonState;
// }
// }
// void ds18b20() {
// if (!ds.search(addr))
// {
// ds.reset_search();
// return;
// }
// ds.reset();
// ds.select(addr);
// ds.write(0x44, 1); // start conversion, with parasite power on at the end
// ds.reset();
// ds.select(addr);
// ds.write(0xBE); // Read Scratchpad
// for (byte i = 0; i < 9; i++) data[i] = ds.read(); // we need 9 bytes
// // Convert the data to actual temperature
// // because the result is a 16 bit signed integer, it should
// // be stored to an "int16_t" type, which is always 16 bits
// // even when compiled on a 32 bit processor.
// int16_t raw = (data[1] << 8) | data[0];
// byte cfg = (data[4] & 0x60);
// // at lower res, the low bits are undefined, so let's zero them
// if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
// else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
// else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
// //// default is 12 bit resolution, 750 ms conversion time
// celsius = (float)raw / 16.0;
// fahrenheit = celsius * 1.8 + 32.0;
// }
# include <RotaryEncoder.h>
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <EEPROM.h>
// ds18b20
OneWire ds(7);
byte data[9];
byte addr[8];
float celsius, fahrenheit;
#define ROTARYSTEPS 2
#define ROTARYMIN -1
#define ROTARYMAX 1
// Setup a RoraryEncoder for pins A2 and A3:
RotaryEncoder encoder(A2, A3);
#define sw 3
// Last known rotary position.
int lastPos = 1;
// ////////////Setup a LCD ////////////////////////
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
// ///////////////////////////////
bool sw_state = HIGH;
byte sel=1 ;
int var=0 ;
String dir ="";
bool up= false;
bool down= false;
bool set1 =false;
bool set2 =false;
bool screen =false;
bool same_state = false;
byte lastButton;
unsigned long lasbutStat=0;
unsigned long lasbutStat_h=0;
unsigned long deb =50;
unsigned long prev = 0;
// /////////////////////////////////////////////////
void setup() {
lcd.begin(16,2);
pinMode(sw,INPUT_PULLUP);
lastButton =digitalRead(sw);
// EEPROM.get(0,lastPos);
// EEPROM.put(0,var);
// EEPROM.put(1,dir);
encoder.setPosition(10 / ROTARYSTEPS); // start with the value of 10.
// You may have to modify the next 2 lines if using other pins than A2 and A3
PCICR |= (1 << PCIE1); // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
PCMSK1 |= (1 << PCINT10) | (1 << PCINT11); // This enables the interrupt for pin 2 and 3 of Port C.
}
//PCINT 10 for A2
ISR(PCINT1_vect) {
encoder.tick(); // just call tick() to check the state.
}
unsigned long last_read;
void loop() {
if (millis() - last_read > 250)
{
ds18b20();
last_read = millis();
}
debounce();
enco();
variable();
if (millis() - prev >100){
Screen();
prev=millis();
}
}
void lcd_print( String Value, int row , int col ) {
lcd.setCursor(col,row);
lcd.print(Value);
}
void Screen(){
if (screen){
lcd_print(String (celsius), 0, 0);
lcd_print(String (fahrenheit ), 1, 0);
// lcd.setCursor(0, 1); // start to print at the first row
// lcd.print(celsius); // print the temperature in Celsius
// lcd_print(String ((char)223), 0, 0);
//
// lcd.print((char)223); // print ° character
// lcd.print("C");
}
if(!screen){
lcd_print("SET : ", 0 , 2 );
lcd_print("TIMER : ", 1, 2);
if(sel ==1 ){
lcd_print(">",0,0);
lcd_print(" ",1,0);
}
lcd_print(String (var),0,10);
lcd.print(" ");
if(sel ==2 ){
lcd_print(">",1,0);
lcd_print(" ",0,0);
}
lcd_print((dir),1,10);
}
}
void variable (){
if (up && set1==false && set2== false){
sel=1;
up = false;
}
if (down && set1==false && set2== false ) {
sel=2;
down = false;
}
if (set1){
if (up && var<= 60){
var++;
up=false;
}
if (down && var >= -25){
var--;
down=false;
}
}
if (set2){
if (up){
dir ="CW ";
up=false;
}
if (down){
dir ="ACW";
down=false;
}
}
}
void enco(){
int newPos = encoder.getPosition() * ROTARYSTEPS;
if (newPos < ROTARYMIN) {
encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
newPos = ROTARYMIN;
}
else if (newPos > ROTARYMAX) {
encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
newPos = ROTARYMAX;
}
//
if (lastPos != newPos) {
lastPos = newPos;
if (lastPos>0){
up =true;
}
if (lastPos<0){
down =true;
}
}
}
void debounce(){
if (millis() - lasbutStat >= deb) {
byte buttonState = digitalRead(sw);
if (buttonState == LOW && lastButton == HIGH) // raising edge
{
if (sel == 1){
set1 = !set1;
// EEPROM.get(0,var);
}
if (sel == 2){
set2 = !set2;
// EEPROM.get(1,dir);
}
lasbutStat_h =millis();
}
else if (buttonState == LOW && lastButton == LOW) // same
{
if (millis() - lasbutStat_h>=1000 && same_state==false) {
same_state = true;
screen=!screen;
Serial.println(screen);
lcd.clear();
}
}
else if (buttonState == HIGH && lastButton == LOW) // failling edge
{
same_state = false;
}
lasbutStat = millis();
lastButton =buttonState;
}
}
void ds18b20() {
if (!ds.search(addr))
{
ds.reset_search();
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (byte i = 0; i < 9; i++) data[i] = ds.read(); // we need 9 bytes
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
}