search loop, i =i + 1/3, 1/2, 2/3, 1, i should be increased in steps, possible?

Hey there,

i think i have a simple Problem.
I need to fill an Array with the numbers increasing like this pattern:

0, 1/3, 1/2, 2/3, 1, 1 1/3, 1 1/2, ...etc

now i am searching for a loop function in which i can increase the i=i+? in self chosen steps.

something like that:

int c=0;
for(float i = 0, i < 10, i=i**+?**){
float steps

 = { };
c++;}


hope you can help
greetings

In such cases, the best approach is to iterate using integers, and then scale or convert the data type of the iterator as needed.

But your pattern needs some definition... please demonstrate an algorithm in pseudocode because it is not immediately obvious how your sequence is developed. Please use whole fractions like 3/2 instead of 1 1/2 because the computer will not be able to deal with those anyway.

0, 1/3, 1/2, 2/3... does not seem to have any rules to it. It's just arbitrarily increasing familiar fractions...

So you want to create x, x+1/3, x+1/2, x+2/3 and then go to x+1

May be your for loop could be about x and not the increments which are constants. For each x you use, it fills in 4 entries of your array at index 4x+0, 4x+1, 4x+2 and 4x+3

Should now be pretty obvious to write

thanks for your answer.
you're right.

i need an array filled with the following steps, starting at 0:
evenNumber, eN+1/3, eN+1/2, eN+2/3

should look like this:

step[0] = {0}; //first cycle start (even number = 0)
step[1] = {1/3}; //
step[2] = {1/2}; //
step[3] = {2/3}; // first cycle end
step[4] = {1}; //sec. cycle (even number = 1)
step[5] = {4/3};
step[6] = {3/2};
step[7] = {5/3}; //sec. cycle end
step[8] = {2};
step[9] = {7/3};
... etc.

i hope now the pattern is clear now ... ?

thanks

Yep - you have all you need from my answer :slight_smile:

Show us some code, we won’t write it for you

hey j-m-l,

it's still not so obvious to me, sorry.
my english isn't the best, hard for me to bring it to code.

could you give it another try?

thanks

OK it’s Xmas...

const byte maxsteps = 4*10;
float step[maxsteps];

for (int x=0; x<maxsteps/4; x++) {
   step[...] = ....;
   step[...] = ....;
   step[...] = ....;
   step[...] = ....;
}

You need to figure out the ... which should be easy from my earlier post

:slight_smile:

oh, wow, ultra obvious now. :o

thank sou very much :slight_smile: ,
have a nice evening.

this problem is solved.

:slight_smile:

Post the final code

haaha,
heeere:

for (int z=0; z<maxsteps/4; z++) {
step[4z] = z;
step[4
z+1] = z+1/3;
step[4z+2] = z+1/2;
step[4
z+3] = z+2/3;
};

hui.
thanks again

Congrats :slight_smile:

(You don’t need a semi colon after the closing })

Why not populate the array as 1/6s, then simply divide by 6 when you use it?

0/6, step[0] = {0}; //first cycle start (even number = 0)
2/6, step[1] = {1/3}; //
3/6, step[2] = {1/2}; //
4/6, step[3] = {2/3}; // first cycle end
6/6, step[4] = {1}; //sec. cycle (even number = 1)
8/6, step[5] = {4/3};
9/6, step[6] = {3/2};
10/6, step[7] = {5/3}; //sec. cycle end
12/6, step[8] = {2};
14/6, step[9] = {7/3};
... etc.

Then you don't need a big array of floats, just byte or unsigned int if you're going past 255.

There is probably not even a need for the array - a bit of math formula would probably do

could you enlighten me again?

What are you trying to do ?
What’s the use case for the array?

there is a menu on a display.
in this array should be mathematic values for a variable calculations with a formula.
you can scroll trough them with two physical buttons +/-, activate them with enter button, so that you can calculate with them.
another array is set to what should be shown on the display if one of the value arrays is activated.

value[1] = 4/3; //this is the value array, with this value will be calculated
displayValue[1] = "1 1/3"; // this should be shown on the display, if value[1] is chosen and set

there are more other values which should be variable in the calculation formula, 7 in total.
you can change those 7 values around, but only in the preset values in the arrays not freely, to get a result.

Why are you "lurching" through the values? The difference between 0 and 1/3 is not the same as between 1/3 and 1/2. The number that is halfway between 0 and 1/2 is 1/4.

indeed.
i don't need that.
i need those 1/3, 1/2 and 2/3 stops

olafolaf:
indeed.
i don't need that.
i need those 1/3, 1/2 and 2/3 stops

I was asking you why.

Yes curious what is the use case

Otherwise if you want a math formula this might work (need to double check to force integer calculation)

for (int z = 0; z <40; z++)
   step[z] = (z/4) + ((z%4)%2)/3.0 + ((z%4)/2)/2.0 - ((z%4)/3)/6.0;