Go Down

Topic: Reading Signal through analog input A5, SD card is not being written while A5=0V (Read 716 times) previous topic - next topic

carluri

Hello,

I'm trying to log some data from a square wave that jumps from 0V to 1.3V at some intervals.  I'm using the arduino uno and writing to an SD card.  The way I do this is by filling 2 buffers and alternating them between being filled and being written to the SD card.

Everything works fine except that when the A5 input is 0V then no data is written to the SD card.  When the square wave is 1.3V then data is correctly logged in to the SD card.

I haven't been able to find a solution for this problem.  Can anyone give me an idea on how to solve it?

When I use the serial monitor I see that the values on the A5 pin are being read fine.  Is just the writing to the SD card that stops.

Below is the code that I am using.  Any help is greatly appreciated.

Code: [Select]
#include <ArduinoStream.h>
#include <bufstream.h>
#include <ios.h>
#include <iostream.h>
#include <istream.h>
#include <MinimumSerial.h>
#include <ostream.h>
#include <Sd2Card.h>
#include <SdBaseFile.h>
#include <SdFat.h>
#include <SdFatConfig.h>
#include <SdFatmainpage.h>
#include <SdFatStructs.h>
#include <SdFatUtil.h>
#include <SdFile.h>
#include <SdInfo.h>
#include <SdStream.h>
#include <SdVolume.h>

#include <TimerOne.h>

#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;  // define the Real Time Clock object

//-----------------------------------------------------------------------------
const uint8_t SD_CHIP_SELECT = SS;

// Data inputs:
const uint8_t SensPin1 = A0;
const uint8_t SensPin2 = A1;
const uint8_t WavePin = A5;

// Period for which data is logged
const uint32_t PERIOD = 50000;

// Setup the parameters for size of file storage. Length of LINESPERBLOCK is limited to 12
// because of buffer length and number of characters in 1 line of logged data. Do not
// Change LINESPERBLOCK unless buffer length or file structure is also changed
const uint8_t LINESPERBLOCK = 12;
// NUMBLOCKS can be changed to alter the file length.
const uint16_t NUMBLOCKS = 4000;

// file system
SdFat sd;

// File to be written to
ofstream file;

// Two buffers (arrays of data) for storing blocks of data before writing to SD card
char buf0[256];
char buf1[256];

//Setup two buffer stream objects to make formatting our buffers easier
volatile obufstream bout0(buf0, sizeof(buf0));
volatile obufstream bout1(buf1, sizeof(buf1));

// We begin with no data written
volatile uint8_t lineCount = 0;
uint8_t blockCount = 0;
uint16_t wave = 0;
// Initialize to begin by writing to buffer 0
boolean bufSelect = 0;

// Serial output stream for testing
ArduinoOutStream cout(Serial);
//-----------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//-----------------------------------------------------------------------------

//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
    DateTime now = RTC.now();

  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(), now.day());

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
// format date/time
ostream& operator << (ostream& os, DateTime& dt) {
  os << dt.year() << '/' << int(dt.month()) << '/' << int(dt.day()) << ',';
  os << int(dt.hour()) << ':' << setfill('0') << setw(2) << int(dt.minute());
  os << ':' << setw(2) << int(dt.second()) << setfill(' ');
  return os;
}



void setup()
{
   // Set pin to be read as an input
  pinMode(WavePin, INPUT);
  wave = analogRead(WavePin);

  delay(5000);  // Allow circuit to power up

  // connect to RTC
  Wire.begin();
  if (!RTC.begin()) error("RTC failed");

  // set date time callback function
  SdFile::dateTimeCallback(dateTime);
  DateTime now = RTC.now();
  cout  << now << endl;

  // Setup the Sd card to write at half the max possible speed to avoid any errors
  // due to connection latency. If faster writing is required, try using
  // SPI_FULL_SPEED to replace SPI_HALF_SPEED
 
  if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED))
  {
    sd.initErrorHalt();
  }

  // Create a new file
  char fileName[] = "MyFile.CSV";
      file.open(fileName);
   
  // Make sure that the file has been opened correctly
  if (!file.is_open()) error("file.open");

  // Setup the timer to interrupt and log data every timer Period when it is restarted
  Timer1.initialize(PERIOD);
  Timer1.stop();
  Timer1.attachInterrupt(logPoint);

  // Add basic file information as a header in buffer
  bout1 << "Filename: " << fileName << endl;
  bout1 << "Start Time: " << millis() << "\n" << endl;

  // Restart the timer to begin datalogging
  Timer1.restart();

  // Finish writing header on screen and in buffer
  bout1 << "Time,Sens1,Sens2,Wave" << "\n" << endl;

  // Write the header into the file by flushing the buffer where header information was stored
  file << buf1 << flush;
}

void loop()
{
lineCount = 0;

 // Wait until one buffer is filled ip
 while(lineCount < LINESPERBLOCK)
 {
  if (millis() >= 1000000)
  {
    // Stop data logging if timeout occurs
    file.close();
    while(1) {}
  }
 }
 // Switch what buffer we are filling and keep count of number of blocks written
 blockCount++;
 bufSelect = !bufSelect;

  // Write the buffer we just filled onto the SD card and prepare the other buffer to be written to
 if(!bufSelect)
 {
   bout0.init(buf0, sizeof(buf0));
   file << buf1 << flush;
 }
 else
 {
  bout1.init(buf1, sizeof(buf1));
  file << buf0 << flush;
 }

 // Keep running the code until we hit the maximum file length
 if( blockCount < NUMBLOCKS )
 {
  return;
 }
 // When finished, wait.
 file.close();
 while(1) {}
}
/*
 * logPoint():
 *
 * Parameters: None
 * Returns: None
 */
void logPoint()
{
  // Collect all needed data for one point
  uint32_t t = millis();
  uint16_t s1 = analogRead(SensPin1);
  uint16_t s2 = analogRead(SensPin2);
  wave = analogRead(WavePin);

  lineCount++;

  // Write to whichever buffer is not currently being recorded on the SD card
  if(!bufSelect)
  {
    bout0 << t << ", " << s1 << ", " << s2 << ", " << wave << endl;
  }
  else
  {
    bout1 << t << ", " << s1 << ", " << s2 << ", " << wave << endl;
  }
}



Thank you.

el_supremo

On UNO and similar Arduinos, the I2C pins used for the DS1307 are pins A4 and A5. But you are using A5 as the square wave input as well.
Move the square wave pin to something that's not already being used by the RTC/uSD etc.

Pete
Don't send me technical questions via Private Message.

carluri


Go Up