Go Down

Topic: Multiples sensor logger problem with else if statement (Read 1 time) previous topic - next topic

gabscar

Jun 19, 2014, 05:40 pm Last Edit: Jun 19, 2014, 05:43 pm by gabscar Reason: 1
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.

Code: [Select]
// 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.begin(9600);
        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
  if(!SD.begin(CS_pin))
  {
    Serial.println("Card Failure");
    return;
  }
 
    // RTC set up and test
 
  #ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();

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

void loop() {

if (i < no_therm) // read the thermocouples

{

uint8_t channel;
        for (channel=1; channel<=CHANNELS_ENABLED; channel++)
{
tc.select_channel(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)
tc.update();

        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
        i++;
}
}

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);
          Serial.println(voltage);
          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
          i++;
        }
}

else // If all the sensors have been read then log them
{
       DateTime now = rtc.now(); // Request time from RTC
       
       sprintf(file_today_name, "%02d%02d%02d.txt", now.day(), now.month(), now.year()); // (C++ function) Turns file_today_name into date.txt format
       SD.begin(CS_pin);
       File dataFile = SD.open(file_today_name, FILE_WRITE);
       
     
      dataFile.print(' ');
      dataFile.print(now.hour(), DEC);
      dataFile.print(':');
      dataFile.print(now.minute(), DEC);
      dataFile.print(':');
      dataFile.print(now.second(), DEC);
      dataFile.print(' ');
     
      Serial.print(' ');
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      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(DatMat[k][0]);
      dataFile.print(' ');
     
      Serial.print(DatMat[k][0]);
      Serial.print(' ');
      }
      Serial.println(' ');
      dataFile.println(' ');
      dataFile.close();   
      delay(5000); //frequency of data aquisition 
      i =1; // reset counter

}

Nick_Pyner

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

Hackscribble

Hi gabscar

Code: [Select]
const int no_V_In = 1;
...
for (int n = 0; n < (no_V_In -1); n++)


So the for loop is equivalent to ...

Code: [Select]
for (int n = 0; n < 0; n++)

So won't execute.

Regards

Ray



Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

gabscar

Thanks Ray!

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

Code: [Select]
for (int n = 0; n <= (no_V_In -1); n++)

Go Up