Usable conditions in the 'for' statement.

Hi all :slight_smile: Glad to meet you again.

Just a quick answer regarding the ‘for’ statement like this:

for (initialization; condition; increment)

In the ‘condition’ part, is it polically correct :wink: to use an ‘if’ type condition (a multiple one) like this:

for(initialization; var1 == 255 || var1 == 0; increment).

Should it work?

I already seen a double ‘for’ statement is OK, but just to tense the line.

Many thanks for your patience with this unquiet newbie.

Engadin.

You can put any valid condition there.

for(initialization; var1 == 255 || var1 == 0; increment).

Should it work?

Why not take a minute and try it ?

Engadin:
In the 'condition' part, is it polically [sic] correct to use an 'if' type condition

Not sure what a politically in/correct condition is, tbh. Do you mean (assuming it works in the first place) is it good practice, is it elegant?

Don't forget that the condition could be the return value of a non-void function.

AWOL:
You can put any valid condition there.

Thanks AWOL. That's what I was guessing this morning. BTW, it didn't work first time I used it. I'll try it back as I get back home.

UKHeliBob:
Why not take a minute and try it ?

Thanks UKHeliBoB, I did but didn’t want to admit it didn’t work when it looks so ‘working-like’.

But in my actual newbie status, almost anything can be a total fail, so my ‘for’ code can be ok but not the next line.

Just trying to streamline this tiny block of code.

I'm very keen to hear what you're trying to do.

kenwood120s:
Not sure what a politically in/correct condition is, tbh. Do you mean (assuming it works in the first place) is it good practice, is it elegant?

Thanks kenwood120s. You nailed it: my aim is to downsize the code for an LED fading up and down with the use of a single 'for' statement. It looks elegant, as you mention, doesn't it?

Engadin.

AWOL:
Don't forget that the condition could be the return value of a non-void function.

OMG, your comment goes far beyond my tiny comprehension capacity ... so far, I hope. It makes me feel a whole universe of knowledge is ahead me. A shudder is shacking my inner parts in this very moment. I feel somehow alone in my single palm tree island, just somewhere in an inmense ocean of code learning.

No prob, I have plenty of coffee.

Engadin:
the code for an LED fading up and down with the use of a single 'for' statement.

But changing the end point to 255 OR 0 won't achieve that; the start point and sign of the increment play a part too.

It needs to go from 0 to 255 in +1 increments, or from 255 to 0 in -1 increments; you can't just OR the end points.

It looks elegant

Maybe don't use a for loop at all.

Remember, the loop() function is your friend.

kenwood120s:
But changing the end point to 255 OR 0 won’t achieve that; the start point and sign of the increment play a part too.

It needs to go from 0 to 255 in +1 increments, or from 255 to 0 in -1 increments; you can’t just OR the end points.

Hi kenwood120s, yes, when any of the two limits 255 or 0 are reached, I multiply by -1 the incremental variable (two values, 1 and -1) I add to the variable for the led brightness in every for loop .

Please let me show you the code I am testing and that doesn’t work as I wish: now the led just blinks and the serial monitor only shows: 0, -1, -1, 1, 0, -1, -1, 1, … meaning (I guess) it doesn’t follow the for loop at all, not even once (push the reset button and starts the list with again with 0, -1, …). And the sequence of ones and zeroes is a mistery to me.

As usual, I fear IT will most probably be a ‘how the hell didn’t i see it’ issue. And my battered little ego is ready for a punch :wink: … again.

//Variable for increase or decrease the Led light.
int fadeSign = 1;

//Variable the value of the Led light.
int ledInt;

//PWM pin for the fading Led.
const int ledPin = 3;

//-------------------------------------------------------
void setup(){

Serial.begin(9600);

pinMode(ledPin, OUTPUT);

}

//-------------------------------------------------------
void loop(){

  for (int ledInt = 1; ledInt > 0 && ledInt < 255; ledInt = ledInt + fadeSign);
    
    {analogWrite (ledPin, ledInt);
  
    Serial.println(ledInt);
    
    delay(300);}

//If code leaves the for loop it means ledInt value has gone 255 or 0.
//And it's time to change the sense of gradient value fadeSign's variation,
//whichever it is now: if it was 1, let's change to -1 and viceversa.
  
  fadeSign = fadeSign * (-1);

  ledInt = ledInt + fadeSign;

  analogWrite (ledPin, ledInt);
  
  Serial.println(fadeSign);

  delay(300);
}

The ledInt variable in the for loop is not the same ledInt variable that you declare as global. Could that be a problem ?

There's nothing stopping you from putting a function call in the condition part of the for loop:

for(int i=0; someCondition(); i++) {
  activatePump(i);
}

bool someCondition() {
  return checkPlantConditionGood();
}

Regards

Mark

UKHeliBob:
The ledInt variable in the for loop is not the same ledInt variable that you declare as global. Could that be a problem ?

Hi UKHeliBob, thanks.

I have moved it from global to just after the void loop() header and nothing improves.

And just newbie curiosity: why it is not the same variable? If it's global it should be, shouldn't it?

I am using 1.6.10 IDE version.

Engadin:
Hi UKHeliBob, thanks.

I have moved it from global to just after the void loop() header and nothing improves.

And just newbie curiosity: why it is not the same variable? If it's global it should be, shouldn't it?

I am using 1.6.10 IDE version.

No, then you have two variables with the same name, one global and one local.

UKHeliBob:
Maybe don't use a for loop at all.

Remember, the loop() function is your friend.

LOL. I am 5 seconds away from do following your advice, mate.

I have already crashed my fronthead too many times against the wall of this for loop nightmare. XD.

for (int ledInt = 1; ledInt > 0 && ledInt < 255; ledInt = ledInt + fadeSign);

Remove that semicolon at the end of this line and see how it goes.

Jacques

evanmars:
No, then you have two variables with the same name, one global and one local.

OK evanmars, got it. I have declared it and fadeSign in the void loop scoop this way:

void loop(){

int ledInt=1;

int fadeSign=1;

.
.
.
.

Very frustating indeed. But frustation is a painful part of the learning process. It really f****. though.