Reading analog values and store it with for loop

Hello everyone,

Would you please correct my code or make it better to read 16 analogue signals and store them in variables to be used in different steps in the code? I have made it like the one below. It works, but it sounds inelegant.

float Sensor0 = 0.00;
float Sensor1 = 0.00;
float Sensor2 = 0.00;
float Sensor3 = 0.00;
float Sensor4 = 0.00;
float Sensor5 = 0.00;
float Sensor6 = 0.00;
float Sensor7 = 0.00;
float Sensor8 = 0.00;
float Sensor9 = 0.00;
float Sensor10 = 0.00;
float Sensor11 = 0.00;
float Sensor12 = 0.00;
float Sensor13 = 0.00;
float Sensor14 = 0.00;
float Sensor15 = 0.00;
float Sensor16 = 0.00;
byte Sensor[16] = { Sensor0, Sensor1, Sensor2, Sensor3, Sensor4, Sensor5, Sensor6, Sensor7, Sensor8, Sensor9, Sensor10, Sensor11, Sensor12, Sensor13, Sensor14, Sensor15 };



void setup() {
  Serial.begin(115200);
}

void loop() {
  for (int i = 0; i <= 15; i++) {
    // Read analog value from channel A0 to A15
    Sensor[i] = analogRead(i);
  }
  // Example
  if (Sensor4 > Sensor6) {
    Serial.println("High Reading")
  }
  if (Sensor6 > Sensor8) {
    Serial.println("Stop machine")
  }
}

besides some syntax error, guessing the primary problem is specifying the analog pins which don't necessarily start at 0.

guessing you're not using an Arduino UNI which doesn't have 16 analog pins

const int Nsensor = 16;
int sensor [Nsensor];

void setup() {
    Serial.begin(115200);
}

void loop() {
    for (int i = 0; i < Nsensor; i++)
        sensor[i] = analogRead(A0 + i);

    // Example
    if (sensor [4] > sensor [6])
        Serial.println("High Reading");

    if (sensor [6] > sensor [8])
        Serial.println("Stop machine");
}

Capitalize Constants, not variables

1 Like

You seem to understand what an array is just one line later. So I don't understand what you were thinking here. When you find yourself adding numbers to variables names, stop and realize that what you want is an array.

float sensors[17];

But keep in mind that analogRead returns an int, storing that into a float is not going to magically add extra precision. It just wastes memory.

Thank you, @gcjr ;

Yes, I use Mega for this project.

Thank you for your comment @Delta_G.

I am here seeking support because I am not great at software. I try and learn. However, I always get precise float readings from my sensors with any Arduino or ESP microcontroller unless I identify my variables as integers. please advise.

The range of values represented by an analogRead are capable of being precisely represented by float variables, but it isn't recommended to use them for this purpose, because the representation consumes twice as much memory as is necessary, and their manipulation is much slower than for simple integer types.
However, a byte variable is not capable of representing the full range of an analogRead.

That's really interesting @anon56112670; they are measuring pressure and current, so if it is more than 1.75 amp, another action must be taken. any recommendation, please.

The Arduino doesn't know anything about amperes, so some conversion must be done.
You have posted no details of this, so I can't comment further.

AnalogRead

returns an int. Converting it to a float does not make it more precise - in fact less so.

Be aware that there will be noise - because you are converting a REAL value to a digital value.

So you need to allow for that when performing your comparison test.

So you need hysteresis. Eg if ( (abs(A - B)) > 2) { //there has been a significant change

..

}

A float variable is quite capable of representing precise ADC values beyond 16 bits.
The IEEE754 32 bit representation has a 23 bit mantissa (fraction)

So what? analogRead returns an integer number. Putting it into a float is not going to suddenly magically make it more precise. It's not going to start suddenly interpolating between integer numbers and giving you any decimal places?

I was simply countering the assertion that making a float somehow makes the value less precise.
Whilst that may be a wider truth, in this specific instance, it's false.
I'm not advocating their use (far from it), merely pointing-out a falsehood, or, at best, a half-truth (no pun intended)

We will have to disagree. An integer variable is an exact representation of an integer number. Count the sheep in a field and you are using integers.

The distinction shows up when you divide, and it becomes a problem such as when converting the value from the ADC to a voltage.

Suppose your ADC reads 526 and the reference is 5V.
526 * 5 / 1024 = 2.568359375

WRONG.

Having converted to float you now expect a 23 bit mantissa.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.