hey
I am struggling with some code. I don't want the code written exactly for me, more i want to understand in idiot terms!
here is my code
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F // Define I2C Address where the SainSmart LCD is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
int hour;
int minute;
int second;
int month;
int day_of_week;
int day;
int year;
char* dow[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
int gLEDpin = 11;
int bLEDpin = 10;
int rLEDpin = 6;
////////////////////////////////////////////////////////////
int sunRiseHour = 9; // sun rise start
int sunRiseMin = 0;
int dayHour = 10; // daylight starts
int dayMin = 0;
int sunSetHour = 16; // sun set start
int sunSetMin = 0;
int twilightHour = 16; //twilight start
int twilightMin = 0;
int twilightSetHour = 20; // twilight set
int twilightSetMin = 0;
int lightOffHour = 21; // night
int lightOffMin = 1;
///////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600);
Wire.begin();
lcd.begin (20,4);
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
digitalWrite(gLEDpin, LOW);
digitalWrite(bLEDpin, LOW);
digitalWrite(rLEDpin, LOW);
}
void loop()
{
// Below required to reset the register address to 0.
Wire.beginTransmission(104); // transmit to device #104, the ds 1307
Wire.write(0x00);
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(104, 7); // request 7 bytes from slave ds1307, we'll assume it'll send them all even though it doesn't have to
second = Wire.read();
minute = Wire.read();
hour = Wire.read();
day_of_week=Wire.read();
day = Wire.read();
month = Wire.read();
year = Wire.read();
// Convert all the BCD values that might have "tens" to decimal.
// Most arduino folks do this w/shifts but this just looks easier to me.
hour=hour/16 * 10 + hour % 16;
minute=minute/16 * 10 + minute % 16;
second=second/16 * 10 + second % 16;
day=day/16 * 10 + day % 16;
month=month/16 * 10 + month % 16;
year=year/16 * 10 + year % 16;
Serial.print(hour);
Serial.print(":");
if (minute < 10) { Serial.print("0"); }
Serial.print(minute);
Serial.print(":");
if (hour < 12) { Serial.print("am"); }
if (hour > 12) { Serial.print("pm"); }
Serial.print(" ");
Serial.print(dow[day_of_week-1]); // array is 0-6, but the dow register holds 1-7, so subtract 1.
Serial.print(" ");
Serial.print(month);
Serial.print("/");
Serial.print(day);
Serial.print("/");
Serial.print(year);
Serial.print("\n");
lcd.home ();
if (hour <= 9) {lcd.print("0"); }
if (hour > 12 && hour < 22) {lcd.print("0"); }
if (hour <= 12) {lcd.print(hour); }
if (hour > 12) { lcd.print(hour - 12); }
lcd.print(":");
if (minute < 10) { lcd.print("0"); }
lcd.print(minute);
if (hour < 12) { lcd.print("am"); }
if (hour >= 12) { lcd.print("pm"); }
lcd.setCursor (8,0);
lcd.print(dow[day_of_week-1]); // array is 0-6, but the dow register holds 1-7, so subtract 1.
lcd.setCursor (12,0);
lcd.print(day);
lcd.print("/");
if (month < 10) { lcd.print("0"); }
lcd.print(month);
lcd.print("/");
lcd.print(year);
if ((hour >= sunRiseHour) && (hour < dayHour) && (minute >= sunRiseMin) && (minute < dayMin))
{sunRise;}
if ((hour >= dayHour) && (hour < sunSetHour) && (minute >= dayMin) && (minute < sunSetMin))
{dayLight;}
if ((hour >= sunSetHour) && (hour < twilightHour) && (minute >= sunSetMin) && (minute < twilightMin))
{sunSet;}
if ((hour >= twilightHour) && (hour < twilightSetHour) && (minute >= twilightMin) && (minute < twilightSetMin))
{twilight;}
if ((hour >= twilightSetHour) && (hour < lightOffHour) && (minute >= twilightSetMin) && (minute < lightOffMin))
{twilightSet;}
if ((hour >= lightOffHour) && (hour < sunRiseHour) && (minute >= lightOffMin) && (minute < sunRiseMin))
{night;}
delay(250);
}
void sunRise()
{
}
void dayLight()
{
digitalWrite(gLEDpin, HIGH);
digitalWrite(bLEDpin, HIGH);
digitalWrite(rLEDpin, HIGH);
}
void sunSet()
{
}
void twilight()
{
digitalWrite(bLEDpin, HIGH);
digitalWrite(rLEDpin, LOW);
digitalWrite(gLEDpin, LOW);
}
void twilightSet(){}
void night()
{
digitalWrite(bLEDpin, LOW);
digitalWrite(rLEDpin, LOW);
digitalWrite(gLEDpin, LOW);
}
basically i want to all LEDs to begin fading on at a time set and go from zero to 255 over an hour (sunRise. then all lights to stay on (dayLight). Then at a time, begin to fade over an hour from 255 to zero except bLEDpin, this needs to stay on (sunSet). then the bLEDpin to stay on until a time (twilight, then fade off (twilightSet). and then all off (night)
i have got dayLight, twilight and night sorted. but cant seem to get the fading sorted. I cant use delays as there will be other functions, such as button push, that will be added later, so i dont want the program to stop at a delay. i cant seem to see how to write the code for millis. Or can it be mapped to 60 mins?
i know my code is not efficient, but i will improve on this as my experience grows. I am still a newbie!
Kr
Craig