// * Correction of Time with button on pin 9. pullUp on
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#define DS3231_I2C_ADDRESS 0x68
int buttonState = 0; // proměnná pro čtení stavu tlačítka
int ldr = 0; //analogovy pin kde je pripojeno LDR + R 10kOhm na +
int ldr_value = 0; //variable to store LDR values
int bri = 15;
Adafruit_7segment matrix = Adafruit_7segment();
byte decToBcd(byte val){
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val){
return ( (val/16*10) + (val%16) );
}
void setup() {
Serial.begin(9600); //spuštění serioveho rozhrani
Wire.begin();
matrix.begin(0x70);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year,,,, day of week (1=Sunday, 7=Saturday)
// setDS3231time(0,33,14,6,20,1,18);
}
void loop() {
displayTime(); // display the real-time clock data
displayTemp(); // display the temperature
bri = 5; // méně jasu
ldr_value = analogRead(ldr); //čte hodnoty LDR
Serial.println(ldr_value); //zobrazí hodnoty LDR na seriove lince
if (ldr_value < 400 ) bri = 15; // více jasu
matrix.setBrightness(bri); // 0 - 15
}
void displayTime(){
for (uint16_t counter = 0; counter < 10; counter++) {
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs3231(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
pinMode(9,INPUT_PULLUP);
int tlac = 9;
tlac = !digitalRead(9); //čtení tlačítka korekce a negace
byte se = second; //převod na pomocné proměnné
byte mi = minute;
byte ho = hour;
byte we = dayOfWeek;
byte dm = dayOfMonth;
byte mo = month;
byte ye = year;
int kor = 2;
//korekce sekund zpět
if (tlac>0 && se<30) { setDS3231time(0,mi,ho,we,dm,mo,ye);}
//korekce sekund vpřed
if (tlac>0 && se>30) { setDS3231time(59,mi,ho,we,dm,mo,ye);}
//změna času na letní - poslední neděle v březnu ve 2 hodiny
if ((dayOfWeek == 7)&&(dayOfMonth >= 25)&&(month == 3)&&(hour == 2)) {
setDS3231time(se,mi,3,we,dm,mo,ye); } // nastavení hodin na 3 hodinu
//změna času na zimní
if ((dayOfWeek == 7)&&(dayOfMonth >= 25)&&(month == 10)&&(hour == 1)&&(year != 1)) {
setDS3231time(se,mi,ho,we,dm,mo,1); } //rok použit jako indikace, že bylo léto
if ((dayOfWeek == 7)&&(dayOfMonth >= 25)&&(month == 10)&&(hour == 3)&&(year == 1)) {
setDS3231time(se,mi,2,we,dm,mo,0); } // nastavení hodin na 2 hodinu a příznak na 0
// příznak je tam, aby se ve 3 hodiny znovu nenastavovalo na 2
//------------------------------------------------------------------
if ( se <= 30 ) kor = 10;
if ( se > 30 ) kor = 6;
if ( hour > 9 ) matrix.writeDigitNum(0, (hour / 10), false);
matrix.writeDigitNum(1, (hour % 10), false); // this will write the number (0-9) to a single location 1
matrix.drawColon(true); // 0x02 - center colon (both dots)
matrix.writeDigitNum(3, (minute / 10), false);
matrix.writeDigitNum(4,(minute % 10), false);
matrix.writeDigitRaw(2,kor); // 2,3 double dot, 4,5 upper dot, 6,7 upper+double, 8,9 lower, 10,11 lower+double, 12 upper+lower
matrix.writeDisplay();
delay(500);
matrix.drawColon(false);
matrix.writeDisplay();
delay(500);
}
}
void displayTemp(){
byte temp = get3231Temp();
int temp2 = abs( temp );
matrix.clear();
matrix.writeDigitRaw(4,57); // 57 = " C "
matrix.writeDigitRaw(3,99); // 99 = " ° "
// matrix.writeDigitRaw(2,0x10); // decimal dot
if (temp < 0) matrix.writeDigitRaw(0,64); // and if the temperature is negative, we plot the minus sign to first place.
if (temp <= -10 ) matrix.writeDigitRaw(2,12);
if ( temp >= 10 ) matrix.writeDigitNum(0, (temp2/10), false); // position 0, value 1, show decimal)
if ( temp <= -10 ) matrix.writeDigitNum(0, (temp2/10), false); // position 0, value 1, show decimal)
matrix.writeDigitNum(1,( temp2 % 10), false); // position 1, value 9, show decimal)
matrix.writeDisplay();
Serial.println(temp); //zobrazí hodnoty teploty na seriove lince
delay(5000);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year){
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void getDateDs3231(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
float get3231Temp(){
byte tMSB, tLSB;
float temp3231;
Wire.beginTransmission(0x68);
Wire.write(0x11);
Wire.endTransmission();
Wire.requestFrom(0x68, 2);
if(Wire.available()) {
tMSB = Wire.read(); //2's complement int portion
tLSB = Wire.read(); //fraction portion
temp3231 = (tMSB & B01111111); //do 2's math on Tmsb
temp3231 += ( (tLSB >> 6) * 0.25 ); //only care about bits 7 & 8
}
else {
//oh noes, no data!
}
return temp3231;
}
Comments
Please log in or sign up to comment.