Dear All,
Need help with the code to save the settings.
I'm working on making LED controller where LED's will start/stop at specified time. LED brightness will start to increase at start time from 0% & will reach 100% at midday and than back to 0% at stop time. This thing is working as I'm able to start/stop LED's and increase/decrease LED brightness.
My problem is when there is a power cut?? How Arduino will know what brightness LED's has to be set according to current time. I know that I have to say the setting in EEPROM or SD card but what settings need to be saved I'm not able to figure out.
Need help or guidance in right direction.
Regards
BW
#include <UTFT.h>
#include <UTouch.h>
//#include <UTFT_Buttons.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <DS1307new.h>
#define ONE_WIRE_BUS_PIN 10
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern prog_uint16_t img_picube_gif[2500];
extern prog_uint16_t img_wallpaper_jpg[4332];
#define LARGE true
#define SMALL false
UTFT myGLCD(ITDB32S,38,39,40,41);
UTouch myTouch(6,5,4,3,2);
//UTFT_Buttons myButtons(&myGLCD, &myTouch);
int i;
int x, y;
byte dispScreen=0;
unsigned long prevMillis5 = 0;
char *Day[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
char *Mon[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
//------------------Time and Date------------
struct RTC_T
{
int tHour;
int tMinute;
int tDow;
int tDay;
int tMonth;
int tYear;
} tmpRTC, prevRTC;
//------------------LED Variables------------
int screenno = 0;
int led12 = 12;
int brightness1;
int brightness = 0; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
long startTime = 20;
long stopTime = 21;
long interval = (stopTime - startTime) * 7058;
//-------------------------------------------------RTC
void printTime(int thour, int tminute, int posx, int posy)
{
char tmpTime[6], charT[3];
tmpTime[0] = '\0';
if (thour>=0 && thour<=9) { //add space
strcat(tmpTime, " ");
itoa(thour, charT, 10);
strcat(tmpTime, charT);
}
else
itoa(thour, tmpTime, 10);
strcat(tmpTime, ":");
if (tminute>=0 && tminute<=9) { //add 0
strcat(tmpTime, "0");
itoa(tminute, charT, 10);
strcat(tmpTime, charT);
}
else {
itoa(tminute, charT, 10);
strcat(tmpTime, charT);
}
myGLCD.print(tmpTime, posx, posy); // Display time
//Serial.print(tmpTime);
}
void printDate(int x, int y)
{
char chDate[25], tmpChar[5];
//strcat(chDate, " ");
chDate[0] = '\0';
strcat(chDate, Day[RTC.dow]);
strcat(chDate, ", ");
itoa(RTC.day, tmpChar, 10);
strcat(chDate, tmpChar);
strcat(chDate, " ");
strcat(chDate, Mon[RTC.month]);
strcat(chDate, " ");
itoa(RTC.year, tmpChar, 10);
strcat(chDate, tmpChar);
//strcat(chDate, " ");
myGLCD.print(chDate, x, y); //Display date
//Serial.print(chDate);
}
void SaveRTC()
{
RTC.stopClock();
RTC.fillByYMD(tmpRTC.tYear, tmpRTC.tMonth, tmpRTC.tDay);
RTC.fillByHMS(tmpRTC.tHour, tmpRTC.tMinute, 0);
RTC.setTime();
delay(10);
RTC.startClock();
delay(10);
}
//--------------------------------------------------misc
void changeFont(boolean font, byte cr, byte cg, byte cb, byte br, byte bg, byte bb){
myGLCD.setBackColor(br, bg, bb); //font background
myGLCD.setColor(cr, cg, cb); //font colour
if (font==LARGE)
myGLCD.setFont(BigFont); //font size
else if (font==SMALL)
myGLCD.setFont(SmallFont);
}
//--------------------------------------------------Display Screens
void background(){
//HEADER
myGLCD.setColor(255, 0, 0);
myGLCD.setBackColor(255, 0, 0);
myGLCD.fillRect(1, 1, 319, 239);
myGLCD.setColor(255, 255, 255);
//BODY
myGLCD.setColor(153, 50, 204);
myGLCD.setBackColor(153, 50, 204);
myGLCD.fillRect(1, 25, 319, 213);
myGLCD.setColor(255, 255, 255);
//MAIN BODY
//FOOTER
myGLCD.setColor(255, 0, 0);
myGLCD.setBackColor(255, 0, 0);
myGLCD.fillRect(1, 215, 319, 239);
myGLCD.setColor(255, 255, 255);
}
void ledBright(){
if (RTC.hour == startTime){
// Serial.print(RTC.hour);
// Serial.print(" ---- ");
// //Serial.print(c);
// Serial.print(" ---- ");
// Serial.print(startTime);
// Serial.print(" ---- ");
// Serial.print(stopTime);
// Serial.print(" ---- ");
// Serial.print(interval);
// Serial.print(" ---- ");
// Serial.print(brightness);
// Serial.print(" ---- ");
// Serial.println(brightness1);
analogWrite(led12, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
for (int i=0; i<1; i++) {
brightness1 = map(brightness, 0, 255, 1, 100);
myGLCD.setColor(0, 0, 255);
myGLCD.setBackColor(0, 0, 255);
myGLCD.fillRoundRect(98, (i*30)+30 , 180, (i*30)+50);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect(98, (i*30)+30 , 180, (i*30)+50);
myGLCD.printNumI(brightness1, 125, (i*30)+35);
myGLCD.print("%", 150, (i*30)+35);
}
}else if (RTC.hour == stopTime){
analogWrite(led12, LOW);
}
}
void mainScreen(){
background();
myGLCD.drawRoundRect(6, 27, 184, 204);
myGLCD.setColor(65, 105, 255);
myGLCD.setBackColor(65, 105, 255);
myGLCD.fillRoundRect(10, 30, 95, 50);
myGLCD.fillRoundRect(98, 30, 180, 50);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect(10, 30, 95, 50);
myGLCD.print("RB", 15, 35, 1);
//screenno = 2;
}
//--------------------------------------------------Main Setup
void setup(){
Serial.begin(9600);
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
RTC.getTime();
pinMode(led12, OUTPUT);
mainScreen();
}
//--------------------------------------------------Main Loop
void loop(){
unsigned long currentMillis = millis();
if (currentMillis - prevMillis5 > interval){
prevMillis5 = currentMillis;
RTC.getTime();
changeFont(SMALL, 225, 255, 255, 255, 0, 0);
printTime(RTC.hour, RTC.minute, 5, 5);
printDate(RIGHT, 5);
ledBright();
}
}