# Delay inside 'void' that doesn't stop the loop

Hi. I am making a reef tank controller and trying the work out how to do the LED fades from morning to noon etc.

I have one theory but it involves using a delay but think that will mess things up. I was thinking of having a void section containing a delay so that it pulls the total fade time, dividing it by a certain number to give me say 12 delay values which total the total fade time, and then putting a while if loop under this void until the destination brightness is reached. Similar to this...

Pull start fade time, (0900 hrs)
Pull total fade time, (60 minutes)
Pull target brightness,
Pull current brightness,
Minus current brightness from target brightness, (difference)
60 minutes * 60000ms = 3600000ms (to give me the total millisecond delay)
Divide fade time by 12 = 300000ms (5minutes) delay increment
Brightness difference also divide by 12 (difference increment)

While statement
if 0900hrs is true and
brightness is lesser than target brightness
then OUTPUT = OUTPUT + (difference increment)
delay(delay increment)
Loop back around until target brightness is reached (should be over 12 increments)

Or similar.

The main question I am getting at is... is there any way that I can have this loop, with delay, or similar, within a VOID section that will not effect the rest of the programme and allow it to continue looping through as normal?

** I am a beginner and picking this up as I go along so please be patient with me **
I would post an example of my code but currently over 3000 lines long :-/

Many thanks,
Steve

then putting a while if loop under this void

?

``````while()
if (time equals 0900)
if (brightness < total brightness)
{
OUPUT = OUTPUT + difference increment
}
delay(delay increment)
``````

My thinking is was that it will continue looping until current brightness = target brightness. No?
I may be way off but as I said, I’m still very new to this.

You are clearly misusing the word “void”, and probably others, such that it is difficult for us to understand you.

``````void thisisafunction() { //this is a function named "thisisafunction", which returns type void (ie, it returns nothing)
doStuff(); //this code is inside the 'thisisafunction()' function
Serial.println(thisisalsoafunction(5));
}

int thisisalsoafunction(byte b) { //this is a function named "thisisalsoafunction", which returns type int (ie, it returns an integer datatype)
int retval=b<<7; //define an integer to return, and set it equal to b<<7 (ie, b*128)
return retval;
}
``````

That while loop… i don’t think it’ll do anything, because you left the test blank, which should be false, so it’ll never run… it certainly won’t do what you want.

I’m starting to wonder if you’re really asking for doing multiple things at once without delay - http://forum.arduino.cc/index.php?topic=223286.0 for more info - you need to keep track of the time, in that case)

You're probably right. I've just kind of mustered myself through this project and probably don't a large chunk of it wrong or the long way around but unfortunately my knowledge of programming is limited, and I cannot bring myself to spend a year or more learning C++ in depth just to make a fish timer. It's kind of catch 22.

I think I rewrote it wrong. I was planning on doing something like this...

``````void lunar_lowsun_fade()
{
totalfade = EEPROM.read(50);                        // Pull total fade time (60 minutes)
delayincrement = totalfade * 60000 /12;             // 3600000 ms / 12 = 5 minutes incr.
brightincrement = targetbright - currentbright;     // Britness increment, say 5 at a time

while(currentbright ! targetbright)
{
currentbright = currentbright + brightincrement  // adds 5 each time
}
delay(delayincrement)                            // 5 minute delays
}

void loop()
{
if (time.h = 9)              // 0900 hrs
if (time.m = 0)
{
lunar_lowsun_fade      // Start fade
}
}
``````
`````` if (time.h = 9)              // 0900 hrs
if (time.m = 0)
``````

The compiler won't complain, but it won't work.
Use ==

If you don't want to have to learn, check out the Reef Angel controller. It is all open source I believe.

Delta_G:
If you don't want to have to learn, check out the Reef Angel controller. It is all open source I believe.

The problem is that they all have their own way of doing things, use their own names and have different equipment that requires different things. Its tricky to marry it all up. I have tried.

I think you should have a look at it If that's what you think. The RA was designed to be totally customizable and DIYable.

I have forgotten what this expression does:

``````while(currentbright ! targetbright)
``````

Please explain.

econjack:
I have forgotten what this expression does:

``````while(currentbright ! targetbright)
``````

Please explain.

From my understanding it will keep running the script below it until the expression inside the parentheses becomes false, then it exits the loop and moves on.

So in mine above, the expression is saying that whilst 'current bright' is not equal to 'target bright', do whatever is below it, which is a script to increase 'currentbright'. When it gets equal to 'target bright', the expression is then false and therefore exits the loop.

Not equal would be written !=

I’m surprised if what is there will compile.

If you want to manage timing without using delay() look at how it is done in several things at a time.

The technique that it uses with millis() could also be used with an RTC.

...R

Delta_G:
Not equal would be written !=

I'm surprised if what is there will compile.

It doesn't.

``````  while(currentbright ! targetbright)
{
currentbright = currentbright + brightincrement  // adds 5 each time
}
delay(delayincrement)                            // 5 minute delays
}
``````

Out of 5 lines, three have errors in them.

Better at least nut out the syntax of C before starting to write 3000 lines. You are probably doing things that could be simplified.

Thanks for all of your wonderful condescending advice but as I have said, I haven't actually written this code yet, nor have I checked to see if it will compile, or return the results that I am after. The original question, which remains unanswered, is would the delay in the first void stop the rest of the program from running until it finishes. I suspect it would but wanted someone to just confirm or deny, not rip apart every other little thing otherwise I would have said 'can anyone please pick my work apart and make me feel and look like an idiot please'. I mean, what part of the forums would that question even go in.

Thank you Robin2 for probably the only practical advice in a reply. Thanks mate.

Delta_G:
Not equal would be written !=

I'm surprised if what is there will compile.

It probably won't compile. I haven't tried. I'm just playing about at the moment trying to work things out and the best way of doing it.

You missed an opportunity to point out that there are also no semicolons in that code because I'm an idiot.

``````int current_brightness;

void loop() {
int target_brightness = /* calculate what the brightness *should* be at this time */;

if(target_brightness != current_brightness) {
/* do whatever you have to do to change the brighness to match the target */ ;
current_brightness = target_brightness; // remember the fact that we have changed the brightness
}
}
``````

Actually AWOL's advice was good too, but kick back and criticise us if you like.

Yes, delay stops the program. What else would it do? Nothing? Sheesh.

That like asking if I turn the oven on, does it get hot.

I'm not the one criticising. As always, on any forum, people will ask silly questions which might not be silly to them, and as an experienced person on the forum (like I am on different forums offering help on other things) you have two choices of replies, "no mate thats wrong, try something like this, or do this".... or just be snappy, condescending, sarcastic and not actually offer any practical advice apart from belittle the other person. It happens on all forums and for someone like me who would help teach someone to tie their shoelaces if they needed it, find it a little frustrating that people on forums, who join to help people, seem to take pleasure in doing exactly the opposite.

"if I turn the oven on, does it get hot." maybe, depends if you mean turn on the plug to the mains in which case no, if you turn on just the fan, no, if you turn the temperature dial up, yes. See, many answers for yet a simple question.