Please help me make this loop work

Hi Guys,

I thought I was steaming through this little problem, but I’ve hit a wall and cannot figure out how to get passed it.

I am writing a little program that takes a few user variables then creates an ‘S’ shaped curve of data inside an array. The S must finish at the number 140±2 and begin at 80±1.

I have managed to make the S curve by starting at 140, using a little maths then backfilling the array.

What I am struggling with though is how to get the program to check if testArray[0] < 80 then repeat the function changing one of the variables (c) until testArray>= 80.

I am sure I am missing something incredibly obvious but whenever I try and get the function to repeat with the variable changing testArray[0] just spits out the answer to the very first run through the function and doesn’t change.

Any help would be massively appreciated…

Smirks

float i = 140;       // Max number at the end of the S
int testArray[90];   // array to store the current S values.  The arraysize will be determined by the users decision for how long the event should last for
int x = 90;          // value for x will be the sames as the array size
float y = 90;        // value for y will be the sames as the array size
float z = 1;         // global standard and does not change
float c = 1.1;       // This will be altered by the program to make the numbers fit correctly.  Every time (testArray[0] < 80) c will get a little smaller
int f = x - 1;       // Used to store the S values in to the array
int zz = 0;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
    
      figure();
      Serial.println(testArray[0]); 
      Serial.println(c); 

}
   
  void figure(){
  while (testArray[0] <= 79)
  {
    x = 90;
    c = c - 0.01;
      while (x >= (0.78 * y))   // Creates the end of the trailing part of the S and stores the value to the array based on 78% of the distance from the start
      {
        z = z * c;
        i = i - z;
        testArray[f] = i;
        Serial.println(testArray[f]); 
        f --;
        x --;      
      }

      while (x >= 0)            // Creates the end of the leading part of the S and stores the value to the array
      {
        z = z / c;
        i = i - z;
        testArray[f] = i;
        Serial.println(testArray[f]); 
        f --;
        x --;
      }
  }

You have

while (testArray[0] <= 79)

but you don’t seem to have a line within the WHILE loop which changes the value of testArray[0] which would mean that the WHILE loop would run for ever

…R

Robin2:
You have

while (testArray[0] <= 79)

but you don’t seem to have a line within the WHILE loop which changes the value of testArray[0] which would mean that the WHILE loop would run for ever

…R

Hi Robin2,

Many thanks for replying.

The function adds the values to testArray as it runs through. testArray[0] is the last variable to get changed.

With one run through of function(), testArray[0] changes from 0 to 9. The while loop seems to keep going through then with testArray[0] = 9, rather than changing the float c by -0.01 as it should each time until testArray[0] = > 79.

I hope this helps explain it a little more?

Thanks again,

Smirks

smirks:
I hope this helps explain it a little more?

I will take your word for it, but it is not obvious to me.

And most of my own problems have arisen where my code was not obvious. Try using meaningful names for your variables so that your code can be read like a short essay.

I'm not even clear what you are trying to do. Are you trying to make a pattern something like this

0000111000
0001000100
0000100000
0000010000
0000001000
0000000100
0010000100
0001111000

...R

Robin2:
I will take your word for it, but it is not obvious to me.

And most of my own problems have arisen where my code was not obvious. Try using meaningful names for your variables so that your code can be read like a short essay.

I'm not even clear what you are trying to do. Are you trying to make a pattern something like this

0000111000
0001000100
0000100000
0000010000
0000001000
0000000100
0010000100
0001111000

...R

Heh, aye the point about the variable naming was very fair. I busted out the code in a 4am brainstorm but never got around to tidying up the names.

The output to the array actually creates an S shape if printed on to a graph, starting at 80 and finishing at 140. The idea is that the user can change a variable to alter the shape of the S, but that the start and finish stay the same.

Anyhow, I have figured out what was stopping it form working and fixed it! I wasn't reverting some of the important variables that change back to their original starting point. I've now fixed that and it works quite nicely!

Hoozah!

Thanks for your time in looking at it though, most appreciated!

Smirks