Which way to read external ADC's is preferred?

So, I have at least four external MCP3008 chips to read analog data from. I've come up with three different ways to read them. Wondering which method is preferred, or is there another way I haven't thought of?

void scanSensors() {
  
  for (byte i = 0; i <= 31; i++) {
    switch (i) {
      case 0 ... 7:
        sensors[i] = adc1.readADC(i);
        break;
      case 8 ... 15:
        sensors[i] = adc2.readADC(i - 8);
        break;
      case 16 ... 23:
        sensors[i] = adc3.readADC(i - 16);
        break;
      case 24 ... 31:
        sensors[i] = adc4.readADC(i - 24);
        break;
    }
  }

  for (byte i = 0; i <= 31; i++) {
    if ( (i >=0) && (i <= 7) ) {
      sensors[i] = adc1.readADC(i);
    }
    else if ( (i >=8) && (i <= 15) ) {
      sensors[i] = adc2.readADC(i - 8);
    }
    else if ( (i >=16) && (i <= 23) ) {
      sensors[i] = adc3.readADC(i - 16);
    }
    else {
      sensors[i] = adc4.readADC(i - 24);
    }
  }

  for (byte i = 0; i <= 7; i++) {
    sensors[i] = adc1.readADC(i);                    
  }
  for (byte i = 8; i <= 15; i++) {                   
    sensors[i] = adc2.readADC(i - 8);                
  }                                                  
  for (byte i = 16; i <= 23; i++) {
    sensors[i] = adc3.readADC(i - 16);               
  }
  for (byte i = 24; i <= 31; i++) {                  
    sensors[i] = adc4.readADC(i - 24);
  }


  for (byte i = 0; i <= 31; i++) {                   //convert analog input to on or off
    sensors[i] >= 512 ? sensorArray[i] = 1 : sensorArray[i] = 0;
  }
}

Hope i did the code correctly, first post...

Thanks,

Mandy

Oops.

(If you're wonder what "oops" means, I suggest you write a simple test loop to see if the expression I quoted does what you hope/expect it does.)

Oops! Hopefully corrected now.

Mandy

Do you really need the values placed in a huge anonymous array? I'm just trying to imagine how you would use that... now I see you convert all of the values to digital. Why not just input digital values on digital input pins in the first place?

Every sensor is the same. They are all LDRs. Each one is monitoring its own zone. Thus the analog inputs converted to digital on/off. I'm detecting if something is blocking light in any of the 32 or more zones.

Which way of reading the sensors is most efficient? It's done three different ways in the code, wondering which is preferred?

Mandy

Well, you already have A/D hardware, so it's a moot point for this project. But you can read LDR circuits directly from a digital input, using the same circuit and components that you would for analog. You just have to live with a fixed threshold (you've chosen 512, a digital input would switch very near that).

Analog reads are relatively slow, so attempts to optimize the code are futile. It's better to expend that effort to make it clearer and more modular. I would do that with structs or classes. But your code can be done like this:

  for (byte i = 0; i < 8; i++) {
    sensors[i] = adc1.readADC(i);                    
    sensors[i+8] = adc2.readADC(i);                
    sensors[i+16] = adc3.readADC(i);                
    sensors[i+24] = adc4.readADC(i);                
  }

Okay, so which way to read doesn't matter. I haven't got the hardware fully setup, so the 512 value is a placeholder until I find the real world value.

The Mega I'm using doesn't have 32 or more spare pins necessary, so I went with external ADCs instead.

It's a pretty big project: 32 or more LDRs, 22 outputs for relays and 22 servos driven by PCA9685 boards all on one Mega. All LDRs data are sent to and external computer program via serial. The computer program sends the outputs to the mega for the relays and servos. Not sure if Structs and Classes would complicate the data transfer to the external computer(actually a Raspberry Pi). I can add the full program if you would like to see it.

Current cycle time of project is approx. 65ms. to read inputs and send outputs to all devices in one program scan.

Mandy

In that case, A/D is more appropriate. I hope you like my version.

Sorry, forgot that in last reply. Your code is very elegant. Will be using.

Mandy