Fun With LEDs- Programming questions

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.

Can I use an RTC like the DS1307 to trigger alarms throughout the day?

Sure.

You can have functions inside of the void loop() function that do what you want. The void in front of the function name means that the function returns nothing. The empty () after a function name is the same as (void) and that means that the function takes no parameters.

Do you have an example? It's all quite confusing to wrap my head around.

Arduino Playground - Function Tutorial To get started on functions. Overview | DS1307 Real Time Clock Breakout Board Kit | Adafruit Learning System Here is a page on the RTC. They show and explain a RTC library and code.

Thanks so much!!!

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.)

Here is what I am trying to do.

RGB1 and RGB2 are LED strips with RGB. RED LED is just a single red LED..

6:00 AM - Turn RGB1 and RGB2 to colors x1 and x2
9:30 AM- Turn RGB1 and RGB2 to colors y1 and y2
7:00 PM- Turn RGB1 and RGB2 to colors z1 and z2
10:00PM- Turn RGB1 and RGB2 to colors a1 and a2
-start the RED LED to start blinking.
at 6 AM repeat