Pages: [1]   Go Down
Author Topic: Using a global variable as the tested condition in a for loop  (Read 319 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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...

Code:
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 Offline
Full Member
***
Karma: 5
Posts: 113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post your code?

[edit]
deleted rest of reply, cus I was wro, wroooo, wrooooooo, damnit, not right! smiley-grin
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 Offline
Brattain Member
*****
Karma: 291
Posts: 25851
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see a problem, but I don't see any code either.

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Currently I have the following;

Code:
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...

Code:
 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 Offline
Brattain Member
*****
Karma: 291
Posts: 25851
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 5
Posts: 113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks AWOL smiley
Logged

For whom does the clock pulse? It pulses for you!

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 291
Posts: 25851
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Online Online
Faraday Member
**
Karma: 109
Posts: 4083
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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*  smiley-grin
Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are pronouncing the "J" wrong, it's pronounced "Yoke"  smiley-wink
Logged

For whom does the clock pulse? It pulses for you!

Pages: [1]   Go Up
Jump to: