Lesson 30: How to Read and Write to SD Card

 

Written by Jonathan Sim

You can find this lesson and more in the Arduino IDE (File -> Examples -> Andee). If you are unable to find them, you will need to install the Andee Library for Arduino IDE.

Works on iOS and Android
Annikken Andee allows you to easily read and write data onto an SD card. This is very useful especially when you have a lot of information to handle or when you need some variable information to be retained even after the Arduino x Annikken Andee is powered off.

Here's how!

Contents

Top of the Code

#include <SPI.h>
#include <Andee.h>

AndeeHelper btnWriteContent;
AndeeHelper displaybox;
AndeeHelper btnResetOffset;
AndeeHelper btnReadContent;

// Essentials for Read/Write to SD Card
char filename[] = "FILENAME.TXT"; // String to store the name of file
char errorMessage[64]; // String to store error message
int offset = 0; // Position of where to read/write data in the file

// Essentials for Writing to SD Card
char dataToWrite[150]; // String to store the data for each line before writing

// Essentials for Reading SD Card
char dataRead[64]; // String to store the data for each line before writing
char delimiter[] = "\n"; // What character to detect as the end of each line
int maxLength = 64; // Maximum length to read from each line
/* You'll need to specify both the maximum length of characters per 
   line and the delimiter. readLineFromSD() will read stop reading in 
   a line when it reaches the maximum length OR the delimiter, depending
   on which it encounters first. */

setup()

void setup()
{
  Andee.begin();  // Setup communication between Annikken Andee and Arduino
  Andee.clear();  // Clear the screen of any previous displays
  setInitialData(); // Define object types and their appearance
}

setInitialData()

We'll create a user interface that looks like this to demonstrate the read/write functions:

Lesson30.png


void setInitialData()
{
  btnWriteContent.setId(0);
  btnWriteContent.setType(KEYBOARD_IN); // Defines object as a text input button
  btnWriteContent.setKeyboardType(ALPHA_NUMERIC); // Defines the keyboard type to use
  btnWriteContent.setLocation(0,0,FULL); // First row, left-most, full width
  btnWriteContent.setTitle("Write to SD Card");
  btnWriteContent.setColor(THEME_DEFAULT_DARK);
  
  displaybox.setId(1);
  displaybox.setType(DATA_OUT);
  displaybox.setLocation(1,0,FULL);
  displaybox.setTitle("Read/Write SD Card Status");

  btnResetOffset.setId(2);
  btnResetOffset.setType(BUTTON_IN);
  btnResetOffset.setLocation(2,0,FULL);
  btnResetOffset.setTitle("Return to Start of File");
  btnResetOffset.setColor(THEME_DEFAULT_DARK);
  
  btnReadContent.setId(3);
  btnReadContent.setType(BUTTON_IN);
  btnReadContent.setLocation(3,0,FULL);
  btnReadContent.setTitle("Read Next Line");
  btnReadContent.setColor(THEME_DEFAULT_DARK);
}

loop()

void loop()
{
  // This is how you write data to an SD Card
  if( btnWriteContent.isPressed() )
  {
   // Show on-screen keyboard to get user input and store in "dataToWrite" 
   btnWriteContent.getKeyboardMessage(dataToWrite);
  
   // Write data to SD card
   offset = Andee.writeSD(filename, dataToWrite, offset, errorMessage);
   offset = Andee.writeSD(filename, "\n", offset, errorMessage); 
   // Insert a next line character
  
   // If there is an error
   if(offset == -1)
   {
     // Show error message on another display box
     displaybox.setData(errorMessage); 
     displaybox.update();
   }
   else
   {
     displaybox.setData("Data Successfully Written!"); 
     displaybox.update();
   }
  
   // Tell smartphone that button press message has been received
   btnWriteContent.ack(); 
  }
  
  // This is how you read data
  if( btnReadContent.isPressed() )
  {
   // Read data from SD card and store it in a string
   offset = Andee.readLineFromSD(filename, offset, dataRead, maxLength, delimiter);
  
   // Display the data from SD card onto a display box
   displaybox.setData(dataRead);
   displaybox.update();
  
   // Tell smartphone that button press message has been received
   btnReadContent.ack(); 
  }  
  
  if( btnResetOffset.isPressed() )
  {
    btnResetOffset.ack();
    offset = 0;
    displaybox.setData("Offset Reset to Zero");
    displaybox.update();
  }
  
  displaybox.update();
  btnResetOffset.update();
  btnWriteContent.update();
  btnReadContent.update();
  
  delay(500); // Always leave a short delay for Bluetooth communication
}
comments powered by Disqus