#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
#include "OneButton.h"
#include <EEPROM.h>
//#include <LibPrintf.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
#define EESIZE 256
#define EEPROM_MIN 10 //adresse minutes in EEProm
#define EEPROM_SEC 11 //adresse seconds in EEProm
#define DS1307_ADDRESS 0x68
byte zero = 0x00;
int col = 0;
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(9600);
//Wire.begin();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { //Ive changed the address //already chill
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Draw a single pixel in white
display.drawPixel(10, 10, SSD1306_WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
delay(2000);
// display.display() is NOT necessary after every single drawing command,
// unless that's what you want...rather, you can batch up a bunch of
// drawing operations and then update the screen all at once by calling
// display.display(). These examples demonstrate both approaches...
// 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