Multiples sensor logger problem with else if statement

I have written code to read sensors, first thermocouples, then voltage inputs (flow meters), the code stores each reading into a matrix of no_sensors x 1 dimension, then the matrix is written to and SD card. I am having a problem with the voltage reading with the else if statement not working. Not quite sure where the issue lies. My code is below. Any help would be much appreciated.

// use the following libraries
#include <SPI.h> // serial peripheral interphasing (PIN communications MISO MOSI)
#include<SD.h> // sd function library
#include "MAX31855.h" // cold junction compensator library
#include "KTA259V3.h" // multiplexer library
#include "RTClib.h" // RTC library
#include <Wire.h> // required for RTC

RTC_DS1307 rtc; //RTC command
int CS_pin = 10; // SD pin
char file_today_name[30]; // Create char to hold today's file name by date

const int no_therm = 2; //****************** INPUT THE NUMBER OF THERMOCOUPLES IN USE (1-8) ************************************
const int no_V_In = 1; // ****************** INPUT THE NUMBER OF VOLTAGE INPUTS IN USE (1-2) ************************************
const int no_sensors = (no_therm + no_V_In); // SUM OF SENSORS IN USE

float DatMat [no_sensors][1]; // Data matrix of dimensions (num of sensors X 1) to store sensor data
int i = 1; // counter
int k; //matrix indexing counter

#define CHANNELS_ENABLED no_therm // The number of channels enabled
KTA259V3 tc = KTA259V3(9); // Init with chip select on pin 9

void setup()

        Serial.println("Datalogger initialising");
        pinMode(SS, OUTPUT);
   // SD set up and test

  pinMode(CS_pin, OUTPUT); // CS pin is an output
  // If CS_pin reads false the program will terminate
    Serial.println("Card Failure");
    // RTC set up and test
  #ifdef AVR
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due

  if (! rtc.isrunning()) 
    Serial.println("RTC is NOT running!");

void loop() {

if (i < no_therm) // read the thermocouples


	uint8_t channel;
        for (channel=1; channel<=CHANNELS_ENABLED; channel++)
	delay(100); // each sensor is read in 0.1s (allows all sensors to be read within 1s with enough delay for them to settle)

        DatMat [i-1][0] = (tc.get_temperature()); // Stores sensor data into a matrix, placing each into ascending rows, this will then be used to log the data

else if ((i >= no_therm) && (i < no_sensors)) // if the thermocouples have been read read the voltage inputs
        for (int n = 0; n < (no_V_In -1); n++) 
          int b = analogRead(n);
          float voltage = b * (5.0 / 1023.0);
          DatMat [i-1][0] = (voltage); // Stores sensor data into a matrix, placing each into ascending rows, this will then be used to log the data

else // If all the sensors have been read then log them
       DateTime now =; // Request time from RTC
       sprintf(file_today_name, "%02d%02d%02d.txt",, now.month(), now.year()); // (C++ function) Turns file_today_name into date.txt format
       File dataFile =, FILE_WRITE);
      dataFile.print(' ');
      dataFile.print(now.hour(), DEC);
      dataFile.print(now.minute(), DEC);
      dataFile.print(now.second(), DEC);
      dataFile.print(' ');
      Serial.print(' ');
      Serial.print(now.hour(), DEC);
      Serial.print(now.minute(), DEC);
      Serial.print(now.second(), DEC);
      Serial.print(' ');      
      for (k=0; k <= (no_sensors -1); k++) // This loop writes sensor data to file and prints to screen for all sensors
      dataFile.print(' ');
      Serial.print(' ');
      Serial.println(' ');
      dataFile.println(' ');
      delay(5000); //frequency of data aquisition  
      i =1; // reset counter

Why do you need to have a matrix? Isn't it simpler to just read the sensors and then write them?

Hi gabscar

const int no_V_In = 1;
for (int n = 0; n < (no_V_In -1); n++)

So the for loop is equivalent to …

for (int n = 0; n < 0; n++)

So won’t execute.



Thanks Ray!

I am looping through the analogue pins, so it should have been less than or equal to.

for (int n = 0; n <= (no_V_In -1); n++)