HI! I wanted to make aquarium LEDs fade system based on arduino nano and DS1302. I´m still really new into this and I couldn´t make my own code so I found one on the internet. The problem is that the LEDs brightness is still the same and serial monitor is showing no date from rtc. I´ve changed nothing in that code except I´ve set the date for rtc. I´ve checked the connections many times and they are correct. I´ve also tried different sketch for rtc with differenet library and it worked fine, the serial monitor was showing correct date.
I have really no idea why so if anyone can help I would be really thankfull.
Here is the code and library I´ve used.
#include <Wire.h> // Need for LiquidCrystal library
#include <DS1302.h> // DS1302 RTC library
#include <LiquidCrystal_I2C.h> // I2C liquid Crystal library (I used a 1602 LDC)
// Init the DS1302
// on pins: CE, IO, CLK - 5, 6, 7 on arduino
DS1302 rtc(5, 6, 7);
const int led = 3; // pin 3 connects with the gate on the MOSFET
int ora, minut, secu, o, i, lum; // i - increase procent, lum - light intensity
int cmin; // var that checks if a minute has past
int crestere, scadere; // increase amount, decrease amount
float proc;
Time t;
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
// put your setup code here, to run once:
rtc.halt(false);
rtc.writeProtect(false);
// uncoment the next 3 lines to set the day of week, time and date, upload the code, coment them again and upload once more
// rtc.setDOW(SUNDAY); // Set Day-of-Week to SUNDAY
// rtc.setTime(22, 30, 55); // Set the time to xx:xx:xx (24hr format)
// rtc.setDate(13, 5, 2018); // Set the date to (day, month, year)
lcd.init(); //initialize the lcd
lcd.backlight(); // open the backlight
pinMode (led, OUTPUT); // signal output for the MOSFET's gate
// set the time at startup according to the hour interval - usefull on power loss
t = rtc.getTime();
// at startup, after power loss check the hour to set the corresponding lighting
ora = t.hour;
if (ora >=6 and ora <7) i = 35; // between 6 and 7 light is at 35%
if (ora >=7 and ora <8) i = 45; // between 7 and 8 light is at 45%
if (ora >=8 and ora <9) i = 65; // between 8 and 9 light is at 65%
if (ora >=9 and ora <10) i = 75; // between 9 and 10 light is at 75%
if (ora >=10 and ora <11) i = 85; // between 10 and 11 light is at 85%
if (ora >=11 and ora <15) i = 98; // between 11 and 15 light is at 98%
if (ora >=15 and ora <17) i = 85; // between 15 and 17 light is at 85%
if (ora >=17 and ora <19) i = 70; // between 17 and 19 light is at 70%
if (ora >=19 and ora <20) i = 50; // between 19 and 20 light is at 50%
if (ora >=20 and ora <21) i = 35; // between 20 and 21 light is at 35%
if (ora >=21 and ora <=23) i = 4; // between 21 and 23:59 light is at 4%
if (ora >=0 and ora <6) i = 4; // between 0 and 6 light is at 4%
crestere = 0; scadere = 0;
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// for (i = 0; i <= 255; i ++) {
lcd.setCursor(0,0);
lcd.print("T: "); lcd.print(rtc.getTimeStr());
t = rtc.getTime();
ora = t.hour; minut = t.min; secu = t.sec;
// increase stages - can be adjusted as you wish just make sure it woun't go above 100% or below 0%
if (ora == 5 and minut == 29 and secu == 0) crestere = 31; // from 4 to 35%
if (ora == 6 and minut == 50 and secu == 0) crestere = 10; // from 35 to 45%
if (ora == 7 and minut == 40 and secu == 0) crestere = 20; // from 45 to 65%
if (ora == 8 and minut == 50 and secu == 0) crestere = 10; // from 65 to 75%
if (ora == 9 and minut == 50 and secu == 0) crestere = 10; // from 75 to 85%
if (ora == 10 and minut == 45 and secu == 0) crestere = 13; // from 85 to 98%
// decrease stages
if (ora == 14 and minut == 59 and secu == 0) scadere = 13; // from 98 to 85%
if (ora == 16 and minut == 59 and secu == 0) scadere = 15; // from 85 to 70%
if (ora == 18 and minut == 59 and secu == 0) scadere = 20; // from 70 to 50%
if (ora == 19 and minut == 59 and secu == 0) scadere = 15; // from 50 to 35%
if (ora == 20 and minut == 59 and secu == 0) scadere = 31; // from 35 to 4%
// this two lines are for debug
Serial.print("crestere: "); Serial.println(crestere);
Serial.print("scadere: "); Serial.println(scadere);
// decrease light intensity
if (scadere > 0)
{
if (minut - cmin == 1)
{
i--;
scadere--;
}
cmin = minut;
}
// increase light intensity
if (crestere > 0)
{
o = minut - cmin; Serial.println(o);
if (minut - cmin == 1)
{
i++;
crestere --;
}
cmin = minut;
}
if (i < 4) i = 4; // failsafe - it always keeps the light to at least 4%
// set the light intensity of the LEDs
lum = i*255/100;
analogWrite(led, lum);
lcd.setCursor(0,1);
lcd.print (i); lcd.print(" %");
// debug data
Serial.print("Data: "); Serial.print(t.date); Serial.print("-"); Serial.print(t.mon);
Serial.print("-"); Serial.println(t.year); Serial.println(t.dow);
delay (1000);
lcd.clear();
}
And here is the library for ds1302
/*
DS1302.h - Arduino library support for the DS1302 Trickle Charge Timekeeping Chip
Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
This library has been made to easily interface and use the DS1302 RTC with
the Arduino.
You can find the latest version of the library at
http://www.RinkyDinkElectronics.com/
This library is free software; you can redistribute it and/or
modify it under the terms of the CC BY-NC-SA 3.0 license.
Please see the included documents for further information.
Commercial use of this library requires you to buy a license that
will allow commercial use. This includes using the library,
modified or not, as a tool to sell products.
The license applies to all part of the library including the
examples and tools supplied with the library.
*/
#ifndef DS1302_h
#define DS1302_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define FORMAT_SHORT 1
#define FORMAT_LONG 2
#define FORMAT_LITTLEENDIAN 1
#define FORMAT_BIGENDIAN 2
#define FORMAT_MIDDLEENDIAN 3
#define MONDAY 1
#define TUESDAY 2
#define WEDNESDAY 3
#define THURSDAY 4
#define FRIDAY 5
#define SATURDAY 6
#define SUNDAY 7
#define TCR_D1R2K 165
#define TCR_D1R4K 166
#define TCR_D1R8K 167
#define TCR_D2R2K 169
#define TCR_D2R4K 170
#define TCR_D2R8K 171
#define TCR_OFF 92
class Time
{
public:
uint8_t hour;
uint8_t min;
uint8_t sec;
uint8_t date;
uint8_t mon;
uint16_t year;
uint8_t dow;
Time();
};
class DS1302_RAM
{
public:
byte cell[31];
DS1302_RAM();
};
class DS1302
{
public:
DS1302(uint8_t ce_pin, uint8_t data_pin, uint8_t sclk_pin);
Time getTime();
void setTime(uint8_t hour, uint8_t min, uint8_t sec);
void setDate(uint8_t date, uint8_t mon, uint16_t year);
void setDOW(uint8_t dow);
char *getTimeStr(uint8_t format=FORMAT_LONG);
char *getDateStr(uint8_t slformat=FORMAT_LONG, uint8_t eformat=FORMAT_LITTLEENDIAN, char divider='.');
char *getDOWStr(uint8_t format=FORMAT_LONG);
char *getMonthStr(uint8_t format=FORMAT_LONG);
void halt(bool value);
void writeProtect(bool enable);
void setTCR(uint8_t value);
void writeBuffer(DS1302_RAM r);
DS1302_RAM readBuffer();
void poke(uint8_t addr, uint8_t value);
uint8_t peek(uint8_t addr);
private:
uint8_t _ce_pin;
uint8_t _data_pin;
uint8_t _sclk_pin;
uint8_t _burstArray[8];
uint8_t _readByte();
void _writeByte(uint8_t value);
uint8_t _readRegister(uint8_t reg);
void _writeRegister(uint8_t reg, uint8_t value);
void _burstRead();
uint8_t _decode(uint8_t value);
uint8_t _decodeH(uint8_t value);
uint8_t _decodeY(uint8_t value);
uint8_t _encode(uint8_t vaule);
};
#endif