Using If to trigger different functions

Hey all, so I am having trouble figuring out why this won't work. Can anyone take a look at my code and help me?

[code]
  // I used the time_t t =now() to declare t
  
 if (t >= hour(6) minute(00) second(00)) 
  void sunrise_cycle();
 if (t >= hour(9) minute(30) second(00)) 
  void daytime_cycle();
  if (t >= hour(19) minute(00) second(00)) 
  void sunset_cycle();
   if (t >= hour(9) minute(15) second(00)) 
  void night_time_cycle();

[/code]

It won't work because it's not valid C code.

Neither the text inside the "(" and ")" nor the statements after each if are valid.

Every single line is wrong. Your if statements are so messed up that I'm not sure what you are trying to do.

I guess you're trying to say (in pseudo-code)

if the time is >= 06:00
  call the function sunrise_cycle();
if the time is >= 9:30
  call the function daytime_cycle();
if the time is >= 19:00
  call the function sunset_cycle();
if the time is >= 09:25
  call the function night_time_cycle();

Note that if the time is >= 06:00, it will also be greater than all your other times, so you'll need to change your if statements to a series of if/else if statements, like this:

if ()
  //do something
else if ()
  //do something
else if ()
  //do something

I'm not familiar with the Arduino time library so it's a little hard to tell you what to do to fix your code, especially since it isn't clear what you want to do.

One big thing:

You don't put the word void in front of calls to functions. The word void is used when you define a function to indicate that the function does not return a result.

It's clear from what you posted that you don't know the first thing about the C language. I suggest you get a book on C and read it, doing the tutorials, before you start trying to write original programs.

To use a cooking analogy, you are trying to make a soufflé when you don't know how to heat up a can of soup in a microwave.

iwright13:
Hey all, so I am having trouble figuring out why this won't work. Can anyone take a look at my code and help me?

[code]

// I used the time_t t =now() to declare t
  
 if (t >= hour(6) minute(00) second(00))
  void sunrise_cycle();
 if (t >= hour(9) minute(30) second(00))
  void daytime_cycle();
  if (t >= hour(19) minute(00) second(00))
  void sunset_cycle();
   if (t >= hour(9) minute(15) second(00))
  void night_time_cycle();


[/code]

ok then... what to do about it...

  // I used the time_t t =now() to declare t
  
 if (t >= hour(6) minute(00) second(00)) 
  void sunrise_cycle();
 if (t >= hour(9) minute(30) second(00)) 
  void daytime_cycle();
  if (t >= hour(19) minute(00) second(00)) 
  void sunset_cycle();
   if (t >= hour(9) minute(15) second(00)) 
  void night_time_cycle();

you are not that far off...

try something like this, but keep it simple first... program one time-based event.

time_t t = now();  // right out of the Time.h library, yes
  //
  if (hour(t) == 6 && minute(t) == 0 && second(t) == 0)  // this will give you several thousand opportunities to capture the moment if you are not using blocking code
  {
    if (!active)// some flag to make sure it only runs your function once
    {
      sunrise_cycle();
    }
  }
  else if (hour(t) = 9 &&  minute(t) == 0 && second(t) == 0)
  {
    if (!active)// some flag to make sure it only runs your function once
    {
      sunset_cycle();
    }
  }
  // well you are starting to get it...
  //
  // more stuff here
  //
}// end of loop()

void sunrise_cycle();
{
  // your function stuff here
}

void sunset_cycle();
{
  // your other function stuff here
}

okay, so I wrote all of my else if statements,

now how do I make sure they become active when the device is plugged in?

[code]
void loop() {

  if (hour() >= 6 && minute() >= 00) {
   sunrise_cycle();

  }
   else if (hour() >= 9 && minute() >= 30){
     daytime_cycle();

   }
  else if (hour() >= 19 && minute() >= 15){
    sunset_cycle();

  }
  else if (hour() >= 21 && minute() >= 30){
     night_time_cycle();

   }
  }

[/code]

iwright13:
now how do I make sure they become active when the device is plugged in?

The device being the Arduino?- well as soon as you upload the sketch from the IDE, the sketch runs. Then if you power down, at next power up it starts running straightway.

void loop() {

  if (hour() >= 6 && minute() >= 00) {
   sunrise_cycle();

  }
   else if (hour() >= 9 && minute() >= 30){
     daytime_cycle();

   }
  else if (hour() >= 19 && minute() >= 15){
    sunset_cycle();

  }
  else if (hour() >= 21 && minute() >= 30){
     night_time_cycle();

   }
  }

Is it ok with the rest of your code, that the (eg) sunrise_cycle() starts again and again and again every time through loop() while the time is 6:00, ie 1000s of times a second for a whole minute? Not knowing what the sunrise_cycle() does, we can't say if that's a problem or not. If it set off a canon, it would be doing that a zillion times; if it's switching on a light that would be no problem probably, merely redundant.

edit: in fact with that structure and with the ">" signs, it's going to re-run those cycles a gazillion times.

You need to embrace BL's approach with the run-once-only-flags......

The idea is that each function is triggered at certain times. Each function is a set colors for the RGB LEDs

iwright13:
The idea is that each function is triggered at certain times. Each function is a set colors for the RGB LEDs

Yeah but your code as it stands, triggers the function a zillion times. Rememer loop() is zooming away 100k times a second. At 5:59 nothing happens. Next time thru loop() it sees a 6 so it fires the function. 1/00th of a second later it's back again, aha it's still 6 so it fires again. And so on. As I say, that might not be harmful: if it's just redundantly switching on an LED that's already on, no harm done. But if it was sending you a wake-up sms?- different story.

Your logic's sub-optimal in that sense.

It's even worse than that actually: 5 hours later, the sunrise test will still pass because of the >6... it's going to fire every 1/100th of a second all day, I think.

Do yourself a favour and go the flag route, much less hassle.

Better yet use the timer alarm library (I forget its correct name) which allows you set alarms very easily without having to code that stuff yourself. Where's the time coming from, btw- an RTC chip?

1/00th of a second later it's back again

Geez, I hope not.

Then the question is, how do I make sure that between 6 am and 9:29 am that the sunrise test turns on.

Then the question is, how do I make sure that between 6 am and 9:29 am that the sunrise test turns on.

You need a boolean flag:

bool beenThereDoneThat = false;

if(hour() == 6 && min() == 30)
{
   if(!beenThereDoneThat)
   {
      doTheSixThirtyThing();
      beenThereDoneThat = true;
   }
}

If the doTheSixThirtyThing() is non-blocking, then you need to set another flag to indicate that the doTheSixThirtyThing() function should be called. Set that to false initially, and to true in place of the doTheSixThirtyThing() call.

After the above block, test the added flag, and call doTheSixThirtyThing() if it's true.

You can add a similar test for 9:30, to stop doing the 6:30 thing.

You at this thread - its about the same kind of problem:
http://forum.arduino.cc/index.php?topic=268537.0

In particular note the need to detect when the time now is different
from the last time round loop() - that's when you should trigger actions,
not every single time round the loop.

The exact same issue applies to timed events using millis(), button presses,
etc etc - you detect a change to trigger a response, not respond to a steady
state condition.

If the reponse is idempotent, it doesn't matter, but when you need some
non-idempotent you have to pay attention to detecting transitions rather
than states.

Google "idempotent", its a great word.