UK
Offline
Sr. Member
Karma: 1
Posts: 313
|
 |
« on: February 02, 2013, 05:01:56 pm » |
Evening all, 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... for(x = 0; measurement_val < calibration_val; x++); { code } 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. Many thanks in advance!
|
|
|
|
« Last Edit: February 02, 2013, 05:04:17 pm by jtw11 »
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 5
Posts: 112
|
 |
« Reply #1 on: February 02, 2013, 05:07:08 pm » |
Can you post your code? [edit] deleted rest of reply, cus I was wro, wroooo, wrooooooo, damnit, not right!  Thanks AWOL [/edit]
|
|
|
|
« Last Edit: February 02, 2013, 05:19:11 pm by 0AlphaOmega »
|
Logged
|
For whom does the clock pulse? It pulses for you!
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #2 on: February 02, 2013, 05:08:57 pm » |
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.
|
|
|
|
« Last Edit: February 02, 2013, 05:16:53 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
UK
Offline
Sr. Member
Karma: 1
Posts: 313
|
 |
« Reply #3 on: February 02, 2013, 05:10:59 pm » |
Currently I have the following; 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... for (heater_duty = 145; UR_m > UR_c; heater_duty++) { analogWrite(heater_pin, heater_duty); delay(280); UR_m = analogRead(UR_pin); } It seems it should work...
|
|
|
|
« Last Edit: February 02, 2013, 05:12:57 pm by jtw11 »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: February 02, 2013, 05:18:33 pm » |
I don't like delays, but I don't see why it should not work, for some definitions of "work"
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Full Member
Karma: 5
Posts: 112
|
 |
« Reply #5 on: February 02, 2013, 05:20:39 pm » |
Thanks AWOL 
|
|
|
|
|
Logged
|
For whom does the clock pulse? It pulses for you!
|
|
|
|
UK
Offline
Sr. Member
Karma: 1
Posts: 313
|
 |
« Reply #6 on: February 02, 2013, 05:21:27 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: February 02, 2013, 05:26:32 pm » |
Well, if you remove the delay, you won't be using a while or for loop, so the construct is pretty redundant.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
UK
Offline
Sr. Member
Karma: 1
Posts: 313
|
 |
« Reply #8 on: February 02, 2013, 05:32:09 pm » |
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...
|
|
|
|
« Last Edit: February 02, 2013, 05:41:44 pm by jtw11 »
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 49
Posts: 1435
May all of your blinks be without delay
|
 |
« Reply #9 on: February 02, 2013, 05:42:08 pm » |
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 !)
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Sr. Member
Karma: 1
Posts: 313
|
 |
« Reply #10 on: February 02, 2013, 05:50:14 pm » |
Thinks - can you use goto to exit from a loop (JOKE !)
*runs off to check what a goto is, and how it forms the basis of a joke* 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 5
Posts: 112
|
 |
« Reply #11 on: February 02, 2013, 06:12:21 pm » |
You are pronouncing the "J" wrong, it's pronounced "Yoke" 
|
|
|
|
|
Logged
|
For whom does the clock pulse? It pulses for you!
|
|
|
|
|