I have a program that spits out the value of x and y at the same time. For instance, y may be 5 when x is 9. I can do this by an 'if' statement.
if (x==9){
z=y;}
This would work because when x is known, y is known. But here is the problem. You are going to need a lot of 'if' statements if you have a large number of values for x.
Can anyone suggest a simple coding for this? A map function would not do it because it implies a linear or some other relationship. A specific relation between x and y is not known.
Table lookup with an array?
Then you need only one if statement and a loop to run through the array.
How many values of x are there and what is the range of those values ?
Is there a way of calculating y when x is known?
Sorry, I see there is not.
show a complete list of from-to values and tell, for which microcontroller.
Yes, you need a lookup table.
This is just an array with all posible values of X in one column and the Y values in the second column.
If X values are correlative, for example from 0 to 99, then only one column is needed:
int lookupt[] = {
5, // x=0
8, // x=1
60, // x=2
85, // x=3
13, // x=4
...
123,// x=98
76 // x=99
}
...
x = 3;
y = lookupt[x]; // y -> 85
If X can be any number then you need a 2 dimensional array:
int lookupt[100][2] = {
{ 5, 7}, // x->5, y->7
{ 35, 120}, // x->35, y->120
{ 132, 14}, // x->132, y->14
...
}
To find Y, you loop through the array until lookupt[i][0] == X, then you have Y in lookupt[i][1].
You can do some optimizations to find X faster, if needed.
You haven't told us which processor board you're using. On one that supports the STL (eg, ARM, ESP), you could use std::map
.
Or, you could make an array of structs where each struct contains an X, Y value pair. Assuming you order the array by the X value, you could then do a binary search for the desired element.
And there's switch-case
:
switch(x){
case 0: y=0;break;
case 1..10: y=map(x,1,10,10,1);break;
case 11..80: y = sin(0.01*x); break;
case 81: y=10;break;
case 82: ++y;
case 83: ++y;
case 84: ++y;
case 85: ++y;
case 86: ++y;
case 87: ++y; break;
case 100..140: y=1023 ; break;
case 999:
case 9999:
case -1:
case 141..151: y=1024 ; break;
default: y=random(x);
...
}
I have not been specific enough in what I am trying to do. A main program is providing two variables x and y at the same time. y has no particular relationship to x and could be any number. But x is rising in a predictable way. I think I have a code which will give me the value of y when x has a specific value.
for (x=0; x< 100; x++){
z=y;}
if for example x is at a particular time 40, then at that particular time y will have a value which is saved in z. z can then be used for something else. Remember in my program no specific relationship can be shown to exist between x and y. You can't say, for example, y=x3 +x2. cubed and squared.
I don't see how both of those statements can be true
Why do you need code to give you the value of y, when the main program is already giving you both x and y?
Are you just wanting to set z to the value of y for specific values of x? If so, then a case statement would work. A case can be executed for multiple values of x, with other values of x doing whatever other action you need.
That sounds exactly like a Look Up Table would work. An array y[100]={…}. seems like the solution.
You also haven't specified which processor board you're using as was requested.
i think you saying that you need something that translates an x value to a y value and the relationship is more random than can be described in some formula.
is there a limited range of x values this needs to work for?
if all of above is correct, why not use @DaveX suggestion for a table, perhaps a large table unless the relationship repeats at some point?
or are you measuring y at each value of x?
why is copying y to z relavent? are there specific values of x where y is saved/copied?
just trying to understand what the problem is
@petercl14 We are seriously short of information with regard to your problem
You have not told us how many values of x there are and what the range of values is
You have said that there is no fixed relationship between the x and y values. You have also said that you have code which will give you the value of y for a given value of x but confusingly you have also said that the external system sends the values of x and y to the Arduino sketch. To confuse things further you mention a mysterious z value that you want to set equal to the y value
Can you see how confusing all of that is ?
Please can you clarify the requirements in a way that is consistent
You do not need 'z' if z == y.
False. The function using 'x' as an argument has one, and only one, 'y' output. That is the definition of a function. Your topic title even states this "y is known when x is known."
Would you CLEARLY state the desired input and output. From this, the process/function can be discovered.
you are still not specific.
currently you describe the signature of a function like
int mapping (int x, int y)
where the returned value is z.
now come up with your definition of what values of x and y should give what z.
that is correct. need to know the value of y when x is a specific value. I am not familiar at all with case statements. do you have a suggestion for a code using a case statement which will give me what I am after?
I think my suggested code may work. I have not tested it yet. In the main program x will be holding its value even as long as a minute. I am seeing x++ as a kind of permission in the code for x to be incremented by one. For instance from 40 to 41.
Usually that kind of 'for' statement goes at lightning speed. Can't be sure what will happen when x is held at a specific value for a long time.
Somebody has suggested why have z=y. If I just had 'y' it would have no meaning. You have to do something in the curly brackets.
In the case which maps y. Why show case at all? Why not just the map function. But this implies a relationship between x and y. And as stated there is no relationship.
The value of x and y in my main program appear at the same time. I have to know the value of y when x is a specific value. You would think this would be simple to obtain in some coding.
So far my suggested coding may come closest to achieve it. If you have a case that will do it please let me know.