How to control MCP3008 sampling rate by Arduino & How to write excel file

Here, this is my previous coding for performing ADC operation of MCP3008 (SPI communication protocol) by using Arduino Mega 2560 Board.

#define SELPIN 53 //Selection Pin 
#define DATAOUT 51//MOSI 
#define DATAIN  50//MISO 
#define SPICLOCK  52//Clock  
int readvalue; 



void setup(){ 
 //set pin modes 
 pinMode(SELPIN, OUTPUT); 
 pinMode(DATAOUT, OUTPUT); 
 pinMode(DATAIN, INPUT); 
 pinMode(SPICLOCK, OUTPUT); 
 //disable device to start with 
 digitalWrite(SELPIN,HIGH); 
 digitalWrite(DATAOUT,LOW); 
 digitalWrite(SPICLOCK,LOW);


 Serial.begin(9600); 
} 

int read_adc(int channel) //functiion prototype for Read ADC
{  
  int adcvalue = 0;
  
  byte commandbits = B11000000; //command bits - start (B7) , mode (B6), channel (B3 to B5), dont care (B0 to B2)  Note: B1 (1st bit // LSB) ; B7 (8th bit //MSB)  (Channel refer to datasheet)
  
  //allow channel selection
  commandbits|=((channel-1)<<3); // must put "|" (not one "1" symbol) 
  
  digitalWrite(SELPIN,LOW); //Select adc
  // setup bits to be written
  for (int i=7; i>=3; i--){
   digitalWrite(DATAOUT,commandbits&1<<i);  // correct instruction  Note: it means commandbits & (1<<i), do "1<<i" first. 
   //digitalWrite(DATAOUT,HIGH); //cannot use this instruction
    
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);    
  }

  digitalWrite(SPICLOCK,HIGH);    //ignores 2 null bits
  digitalWrite(SPICLOCK,LOW);
  digitalWrite(SPICLOCK,HIGH);  
  digitalWrite(SPICLOCK,LOW);

  //read bits from adc since it is ADC is 10 bits, then int i=9; i>=0; i-- ; if ADC is 12 bits, then nt i=11; i>=0; i--
  for (int i=9; i>=0; i--){
    adcvalue+=digitalRead(DATAIN)<<i;
    
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);
  }
  digitalWrite(SELPIN, HIGH); //turn off device
  return adcvalue;
}



void loop() 
{ 
 readvalue = read_adc(1);  // read_adc(8) is to read the CH7 of MCP3008 // MCP3208  ; read_adc(1) is to read the CH1 of MCP3008 // MCP3208
 Serial.println(readvalue,DEC); 
 readvalue = read_adc(2); 
 Serial.println(readvalue,DEC);
 readvalue = read_adc(3);  // read_adc(8) is to read the CH7 of MCP3008 // MCP3208  ; read_adc(1) is to read the CH1 of MCP3008 // MCP3208
 Serial.println(readvalue,DEC); 
 readvalue = read_adc(4); 
 Serial.println(readvalue,DEC);
 readvalue = read_adc(5);  // read_adc(8) is to read the CH7 of MCP3008 // MCP3208  ; read_adc(1) is to read the CH1 of MCP3008 // MCP3208
 Serial.println(readvalue,DEC); 
 readvalue = read_adc(6); 
 Serial.println(readvalue,DEC);
 readvalue = read_adc(7);  // read_adc(8) is to read the CH7 of MCP3008 // MCP3208  ; read_adc(1) is to read the CH1 of MCP3008 // MCP3208
 Serial.println(readvalue,DEC); 
 readvalue = read_adc(8); 
 Serial.println(readvalue,DEC); 
 
 Serial.println(" "); 
 delay(250); 
}

My problem is how to control sampling rate of MCP3008 such as 100 samples per second or 1000 samples per second by modifying Arduino coding?

Besides, how to write these data (ADC value) with timestamp and elapsed time into a excel file (not SD card) by modifying the Arduino coding?

Thanks you very much and very appreciate for helping.

My problem is how to control sampling rate

The same way you control any other sampling rate. Call the read_adc() function at specific intervals - using millis(), micros() or a timer interrupt, to determine if it is time to sample again.

Besides, how to write these data (ADC value) with timestamp and elapsed time into a excel file

First, you go to Microsoft's web site and download the Excel for Arduino application. Good luck with that. While you're there, you might want to see if you can find Win7 for the Arduino, so it has an operating system.

Hello, thanks you for your recommendations. However, the Arduino Commands of millis() and micros() does not worked for me.

Previously, I using Matlab software interface with Arduino Mega 2560 Board that connected to MCP3008 which able to write correct results (ADC value) into an excel file (.csv) based on Matlab Coding. However, my problem is it only able to produce 1 sample (ADC value) per second in an excel file since my desired result is 100 samples per second or 1000 samples per second because based on datasheet of MCP3008, it can up to 200 kilo samples per second.

Besides, I could not found the "Excel for Arduino application" in Microsoft Website for writing data into a file. Can you send this particular website link to me? So that I can directly download this application.

The Matlab Coding used for performing ADC operation of MCP3008 by Arduino Mega 2560 Board is attached below, the file name known is "subroutine_MCP3008.m", please refer to it.

Thanks you and very appreciate for helpling.

subrountine_MCP3008.m (11.5 KB)

However, the Arduino Commands of millis() and micros() does not worked for me.

Without any explanation or code, all we can say is "Well, that's too bad. I guess your screwed."

I using Matlab software interface with Arduino Mega 2560 Board

I'm interested in how you loaded Matlab on your Mega.

that connected to MCP3008

Which is a ?

which able to write correct results (ADC value) into an excel file (.csv)

A csv file is NOT an Excel file. It is a comma separated values file that just happens to be a format that Excel knows how to read. So does Notepad++ and Word, but you'll never hear anyone refer to a csv file as a Notepad++ file or a Word file. You shouldn't be referring to csv files as Excel files, either.

Besides, I could not found the "Excel for Arduino application" in Microsoft Website

I'm shocked. Well, not really. More like mildly amused that you'd even look.

The Matlab Coding used for performing ADC operation of MCP3008 by Arduino Mega 2560 Board is attached below, the file name known is "subroutine_MCP3008.m", please refer to it.

I did. That code requires that a Firmata sketch be loaded on the Arduino, making it, basically, an idiot. If you then expect the brainless Arduino to do something on a regular basis, you must tell it to do it EVERY TIME IT NEEDS TO DO IT.

It is then MatlLab's responsibility to store the data. You won't be making any changes to the Arduino code to make this happen.