/* *********************************************************
* TeaBot V1.0 by SnakeP
* ********************************************************
* Code based on: https://www.youtube.com/c/CarloStramaglia
* Thanks ;)
* ********************************************************
* Single click - set time in minutes
* Double click - start timer
* Long click - reset
*/
#include <Wire.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#include "OneButton.h"
#include <EEPROM.h>
#include <LibPrintf.h>
#define EESIZE 256
#define EEPROM_MIN 10 //adresse minutes in EEProm
#define EEPROM_SEC 11 //adresse seconds in EEProm
#define DS1307_ADDRESS 0x68
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
byte zero = 0x00;
int col = 0;
int number, lastMin1, lastMin2, lastHour1, lastHour2;
int iStoredMin, iStoredSec;
Servo myservo;
//Servo position value definition
int POS_UP = 120; //Up-position
int POS_MIDDLE = 85; //Middle-position
int POS_DOWN = 35; //Down-position
char strLine[2][20];
int i_seconds ;
int i_minutes;
int i_hours;
int i_dayWeek;
int i_dayMonth;
int i_month;
int i_year;
// Push Button PIN definition
#define PIN_INPUT A2
OneButton button1(PIN_INPUT, true);
void setup() {
Serial.begin(115200);
Wire.begin();
// Initializes the LCD
lcd.begin(16, 2);
sprintf(strLine[0], "%16s", "Tea Maker V1.0");
sprintf(strLine[1], "%16s", "Starting....");
updateDisplay(true, 0);
setDateTime();
myservo.attach(2);
myservo.write(POS_UP);
button1.attachClick(singleClick_1);
button1.attachDoubleClick(doubleClick_1);
button1.attachLongPressStart(longPressStart_1);
iStoredMin = EEPROM.read(EEPROM_MIN);
iStoredSec = EEPROM.read(EEPROM_SEC);
delay(100);
}
int iLoopCnt = 0;
void loop() {
button1.tick();
delay (10);
iLoopCnt++;
if (iLoopCnt >= 100)
{
showClock();
iLoopCnt = 0;
}
}
void updateDisplay(bool bBothLines, int iLine)
{
if (bBothLines)
{
lcd.setCursor(0,0);
lcd.print(strLine[0]);
lcd.setCursor(0,1);
lcd.print(strLine[1]);
}
else
{
lcd.setCursor(0,iLine);
lcd.print(strLine[iLine]);
}
}
byte convertToBCD(byte val) { // Converts the decimal number to BCD
return ( (val / 10 * 16) + (val % 10) );
}
byte convertToDecimal(byte val) { // Converts from BCD to decimal
return ( (val / 16 * 10) + (val % 16) );
}
void getDateTime()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); // Stop at CI so that it can receive data
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
i_seconds = convertToDecimal(Wire.read());
i_minutes = convertToDecimal(Wire.read());
i_hours = convertToDecimal(Wire.read() & 0b111111);
i_dayWeek = convertToDecimal(Wire.read());
i_dayMonth = convertToDecimal(Wire.read());
i_month = convertToDecimal(Wire.read());
i_year = convertToDecimal(Wire.read());
}
void setDateTime() // Set the date and time of the DS1307
{
byte seconds = 03; // Values from 0 to 59
byte minutes = 02; // Values from 0 to 59
byte hours = 01; // Values from 0 to 23
byte dayWeek = 1; // Values from 0 to 6, where 0 = Domgino, 1 = Monday
byte dayMonth = 17; // Values from 1 to 31
byte month = 5; // Values from 1 to 12
byte year = 21; // Values from 0 to 99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); // Stop at CI so that it can receive data
// The lines below write the values of date and
// time they were placed in the variables above
Wire.write(convertToBCD(seconds));
Wire.write(convertToBCD(minutes));
Wire.write(convertToBCD(hours));
Wire.write(convertToBCD(dayWeek));
Wire.write(convertToBCD(dayMonth));
Wire.write(convertToBCD(month));
Wire.write(convertToBCD(year));
Wire.write(zero); // Start at CI
Wire.endTransmission();
}
void showClock()
{
getDateTime();
if (i_seconds%2)
{
sprintf(strLine[1], "%02i.%02i. %02i:%02i ", i_dayMonth, i_month, i_hours, i_minutes, i_seconds);
Serial.println(i_seconds);
}
else
sprintf(strLine[1], "%02i.%02i. %02i:%02i * ", i_dayMonth, i_month, i_hours, i_minutes, i_seconds);
updateDisplay(false, 1);
}
void singleClick_1()
{
Serial.println("Single Click Button 1");
// Serial.println(minutes);
delay(100);
} // singleClick Add minutes
void doubleClick_1()
{
Serial.println("Double Click Button 1");
delay(100);
} // doubleClick Timer Start
void longPressStart_1()
{
Serial.println("long press Button 1");
delay(100);
} // longPressStart Timer Reset