Division of values in array does not work properly

Hello all!

As you can see in my code below i am reading values of eight analog inputs. For calibration i need to divide them by specific values (944.8, …) but if i do so it only works for the first 4 analog inputs. For the other 4 its not just not working, i cant even read them anymore.

At least i think that is what is happening. I cant figure out a reason or mistake i am making.

Some help would be great.

Thank you!

const int numReadings = 400;
const int THRESHOLD = 0;

int ports[8] = {A0,A1,A2,A3,A4,A5,A6,A7};

float readings[numReadings];
int   readIndex = 0;
float total     = 0;
float average   = 0;
float averagekg = 0;

void setup()
{
  Serial.begin(9600);

  for (int i = 0; i < numReadings; i++)
  {
    readings[i] = 0;
  }
}

void loop()
{
  ports[0] /= 944.8;
  ports[1] /= 942.6;
  ports[2] /= 947.0;
  ports[3] /= 942.2;
  ports[4] /= 942.8;
  ports[5] /= 939.0;
  ports[6] /= 942.8;
  ports[7] /= 939.4;
  
  total -= readings[readIndex];

  float sum = 0;
  int  count = 0;
  for (int i = 0; i< 8; i++)
  {
    float t = analogRead(ports[i]);
    t = analogRead(ports[i]);
    if (t > THRESHOLD)
    {
      count++;
      sum += t;
    }
  }
  
  
  if (count > 0) 
  {
    readings[readIndex] = sum / count; 
  }
  else
  {
    readings[readIndex] = 0; 
  }

  total += readings[readIndex];
  readIndex = (readIndex + 1) % numReadings;

  average =  total / numReadings;
  
  if(readIndex%10==0)
  {
  Serial.println(average,10);
  }
  
  delay(1);
}

  ports[0] /= 944.8;Why are you dividing a pin number by a floating point value?
What do you hope to achieve?

The "pin number" is constantly receiving an analog signal which is converted do digital and lays between 0 and 1023.

Marv_Goodman:
The "pin number" is constantly receiving an analog signal which is converted do digital and lays between 0 and 1023.

but ports is an array of pins. If you had initialized them in the preferred way, as const, you would see your error.

const int ports[8] = {A0,A1,A2,A3,A4,A5,A6,A7};  // pin numbers will not change...

Please read my reply again and then look at how and where you use the ports array.
I don't think you understand what your code is actually doing.

Marv_Goodman:
The "pin number" is constantly receiving an analog signal which is converted do digital and lays between 0 and 1023.

You have the wrong end of the stick altogether.

Your array has the numbers of the pins. The pin numbers are just numbers. It is when you do

float t = analogRead(ports[i]);

that you actually get a value from the pin and save it into to variable called 't'. It would probably be more sensible to call the variable adcValue - but the compiler won't care.

If you call the variable adcValue it should make it clear how it is different from a pin number.

...R

No. parts[x ] is assigned a pin number

int ports[8] = {A0,A1,A2,A3,A4,A5,A6,A7};

The analogRead is assigned to t

t = analogRead(ports[i]);

The value read from the analog pin (analogRead) is never assigned into the posts array.

If you did, if that is what was happening, then the lines

float t = analogRead(ports[i]);
t = analogRead(ports[i]);

would be broke as the value in the ports array would be outside the range of pin numbers (where is pin 760?)

Ok, I now understood my problem/mistake. thank you for that.

How do I now assign my (i already changed it for understanding) adcValue to my ports, so that i can divide the value from pin A0 by the specific value for A0?

I would create two new arrays.

First array called adcValue (nod to Robin2), int as it is the direct result from analogRead (but read the whole post)
Second array call thresh_divisor as a float.

In the second array place your divisors {944.8, 942.6, ...}

Change the second analog read from t to adcValue[i ]

then divide adcValue[i ] by thresh_divisor[i ]

So you end up with

adcValue[i ] = analogRead(ports[i ]);
adcValue /= thresh_divisor[i ];

That answers your original question but brings to light a second question.

analogRead returns an int, that is being divided by a float and stored in an int (adcValue). If you combine the two lines above into

adcValue[i ] = ((float) analogRead(ports[i ])) / thresh_divisor[i ];

Then define the adcValue as float and get a floating point (untruncated result).

Are you using an Uno?

do you really want such a huge array?

const int numReadings = 400;
//...
float readings[numReadings];

you are consuming 1600 bytes of RAM here...

Are you using an Uno?

I hope not if he wants to use A6 and A7 as analogue inputs

I had the same thought. I wasn't understanding the comment on 4 working and 4 not. But thought we should tackle one problem at a time and the sketch was the problem on the OPs mind.

First of all, no i am using a Nano.

Of course you are right @adwsystems. I end up with a problem regarding the data types. The question is if there is a solution?
I guess the “4 working, 4 not” part was just some sort of coincidence. For your information this all is about reading analog data from 2 force measurement plates, where each one has 4 single sensors. This all is built in, on the ground of a treadmill and in the end i want to measure weight/force, during walking.

@bulldog that was just a try. i will change it to a lower number.

Thanks to you all for your help already.

Thats my code now:

const int numReadings = 400;
const int THRESHOLD = 0;

const int ports[8] = {A0,A1,A2,A3,A4,A5,A6,A7};

int adcValue[8];
float thresh_divisor[8] = {944.8, 942.6, 947.0, 942.2, 942.8, 939.0, 942.8, 939.4};  

float readings[numReadings];
int   readIndex = 0;
float total     = 0;
float average   = 0;
float averagekg = 0;

void setup()
{
  Serial.begin(9600);

  for (int i = 0; i < numReadings; i++)
  {
    readings[i] = 0;
  }
}

void loop()
{

  total -= readings[readIndex];

  float sum = 0;
  int  count = 0;
 
  for (int i = 0; i< 8; i++)
  {
    //float adcValue[i] = analogRead(ports[i]);
    adcValue[i] = analogRead(ports[i]) / thresh_divisor[i];
    //adcValue[i] = analogRead(ports[i]);
    //adcValue /= thresh_divisor[i];
    if (adcValue > THRESHOLD)
    {
      count++;
      sum += adcValue;
    }
  }
  
  if (count > 0) 
  {
    readings[readIndex] = sum / count; 
  }
  else
  {
    readings[readIndex] = 0; 
  }

  total += readings[readIndex];
  readIndex = (readIndex + 1) % numReadings;

  average =  total / numReadings;
  
  if(readIndex%10==0)
  {
  Serial.println(average,10);
  }
  
  delay(1);
}

Since you went with my option 2, don't forget to redefine adcValue as a float. (you still have it as an int)

  for (int i = 0; i < numReadings; i++)
  {
    readings[i] = 0;
  }

The runtime initialisation crt0 ensures these values are initialised to zero (0.0) before setup runs. You’re just wasting code space doing it yourself.
float thresh_divisor[8] = {944.8, 942.6, 947.0, 942.2, 942.8, 939.0, 942.8, 939.4};  Don’t you want to qualify this as “const” also?

I can’t see a good reason for adcValue to be an array.
In fact, if it is, the compiler will complain about this if (adcValue > THRESHOLD)

  Serial.println(average,10); It doesn’t make any sense to print more decimal places than the float datatype can provide - about 6 or 7 is the best you can expect, if I remember correctly.