Populate array with for loop

Hello,

I am struggling to write a portion of code and was hoping for some assistance. I am trying to take the results of a calculation and populate an array with them.

int raw = 0;
int Vin = 5;
float Vout = 0;
float R1 = 5000;
float R2 = 0;
float buffer = 0;
float R2array[20];

  if(raw){
    buffer = raw * Vin;
    Vout = (buffer)/1024.0;
    buffer = (Vin/Vout) - 1;
    R2 = R1 * buffer;

    for(int i=0; i<20; i=i++){
    R2array[i];
    }
    Serial.print(R2array[5]); 
}

I want to populate my R2array with my R2 value as it will be slightly different every time. Any help will be greatly appreciated.

Another way I have tried is to put the calculation in the for loop.

    for(int i=0; i<20; i=i++){
    raw = analogRead(analogPin);
    digitalWrite(8, HIGH);
    if(raw){
    buffer = raw * Vin;
    Vout = (buffer)/1024.0;
    buffer = (Vin/Vout) - 1;
    R2 = R1 * buffer;
    R2array[];
    }

I am fairly new to Arduino programming, maybe I should be trying to populate my array another way? Anyway, thanks in advance!

That correct. You need a statement assigning each of the 20 calculated values to an array value by index. The last statement of the for loop needs completing

R2array[i] = R2;
1 Like

Thank you cattledog. I added the completion of R2array[i] = R2.
It seems like I am getting closer, however, when I try Serial.print(R2array[5]); I get all zeros (000.000.000.....)

Please post complete code which compiles.

There is a compiler warning with the syntax of the for loop.

 warning: operation on 'i' may be undefined [-Wsequence-point]
    for (int i = 0; i < 20; i = i++) {
                            ~~^~~~~

Use

 //for (int i = 0; i < 20; i = i++) { //i=i++ may be undefined
  for (int i = 0; i < 20; i++) {

It's not clear to me what the value you are trying to store in the array represents after this manipulation.

    buffer = raw * Vin;
    Vout = (buffer)/1024.0;
    buffer = (Vin/Vout) - 1;
    R2 = R1 * buffer;
1 Like
i = i++

do you mean?:

for(int i=0; i<20; i++)

Check the "precedence" of instructions: here: when is the "=" done and when is the "++" done?

What happens here is this:
"assign to i the value if i (the same value at the end!). Afterwards, when this assignment was done: increment i to next value."
So, you assign all the time the same value to i, it is like:

i = i; //anytime later do: i = i + 1;

But your for loop has no clue which version of i do you mean? The one after the first instruction or the one after the second instruction?

It is optimized to MCU instructions used (machine code, instruction set). So, the for loop uses just the "i = i" but the "i++" as "i = i + 1" is done automatically by the instruction set:
after i was taken for the next loop to start over - it is incremented. So, this "i = i + 1" is done after you have jumped back to start of for loop.

I would assume: you never increment i: your for loop takes just and only the value 0 for i.
It will never progress. You never write to other indexes i (just [0], anything else is never written).

This "=" vs. '==" and "i++" vs. "++i" is pretty tricky to understand in C/C++ coding.
So, check carefully the "spelling" of instructions.

Assume that "i = i++" will always take the same value again, over and over, here the init value 0. This "++" does not have any effect.

1 Like

What happens here is UNDEFINED. The expression "i = i++" produces different results (increment, no change, crash...) on different versions of different compilers.

Some valid ways of incrementing 'i':
i = i + 1
i += 1
i++
++i

2 Likes

You're attempting to divide by zero.

1 Like

You missed a line:

    Vout = (buffer)/1024.0;
    buffer = (Vin/Vout) - 1;
1 Like

Wow, appreciate all the help! One more question below in addition to more info on the purpose of my program.

The program is a voltage divider reading the resistance of a thermocouple as temp rises and falls. My initial program without the for loop/array works, however, I have been enjoying learning to program and want to keep developing/improving the program.
With that said, the goal of the for loop/array is to gather 20 calculations, then calculate the average of the 20 for better accuracy.

How can I be sure my array is populating correct?

float R2array[20];
int analogPin = 1;
int Vin = 5;
float Vout;
float R1 = 5000; //Fixed value
float R2;

void setup(){
Serial.begin(9600);
pinMode(8, OUTPUT);
}

void loop(){
  digitalWrite(8, HIGH);
  int raw = analogRead(analogPin);
  if(raw){
    for(int i=0; i<20; i=i+1){
    float buffer = raw * Vin;
    Vout = (buffer)/1024.0;
    buffer = (Vin/Vout) - 1;
    R2 = R1 * buffer;
    R2array[i]=R2;
    }  
    //Serial.println(R2array[3]);
  }
}

Thanks again everyone!
Any suggestions to help improve my code are always welcome:)

You need to take a new reading 20 times within the for loop.
With the present code, you take one reading before the for loop and then do the math on the same value 20 times.

1 Like

Thank you cattledog; I knew I wasn't populating the array correctly. I am struggling to see exactly what you're saying, but I will keep working through it.

What is a good way to verify I am populating my array correctly? I really want to get this working properly before moving on to the next phase - calculating the average.

IDK, something like below. My biggest problem is not knowing if I am populating the array with different values as I intend.

    for(int i=0; i<20; i=i+1){
    digitalWrite(8, HIGH);
    int raw = analogRead(analogPin);
    if (raw){
    float buffer = raw * Vin;
    Vout = (buffer)/1024.0;
    buffer = (Vin/Vout) - 1;
    R2 = R1 * buffer;
    R2array[i]=R2;
    }
    Serial.println(R2array[i]);
  }
}

What is a good way to verify I am populating my array correctly?
After you populate the array, do the numbers make sense?

for(int i=0; i<20; i=i++){
 Serial.println(R2array[i]);
float R2array[20];
int analogPin = 1;
int Vin = 5;
float Vout;
float R1 = 5000; //Fixed value
float R2;

void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);
  int raw = analogRead(analogPin);//for best accuracy discard first reading
  if (raw) {
    for (int i = 0; i < 20; i = i + 1) {
      raw = analogRead(analogPin);
      float buffer = raw * Vin;
      Vout = (buffer) / 1024.0;
      buffer = (Vin / Vout) - 1;
      R2 = R1 * buffer;
      R2array[i] = R2;
    }
  }

  for (int i = 0; i < 20; i = i + 1)
  {
    Serial.println(R2array[i]);
  }
}

void loop() {}

Can you attach a sketch of how you have your voltage divider configured, and where you are taking the measurement.

1 Like

Note: I have changed my fixed value to 5.5Kohm.

Are you using a thermistor? Can you provide a link to your temperature sensor.

The typical configuration is with the varying resistor(R2) at the ground side of the divider.
https://learn.sparkfun.com/tutorials/voltage-dividers/all

I think you have the math correct for your placement but it numbers don't make sense you might want to switch the arrangement to the more standard configuration and formula.

Thank you for the link cattledog:) I look forward to a more in-depth review of the site.

Yes, it is a thermistor. I am measuring within the ranges of 4K and 6K causing a fault when resistance goes outside of those parameters. With the 5K fixed resistor, I randomly get a (+-200ohm) difference compared to the majority. Surprisingly, switching to a 5.5K fixed resistor value dropped that deviation to only (+-20ohm). Not nearly as large, nevertheless, an average of these calculations should reduce the deviation even further.

Terminal Results:
5248.09
5227.62
5227.62
5227.62
5227.62
5227.62
5227.62
5227.62
5207.22

Since my program works on a basic level. My intent is to improve this program to dive deeper into programming. Basically, after I figure out how to get these values into an array I get to learn something new. Average them.

Thank you cattledog and others, for all your questions and helping me learn.

You can stop right there. Why would you perform the same write 20 times?

It sounds like you have hardware issues. Then averaging is only "sweeping the problem under the rug".

Hi aarg, thank you for the feedback.
My original code has the write outside of the for loop; this iteration is me trying to apply cattledog's advice, "You need to take a new reading 20 times within the for loop." I am trying to get 20 calculations into an array, and thus far I cannot seem to do that.

Any feedback related to accomplishing that would be super helpful.