LowLatencyLogger with more than 8 analog inputs

I have an Uno with a sparkfun datalogger shield and an MCP3008 adc (extra 8 adc inputs with 10bit resolution connected to the uno via SPI)

I'm basically using the lowlatencylogger from sdfat and modifying slightly to also log the extra anolog inputs from the MCP3008 based on the "simpleADC" library which apparently was originally intended for raspberry pie and ported to arduino

I modified "acquiredata" function to acquire both the 6 anolog inputs on the UNO and the 8 more on the MCP3008. That part seems to be working, I can use any combination of up to 8 total inputs from the UNO board or the MCP and everything works, but if I go above 8 it doesn't seem to do the conversion from binary to csv correctly.

The part of the code that does that function is WAY over my head, so I was hoping somebody could help me figure out why it doesn't like more than 8 anolog inputs.

Complete sketch below

Thanks for any help

Apparently the complete code is over the size limit for forum posts, so the sdfat low latency logger that I started with is here:

and the modifications that I made are all in the "acquiredata" function

original:

// Acquire a data record.
void acquireData(data_t* data) {
  data->time = micros();
  for (int i = 0; i < ADC_DIM; i++) {
    data->adc = analogRead(i);
  }
}

With my changes:

// Acquire a data record.
void acquireData(data_t* data) {
  data->time = micros();
  for (int i = 0; i < 6; i++) {
    data->adc = analogRead(i);
  }
  digitalWrite(10, HIGH);
  digitalWrite(8, LOW);
  for (int i=6; i<ADC_DIM; i++) {
   int val = adcc.readADC(i);
       data->adc = val;
  }
  digitalWrite(8, HIGH);
  digitalWrite(10, LOW);
  
  
  
}

I also had to add the following which I stole from the "Simpleadc" example in the MCP3008 liberary on github:

#include <MCP3008.h>


// define pin connections
#define CS_PIN 8
#define CLOCK_PIN 9
#define MOSI_PIN 11
#define MISO_PIN 12
MCP3008 adcc(CLOCK_PIN, MOSI_PIN, MISO_PIN, CS_PIN);

However I don't think any of that is the problem, because it all works as long as I keep the total number of analog inputs at 8 or less, whether they come from the MCP3008 or the on board analog pins.

Thanks for any help

Here is an exmple of the result I'm getting.

time adc0 adc1 adc2 adc3 adc4 adc5 adc6 adc7 adc8 adc9 adc10 adc11 adc12 adc13
162310008 717 274 274 463 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162320008 880 274 274 366 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162330008 950 274 274 322 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162340008 982 274 274 303 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162350008 996 275 275 293 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162360008 1002 275 274 289 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162370008 1006 274 274 286 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162380008 1007 274 274 285 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162390008 1008 274 274 284 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162400008 1008 275 274 283 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162410008 1009 274 273 283 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162420008 1009 274 274 283 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162430012 1008 274 273 282 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162440008 1010 274 274 282 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162450008 1009 274 274 282 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162460008 1009 274 273 282 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162470008 1010 274 273 281 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162480008 1009 274 273 281 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162490008 1010 274 273 281 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162500008 1009 274 273 281 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162510008 1010 274 274 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162520008 1010 274 273 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162530008 1009 274 274 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162540008 1010 274 273 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162550008 1009 274 273 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162560016 1010 274 274 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162570008 1010 274 273 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162580008 1009 274 273 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162590008 1010 274 273 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
162600008 1009 273 273 280 1023 1023 0 0 65535 65535 65535 65535 65535 65535
[/table]

I should note I'm logging at 100hz and don't seem to be getting any errors, and adc0 through adc7 seem to be giving reasonable results.

I should note that you have NOT posted code correctly. Use code tags (the # icon) NOT quote tags.

This is clearly NOT what your code looks like:

    data->adc = analogRead(i);

My apologies for that.

Fixed now I think.

Fixed now I think.

  for (int i = 0; i < 6; i++) {
    data->adc = analogRead(i);
  }
  digitalWrite(10, HIGH);
  digitalWrite(8, LOW);
  for (int i=6; i<ADC_DIM; i++) {
   int val = adcc.readADC(i);
       data->adc = val;
  }

I guess I'm failing to see how reading 6 pins, and overwriting the data in data->adc, then diddling two pins, reading some unknown number of pins, writing over the same value, then diddling two more pins is going to accomplish anything.

Post ALL of your code, with links to the libraries you are using.

I'm guessing that adc is supposed to be an array. If it IS an array, have you changed the size of the array, to accommodate the additional data?

Again my apologies, I'm committing the "forum sin" of assuming the rest of the wold is familiar with the details of what I'm doing.

I'll try to explain better. I tried to post the whole code but I got an error message that it exceeded the maximum length of a forum post. Maybe it will let me do it now that I know how to properly post code, can't try again until tonight after work.

I'm using the "Lowlatencylogger" example in the SdFat library here:

And I'm bashing it together with the MCP3008 library here. This is an 8 channel 10 bit ADC connected by SPI:

The reason for diddling with the pins is that MCP3008 is a SPI connected device, and I'm logging to a spi connected SD card (the adafruit data logging shield). So multiple SPI devices. pins 8 and 10 are the CS pins for the ADC and the SD card respectively.

ADC_DIM is the total number of anolog pins being logged, in this case it happens to be 14
"adc"....is a bit of a mystery to me, it seems to be part of the structure "data"

both of them are appear in the included file as part of lowlatencylogger as below:

#ifndef UserDataType_h
#define UserDataType_h
const uint8_t ADC_DIM = 14;
struct data_t {
  unsigned long time;
  unsigned short adc[ADC_DIM];
};
#endif  // UserDataType_h

With all of that said, I think you managed to identify my problem even with all of my vagueness.

the "adcc.readADC(i) will only work with i values of 0 through 7, not the 6 through 13 that I am asking it to do

I need to change it to something like this, which I will try tonight:

// Acquire a data record.
void acquireData(data_t* data) {
  data->time = micros();
  for (int i = 0; i < 6; i++) {     //Read the 6 anolog input pins on the UNO
    data->adc = analogRead(i);
  }
  digitalWrite(10, HIGH);          //Sets the CS pin for the SPI sd card reader high to "disconnect" it
  digitalWrite(8, LOW);            //Sets the CS pin for the SPI connected external ADC low to "connect it"
  for (int i=0; i<8; i++) {        
   data->adc = adcc.readADC(i);        //Read the 8 analog inputs from the MCP3008
     
  }
  digitalWrite(8, HIGH);          //disconnect MCP3008 ADC
  digitalWrite(10, LOW);          //re-connect sdcard reader

I need to change it to something like this, which I will try tonight:

I know that isn't going to work.

    data->adc = analogRead(i);

You can't assign a value to an array like that.

   data->adc = adcc.readADC(i);        //Read the 8 analog inputs from the MCP3008

Or that.

Don't forget, when you add the necessary [, index, and ] that the index value in the second loop is NOT i.

I think I see what you mean, I need something like:

// Acquire a data record.
void acquireData(data_t* data) {
  data->time = micros();
  for (int i = 0; i < 6; i++) {     //Read the 6 anolog input pins on the UNO
    data->adc[i] = analogRead(i);
  }
  digitalWrite(10, HIGH);          //Sets the CS pin for the SPI sd card reader high to "disconnect" it
  digitalWrite(8, LOW);            //Sets the CS pin for the SPI connected external ADC low to "connect it"
  for (int i=6; i<14; i++) {        
   data->adc[i] = adcc.readADC(j);        //Read the 8 analog inputs from the MCP3008
     //somehow I need to get "j" to increment from 0 to 7, or I can just make j = i-6                                                      
  }
  digitalWrite(8, HIGH);          //disconnect MCP3008 ADC
  digitalWrite(10, LOW);          //re-connect sdcard reader

for (int i=6, j=0; i<14; i++, j++) {
data->adc[ i ] = adcc.readADC(j); //Read the 8 analog inputs from the MCP3008
//somehow I need to get "j" to increment from 0 to 7, or I can just make j = i-6 <== Yep, that, too.
}

Perfect thanks!