iwright13:
Hey all,
I am new to Arduino, I am using it to control a light box I am building, it has two individual sets of LEDs that will change color throughout the course of the day. But, I have two questions.
Can I use an RTC like the DS1307 to trigger alarms throughout the day?
AND
Can I run two different voids? EX- the first void changing the LEDs color and the other void causing a single red LED to blink.
Any help would be appreciated.
"Two different voids"? You mean 2 different functions? The Arduino had a single loop() function that gets called repeatedly as long as the device is running. That loop function can do whatever you want. You can write other functions, e.g.
change_RGB_LED_Color()
and
blink_red_LED()
Your loop function would just call both of those functions on each pass.
If you want to have the RGB LED changing colors and the red LED blinking concurrently, you can't use delay() for your timing, since delay makes your whole program freeze. (So while you were using delay() to manage the RGB LED, the code to run the red LED would be frozen.)
You should take a look at the sample program blinkWithoutDelay in the "digital" group of starting programs build into the IDE. That application shows how to keep track of the amount of time that has passed and trigger changes based on the result of the millis() function. You'll need to use that method.
Your code might look like this:
long color_change_interval;
long blink_interval;
long next_color_change;
long next_blink;
int LED_pin;
bool pin_state;
//--------------------
setup()
{
pin_state = 0 ;
LED_pin = 2; //Whatever pin you have hooked up to the red LED
pinMode(LED_pin, OUTPUT);
ditialWrite(LED_pin, pin_state);
color_change_interval = 50;
blink_interval = 250;
next_color_change = millis() + color_change_interval;
next_blink = millis() + blink_interval;
}
//--------------------
loop()
{
change_RGB_LED_Color();
blink_red_LED();
}
//--------------------
void change_RGB_LED_Color();
{
if (millis() >= next_color_change)
{
//Put your code to adjust the RGB LED color here.
next_color_change = millis() + color_change_interval;
}
}
//--------------------
void blink_red_LED();
{
if (millis() >= next_blink)
{
pin_state = !pin_state;
digitalWrite(LED_pin, pin_state);
next_blink = millis() + blink_interval;
}
}
As far as using the RTC, sure, you can use that. It looks like it uses I2C serial interface. However, it does not appear to have an interrupt line for alarms. You would have to write code, again called by your loop function, that gets the time from the RTC and checks to see if the current time is greater than or equal to the alarm time you're waiting for. (You don't want to only look for an exact match because if you miss it by a small amount then your alarm never fires.)