/* *********************************************************
* 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, iTeaTimeSec, iTimeRemain;
bool bRunning = false;
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[4][20];
char emptyLine[20] = " "; // 20 Blanks
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(20, 4);
sprintf(strLine[0], "%20s", "Tea Maker V1.0 ");
sprintf(strLine[1], "%20s", "Starting.... ");
updateDisplay(true, 0);
//setDateTime();
setEEPromDefault();
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);
sprintf(strLine[2], "TeaTime: %imin %isec", iStoredMin, iStoredSec);
updateDisplay(false, 2);
Serial.println(iStoredMin);
Serial.println(iStoredSec);
delay(100);
}
int iLoopCnt = 0;
void loop() {
button1.tick();
delay (10);
iLoopCnt++;
if (iLoopCnt >= 100)
{
showClock();
updateProgress();
iLoopCnt = 0;
}
}
void updateDisplay(bool bAllLines, int iLine)
{
if (bAllLines)
{
for (int iLine = 0; iLine < 4; iLine++)
{
lcd.setCursor(0,iLine);
lcd.print(strLine[iLine]);
}
}
else
{
lcd.setCursor(0,iLine);
lcd.print(emptyLine);
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 setEEPromDefault()
{
Serial.print("EEPROM length: ");
Serial.println(EEPROM.length());
//Print the result of calling eeprom_crc()
Serial.print("CRC32 of EEPROM data: 0x");
Serial.println(eeprom_crc(), HEX);
EEPROM.update(EEPROM_MIN, 2);
EEPROM.update(EEPROM_SEC, 25);
}
unsigned long eeprom_crc(void) {
const unsigned long crc_table[16] = {
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
unsigned long crc = ~0L;
for (int index = 0 ; index < EEPROM.length() ; ++index)
{
crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4);
crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4);
crc = ~crc;
}
return crc;
}
void showClock()
{
getDateTime();
if (i_seconds%2)
{
sprintf(strLine[1], "%02i.%02i. %02i:%02i ", i_dayMonth, i_month, i_hours, i_minutes, i_seconds);
}
else
sprintf(strLine[1], "%02i.%02i. %02i:%02i * ", i_dayMonth, i_month, i_hours, i_minutes, i_seconds);
updateDisplay(false, 1);
}
void updateProgress()
{
static int iDots = 20;
static int iLastDots = 0;
if (bRunning)
{
int i = iTeaTimeSec/20;
iTimeRemain--;
iDots = iTimeRemain/i;
if (iDots != iLastDots)
{
iLastDots = iDots;
sprintf(strLine[3], "%20.*s",iDots, "*>>>>>>>>>>>>>>>>>>>");
updateDisplay(false, 3);
}
//Serial.print("Dots: ");
//Serial.println(iDots);
}
else
{
iDots = 20;
sprintf(strLine[3], "%20s", "start press 3s \"S\" ");
updateDisplay(false, 3);
}
}
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");
iTimeRemain = iTeaTimeSec = 60*iStoredMin + iStoredSec;
bRunning = true;
delay(100);
} // longPressStart Timer Reset