I've just had a thought to improve a piece of code I've written. Currently, my for loop has the following structure, in that the same variable is the initialized value, the tested value, and the incremented value. Here's an example.
for(x = 0; x < 255; x++);
{
code
}
Instead, I'd like to make the tested variable a different variable to the initialized, and incremented value - so I'd end up with the following structure...
Is there any problems with doing this? The only reason I ask, is I've only ever used for loops where all three entries in the for loop are the same variable - and that seems to be how all examples are too.
I don't see a problem, but I don't see any code either.
X is a counter, and it is x that need to be compared
In some cases that may be true, but not in all. A for loop is just a special form of a while loop, and how long it iterates does not necessarily depend on the count value.
for (heater_duty = 145; heater_duty <= 255; heater_duty++)
{
analogWrite(heater_pin, heater_duty);
delay(280);
// sample probe temp, and break for loop if temp is reached before time out
UR_m = analogRead(UR_pin);
if (UR_m >= UR_c)
{
break;
}
}
However, I thought doing it the following way would be simpler...
AWOL:
I don't like delays, but I don't see why it should not work, for some definitions of "work"
I couldn't agree more about delays - this is just part of a piece of code used to test a hardware design before committing to the design in the form of a PCB, the 'proper' software won't have a delay in it.
If the delay was not there, would your comment of "for some definition of work" still stand true, or is the delay your only concern?
Ah, can one not use methods such as that used within the blinkwithoutdelay example, within a for or while loop?
Saying that, I can't quite work out how I'd do that - perhaps a while loop inside the for loop, so that until the timer exceeds its limit a boolean is set low - and while that boolean is low, the while loop just keeps looping.
Hmm, having written that - that of course gives exactly the same effect as delay... Rethink needed for the final software...
You can do what you like in a while loop but if the condition being tested is never true or you don't exit out of it explicitly, then it is going to block execution of other code anyway.
Thinks - can you use goto to exit from a loop (JOKE !)