Show Posts
Pages: [1] 2 3 ... 109
1  Using Arduino / Storage / Re: how to read sd file line by line on: August 26, 2014, 08:12:54 pm
Looks like you are using SD.h.  SD.h is a wrapper for an ancient version of SdFat and lacks many of the features in modern versions of SdFat so you can't use fgets().
2  Using Arduino / Storage / Re: how to read sd file line by line on: August 25, 2014, 10:51:39 am
fgets() in SdFat reads a file by line.

Here is a demo program:
// Demo of fgets function to read lines from a file.
#include <SdFat.h>

// SD chip select pin
const uint8_t chipSelect = SS;

SdFat sd;

SdFile file;

// Maximum line length plus space for zero byte.
const size_t LINE_DIM = 50;

char line[LINE_DIM];
// store error strings in flash memory
#define error(s) sd.errorHalt_P(PSTR(s))
void setup(void) {
  size_t n;
  while (!Serial) {}  // Wait for Leonardo

  Serial.println(F("Type any character to start"));
  while ( <= 0) {}
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  if (!"TEST.TXT", O_READ)) error("open failed");
  int ln = 1;
  while ((n = file.fgets(line, sizeof(line))) > 0) {
      // Print line number.
      Serial.print(": ");
      if (line[n - 1] != '\n') {
        // Line is too long or last line is missing nl.
        Serial.println(F(" <-- missing nl"));
void loop(void) {}
Here is the test file with no nl character after the last line.
Lines to test fgets.

These lines must be
shorter than the buffer size.
The last line might be missing a new line.
Missing new line char

Here is the output:
Type any character to start
1: Lines to test fgets.
3: These lines must be
4: shorter than the buffer size.
5: The last line might be missing a new line.
6: Missing new line char <-- missing nl

3  Using Arduino / Storage / Re: How to get name of directory when directory name is unknown? SdFat on: August 23, 2014, 09:00:43 am
Here is a function that lists all directories on an SD.  You need to add what you need to find files in each directory.
#include <SdFat.h>
// SD chip select pin
const uint8_t chipSelect = SS;

// file system object
SdFat sd;
void printDirs(SdBaseFile* dir, uint8_t indent) {
  SdBaseFile subDir;
  // Indent for subdir level
  for (uint8_t i = 0; i < indent; i++) Serial.write(' ');

  // You could use bool getFilename(char* name) here to get the text.
  while (subDir.openNext(dir, O_READ)) {
    if (subDir.isDir()) printDirs(&subDir, indent + 3);
void setup() {
  while (!Serial) {} // wait for Leonardo
  if (!sd.begin(chipSelect)) sd.initErrorHalt();
  printDirs(sd.vwd(), 0);
void loop() {}

Here is a tree list of an SD done on a PC (quote is not spacing these listings correctly):
│   ├───DIR1A
│   └───DIR1B

Here is output from the above program

4  Using Arduino / Storage / Re: Try this super fast analog pin logger on: August 20, 2014, 08:04:11 am
Edit: I suspect using VirtualWire will cause this logger to fail, even if you modify it to use a different timer.  VirtualWire takes too long in its interrupt routine.

Timer1 is used to start the ADC conversion.  Only timer0 or timer1 can be used to trigger the ADC but timer0 is used for mills() and micros().

Timer2 is free but a mod to the VirtualWire library would be required.  This is probably not too difficult since VirtualWire runs on ATtiny85 using timer0 which is 8-bits.  I believe timer2 has all the required features.

If you can live with logging at 500 Hz or less, the new SdFat has a logger that does not use timer1, the LowLatencyLogger example.

The new library is here

Using VirtialWire with any fast logger may cause problems.
5  Using Arduino / Storage / Re: Program Stuck at SD.begin(4) on: August 19, 2014, 09:06:46 am
Do you have anything in loop() ?   loop() will execute even if the SD fails.

Try replacing these  return statements in startup() with  a forever loop.  And add a Serial.flush.  This won't solve the problem but may allow the entire message to print.
if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
       for (;;);    // init failed  <-----------------------------------forever loop
    Serial.println("SUCCESS - SD card initialized.");
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        for (;;);  // can't find index file <---------------------------forever loop
    Serial.println("SUCCESS - Found index.htm file.");
    Serial.flush();  // <-------------------------------------------flush serial print data

6  Using Arduino / Storage / Re: Using SdFat with String on: August 18, 2014, 12:03:31 pm
The following will get the file name for myFile and place it in an Arduino String.
  SdFile myFile;
  char tmp[13];

  // code to open mfFile goes here

  String name = tmp;

  // verify that the file name is in the String.

The output for a test case is:
7  Development / Other Software Development / RTOS library updates for ChibiOS/RT, FreeRTOS, and NilRTOS on: August 16, 2014, 02:22:30 pm
I have posted new versions of the following RTOS libraries on GitHub.

ChibiOS/RT version 2.6.5

FreeRTOS version 8.0.1

NilRTOS slight update to original version 0.0.1

I am following work on ChibiOS version 3.0.  Version 3.0 will include many new features and NilRTOS will be more compatible with ChibiOS.
8  Using Arduino / Storage / Re: Which microSD cards to use for low current & low power consumption? on: August 16, 2014, 10:46:24 am
I want to emphasize again how important the data logging program is in terms of power use.

The above traces are for the dataLogger example. Each record has the ASCII values of four analog pins in CSV format and records are logged at 5 Hz.  Sync is called after each record so the above trace is repeated five times per second.  Each SD access involves reading the current data block, updating the data block, writing the data block, reading the directory entry, updating the directory entry, and writing it back to the SD.

I removed the sync() call and ran the example again.  Now access to the SD will only happen when the 512 byte cache block is full.

The size of a record is about 20 bytes so now the SD card is accessed about once every five seconds, not five times per second.  Most accesses only involve a single block write.

I estimate the method with sync uses over 50 times as much power.  You could call sync every few seconds and save lots of power but still prevent loss of all but a few seconds of data.

Here is a trace of a write to the old card with no sync.  Note how much shorter the SD access is.

The card draws about 20 ma for 1.5 ms once every 5000 ms.  The average current will be close to the sleep current.

Even the example with sync() only draws high current for about 4 ms every 200 ms so the average is not over a milliamp.
9  Using Arduino / Storage / Re: SD.write taking 3 seconds on: August 16, 2014, 09:01:02 am
Does the file get created by the open?

The first write may need to allocate a cluster.  This can cause a scan of the FAT, File Allocation Table, and can take time to find the first free cluster if your card has lots of data.

If you formatted the card with a small cluster size it can take a very long time.  Be sure to use SD Formatter, not an OS utility.
10  Using Arduino / Storage / Re: Which microSD cards to use for low current & low power consumption? on: August 16, 2014, 08:15:05 am
Looks like pito is correct, old cards use less power.

I did some tests with two cards, an older SanDisk 1 GB SD and a new SanDisk 16 GB high end microSDHC.

Manufacturer ID: 0X3
Product: SE16G
Version: 8.0
Serial number: 0X3752B221
Manufacturing date: 10/2013

cardSize: 15931.54 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true

Manufacturer ID: 0X3
Product: SD01G
Version: 8.0
Serial number: 0XFB5C4F70
Manufacturing date: 12/2006

cardSize: 1015.81 MB (MB = 1,000,000 bytes)
flashEraseSize: 32 blocks
eraseSingleBlock: true

I ran the new dataLogger example in the 20140806 version of SdFat

 I used a Teensy 3.1 so there would be no level shifters or regulators.

First I put a 4.7 ohm resistor in the ground line and looked at the voltage on a scope.  See the three attached traces. The scale is 100 mv/div so with the 4.7 ohm resistor, the scale is about 21 ma/div.

There are two views of the new trace,  the full trace at 2 ms/div and a magnified portion at 500 us/div.  Notice that the current stays higher, about 10 ma for about 6 extra ms.

If you look at the two 500 us/div traces, the write current in the trace is higher for the new card.

Edit: I added a third version of the new trace at 2 usec/div for fun.

I also put a handheld DMM in the 3.3V line to check idle current.  I found that the cards went through at least two states.  The new card immediately drops to about 500 uamp then sleeps at about 70 uamp .  The old card immediately drops to about 200 uamp and sleeps at 60 uamp.

The dataLogger example is a current hog since it calls sync() after every data point.  That will cause at least two reads and two writes for each data point.  If you remove the sync calls after each point, the power use will drop a huge amount.
11  Using Arduino / Storage / Re: Which microSD cards to use for low current & low power consumption? on: August 15, 2014, 12:05:51 pm
Any experience with the old Nokia  microSD cards?

I have not tested Nokia cards but in that era camera and phone companies slapped there brand on cards made by some other company.

Here a "Canon card" is really a Panasonic card:
Manufacturer ID: 0X1
Product: S016B
Version: 4.5
Serial number: 0X35C4B589
Manufacturing date: 9/2005

Run the SdInfo example, often it will give you a clue about what you really have.
12  Using Arduino / Storage / Re: SdCard - reverse engineered on: August 15, 2014, 11:27:39 am
Wow, you don't give up easily.

The "spooks" like NSA and CIA have forensic programs that reconstruct file systems almost automatically.

When I was a mint PhD physicist, I did some work on hydrodynamic modeling programs at a "weapons lab".  When I finished, they shredded my computer.  With people like you around, I see why.

My work was to support the modeling of weapons after the Underground Test Ban.  Before the ban, some computers including the keyboard were put underground with the test device.  Bet that would stop you.

13  Using Arduino / Storage / Re: Which microSD cards to use for low current & low power consumption? on: August 15, 2014, 11:11:32 am
You may read the datasheet and then find you card is not even close.  This happened to me recently when my SanDisk card turned out to be counterfeit   Your article about fakes is true, I now keeps a collection of fakes where I have been ripped off.

This was from an Amazon seller.  If you buy an older smaller card now, the chances are very high the card will be fake.  Forget it on ebay it's a coin toss.  I don't think many sellers know they are pushing fake cards.

Major manufactures no longer produce standard, 2 GB or less, cards so the market for these is full of fakes.

Most cards are now used in phones and power consumption is becoming a bigger issue.  I recently bought about a dozen microSDHC cards  I should test this batch for power use.

Once again, the datasheet won't tell you much for use on Arduino.  I recently did tests with cards on SDIO using large multi-block writes on a high end micro.  The performance is totally different than on SPI with the same card.  I get read/write speeds of 20 MB/sec on SDIO so there can't be much extra flash programming.
14  Using Arduino / Storage / Re: Which microSD cards to use for low current & low power consumption? on: August 14, 2014, 06:02:53 pm
You will never find a simple answer.  The amount of power used depends on how much flash is programmed.

The amount of flash programming depends on the flash controller and your write pattern.  Modern cards have very large pages, often some multiple of 16 KB.

When you write a single 512 byte block with a single block write command, the entire flash page must be programmed so the controller must move existing data to a new flash page.  Wear leveling algorithms also cause extra data movement.

I could continue on forever but the short answer is SD cards were not designed for Arduino use.  The amount of flash programming in the card is much greater than the amount you write.

I have tested many cards and there is not a "best" card for all applications.

You need to do testing with your application.

Another key facror in power usage is card idle current.  SD.h has a bug that can prevent cards from sleeping in low power mode.

I wrote the old version of SdFat used in SD.h and it has not been updated since I fixed the bug.
15  Using Arduino / Storage / Re: Teensy 3.1 / Data Logging / SD Card / Latency / Non-Blocking SDFAT Library on: August 11, 2014, 06:23:03 pm
QuickStart has this warning:
// Normally the SdFat class is used in applications in place
// of Sd2Card, SdVolume, and SdFile for root.
// These internal classes are used here to diagnose problems.
Sd2Card card;
SdVolume volume;
SdFile root;

SdInfo uses these because it is also intended to diagnose problems.  It must work with an SD card that is not formatted.

Look at something like the ReadWriteSdFat example.
Pages: [1] 2 3 ... 109