How to save the last value of a for-loop as input for its following for-loop?

Hi there,

I have the following code:

... 
for (long X = 0 ; X <= 200; X += 1.0) {

float c = sqrt(X * X + pow((h - Y), 2)); 
float angle = degrees(atan2(X, (h - Y)));
ThighStart = ThighStart - angle;
}
for (long X = 200 ; X >= 0; X -= 1.0){
float c = sqrt(X * X + pow(h , 2));
float angle = degrees(atan2(X, h));
ThighStart = ThighStart + angle
}
...

In the second loop for-function, where the value is decreasing from 200 to 0, I want the last value of ,, ThighStart" at X = 200 in the first loop for-function as its input (Start value). How do I do that?

Thanks in advance :slight_smile:

I don't understand. Have you printed the value of ThingStart between the two "for" loops? Why do you think it changes between loops?

Not sure what you are asking. ThighStart will be at the value it was for the last iteration of the first for loop when you enter the second for loop. BTW, for is not a "function".

A couple of comments:

  1. X in both loops is a long type. You could use an int type, however. Either way you should be incrementing by 1 not 1.0 since it is an integer.
  2. You realize both for loops are executing 201 iterations, not 200?

Thanks for the quick response,
I am trying to realize a semi-circle movement where X goes from 0 to 200. So 201 iterations are intended.

In the first for-loop for X=200, ThighStart = 136.83 but in the second for-loop when X=200, ThighStart= 170 instead of 136.83.

The values (after printing them out) for both loops are different, that's why I am asking

So, save the value at 200 in a different float and use that to reset ThinkStart before beginning the second loop.

You mean by setting the Start value directly with the wished value?

Still does not work. Different values are printed.

I am saying that in the first for loop, test the value of "X" for being 199, the 200th pass through the loop. When equal to 199, save the value of ThingStart in a new float.
Then just before beginning the second "FOR", move the saved value back into ThingStart. That gives you the value from the 200 pass through the first "for" loop.

Could you please give me an example of what it should look like?

float temp;

for (long X = 0 ; X <= 200; X++) {
  float c = sqrt(X * X + pow((h - Y), 2)); 
  float angle = degrees(atan2(X, (h - Y)));
  ThighStart = ThighStart - angle;
  if (X == 199) temp = ThighStart;
}

ThighStart = temp;
for (long X = 200 ; X >= 0; X--){
  float c = sqrt(X * X + pow(h , 2));
  float angle = degrees(atan2(X, h));
  ThighStart = ThighStart + angle
}

Sheesh. The oft-seen format for a for loop:

Creates a NEW “X” just for the duration of the loop.
If you want to preserve the value: don’t DO that.

long X;
for (X = 0 ; X <= 200; X += 1.0) {
3 Likes

Without the complete code it is hard to follow where your problem is ...

Would you mind (at least) to post the code with the startvalues of ThighStart, Y and h as well as where you have put the Serial.print()s of ThighStart (in which lines)?

That may help ... :wink:

And another benefit

long X = 0;
for (; X <= 200; X++) {
  ...
  ...
}
X--;
for (; X >=0; X--) {
  ...
  ...
}
1 Like

Though you and @westfw are right regarding the variable X, it looks as if the TO is not concerned about X rather than the variable "ThighStart" ...

However, as long as we do not have the complete information about the algorithms the TO posted, it is only guess work :wink:

If you read post # 4 in this thread the intention of the TO might become more clear (unless this was a misleading phrase):

I suspect that it may depend on where the println() was introduced:

for (long X = 200 ; X >= 0; X -= 1.0){
// If printed here: ThighStart should be like before
if (X == 200) Serial.println(ThighStart);
float c = sqrt(X * X + pow(h , 2));
float angle = degrees(atan2(X, h));
ThighStart = ThighStart + angle;
// If printed here: ThighStart has been increased by angle
if (X == 200) Serial.println(ThighStart);
}

As long as the TO does not give further information - as stated before by me - we can only guess ...

And in the posted part

  • a colon is missing at the end after "ThighStart + angle"
  • the variable c is only calculated but never used
  • the value of variable h is unknown to us
  • the value of variable Y is unknown to us (which is not required if we believe that the value of ThighStart is 136.83 after the first for loop)

What does TO mean?

int i = 0;

/*
First loop, can also be done as a while loop
since we have initialised i to zero above.
See the while loop version below
*/
for(i = 0; i < 10; i++)
{
    //Loop exits when i becomes 10
    //Note that i++ is "post incremented"
    //i is first used and then increased by 1
}

//At this point i equals 10

int j = i; // j now has a value of 10

//Second loop
while (j < 20)
{
    //Loop exits when j becomes 20
    j++;
}

Thread Owner, the one who started the actual thread ... In this case it is you ... :wink:

It would be helpful if you could post some more of your code, in minimum an excerpt that can be compiled and includes all relevant variables and algorithms. Unfortunately (see my post above) the part which you published in the first post does not fulfill these minimum requirements.

Do you think you could create a short sketch that includes all necessary data and algorithms so that one can retrace what's going on?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.