Y variable known when x variable known

I don't believe now that the for loop would work. I have a downloaded value of x and y at the same time. when x is a certain value y is a certain value. I want that value of y to do something. x is already doing something. they are both doing something at the same time.
how do you express this in the coding? all of the suggested answers are assuming I want to save the values of x and y. I don't need to. the values are used just once.
or do you need coding at all? when the values of x and y show up in the main program just grab the y value to do something with it.

No, no you don't. Just grab the y value to do something with it.

a7

This entire discussion is based on the idea that you want to do something specific with y when x is a certain value, and what you do with y changes based on the value of x.

If you always want to do the same thing with y, regardless of the value of x, then the entire discussion is meaningless.

How are they downloaded?

What something do they do?

Perhaps stepping back a bit and considering the rest of the program in terms of this might help:

Since the details of the reading and the using aren't presented in the thread, this assumes that you already have those handled adequately, and you are focused on how to do the looping. loop() will handle that:

int x,y;

void setup(){
}

void loop() {
    readXnY();
    useXnY();
}

void readXnY(){
  //
}

void useXnY(){
  y; 
  x; 
}


where useXnY(){...} holds what you've shown in the for loop.

That untested snippet should does run.

I think it's time to end this discussion.
I didn't want to say this, but someone has to say... it looks like the author has mental or cognitive problems.

I would ne interested to see how these values are "downloaded" and to know where they come from but you seem reluctant to give any information so providing help is difficult

Post #65. Last phrase.

the values of x and y are derived from within the sketch using very complicated maths and also a real time clock. one equation is providing the x value. a separated equation is providing the y value.
the x and y values have to be synchronised to provide actions at the same time. this is where I need a suggestion for code to use. my whole sketch does work in one dimension by just using x. it will also work in one dimension by just using y. I need it to work in 2 dimensions at the same time. I need a suggested code for doing this.
the sketch provides these 2 values at the same time. I don't need to save these values in an array. they are just used one time. the equations will always be supplying new values.

I'm not seeing any code that does either x or y in one dimension, and no idea what it would mean for the two to be used two dimensionally.

?

a7

I think you might just have some of the coding I need. I have now discarded the idea of using a 'for' loop. refer to my last reply to UKBOB.
from your code I see I may only need this;
void loop(){
useXnY(){
y;
x;
}
}
never seen that type of code before. is it in the Arduino help reference? it allows for two dimensional use . I can't see that I would need the readXnY as these values are already available from the 2 equations. refer to latest UKBOB reply from me.
Should I set it out the way you have just leaving out the read parts? is that 'void' bit taken into account even though it is out of the main 'void loop'.

Your post #57.

Do you mean something like this ?

int x;
int y;

void setup()
{
    Serial.begin(115200);
}

void loop()
{
    //some complicated code to generate values for x and y every now and then
    delay(random(1000, 5000));
    x = random(100);
    y = random(100);
    useXnY();
}

//a function to process the x and y values
void useXnY()
{
    char buffer[30];
    sprintf(buffer, "x is %d and y is %d", x, y);
    Serial.println(buffer);
}

Is the part that you have never seen before "functions (link)"?

The "void" bit only means that the function doesn't return a specific value. The readXnY() function was a placeholder for doing this:

or later:

You might follow @robin2's guidelines and write your complicated functions as c++ functions:

int x,y;

void setup(){
}

void loop() {
    calculateXnY();
    useXnY();
}

void calculateXnY(){
  uint32_t myTime = millis(); // fake an RTC
  x = complicatedCalculationForX(myTime);
  y = complicatedCalculationForY(myTime);
}

void useXnY(){ // HT https://forum.arduino.cc/t/y-variable-known-when-x-variable-known/1295412/72
    char buffer[30];
    sprintf(buffer, "x is %d and y is %d", x, y);
    Serial.println(buffer);
}

int complicatedCalculationForX(uint32_t inputT){
    int retVal = (inputT + random(10)) % 100;
    return retVal;
}

int complicatedCalculationForY(uint32_t inputT){
    int retVal = random(100) ;
    return retVal;
}

If you have multi-dimensional data you want to use in a multidimensional way, you can just use it multidimensionally. For example, this simulation generates a complicated 6-D multidimensional vector and uses the result to move 6 steppers in coordinated 6-dimensional motion:

That program breaks the problem down into a few functions, and does the work.

Are you doing something like a clock? with different sort of actions at different times?

Maybe switch would still work:

// mod from https://forum.arduino.cc/t/y-variable-known-when-x-variable-known/1295412/72
int x;
int y;

void setup()
{
    Serial.begin(115200);
}

void loop()
{
    //some complicated code to generate values for x and y every now and then
    delay(random(1000, 5000));
    x = random(100);
    y = random(100);
    useXnY();
}

//a function to process the x and y values
void useXnY()
{
    char buffer[30];
    switch(x){
    case 40:
       sprintf(buffer, "x is forty and y is %d", y);
       break;
    case 50:
       sprintf(buffer, "x is fifty and y is %d", y);
       break;
    default:
       sprintf(buffer, "x is %d and y is %d", x, y);
    }
    Serial.println(buffer);
}

thanks for the code. I think that should do it. answering your questions. the values of x and y are gotten from two very complicated equations in the main part of the sketch. the maths in Arduino were up to handling this without the maths library. the x, y values control servo motors.
a couple of questions on using the code. as the values for x and y are already known from the equations, is 'read xny' really necessary? I am already moving the servo motor with a 'map' function for x.
in the 'useXnY' statement in the curly brackets I will use the map function for x,y to move the servo motor. is that correct procedure? I will include the statement in the main void loop rather than a subroutine as you have done.
do you have a source where I can find more about using readXnY();
useXnY(); ? I have never seen code like this in Arduino before.

No. I made up that function name and insides to stand in for the complicated stuff that you said you already know how to “download” because we do not have a clue about whatever makes x & y.

Did you read this:

And especially the tutorial at

It does an excellent job of explaining how to build an easy to understand and maintain program by using functions like readXnY() and useXnY() to organize and communicate a thought process behind the programming.

I’m glad you see a way forward because it still isn’t clear to the rest of the world what you need.

no Bob. never seen this code before useXnY(); and readXnY. I am looking for the source of this code and how it is normally used so that I can get it right in my coding. it is not in the help section and reference in Arduino.

Very interesting, and it took only 75 posts to arrive at this nugget of information.

Please post those equations!

Any bets on object tracking with pixycam ?

https://woodgears.ca/lego/shooter_aimer.html