Pages: [1]   Go Down
Author Topic: Need code to send block of data present in text file of sd card to arduino  (Read 1131 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.
    i need to do two works.

     1 Read a text file from sd card and send to arduino so that,  2. I can send those data to other arduino board which is a slave to first arduino

Code:
#include <SD.h>
#include <SPI.h>

char c[32];
int i = 0;
File file;

void setup()
{
  Serial.begin(57600);    //Begin serial communication
  pinMode (10,OUTPUT);    //Set pin 53 as output for SD lib
  if (!SD.begin(9)){     //Check that SD card is initialized
    Serial.println("SD not initialized");
  }
  else {
    Serial.println("SD initialized");
  }
  file = SD.open("logdata.txt");  //Open file
  if(file) {
    Serial.println("File opened");  
    while (file.available()>0) {
      c[i]=file.read();
      i=i+1;
    }
    Serial.print(c);


void loop()
{
  delay(100);
}


I need a code that transfers data from sd card to arduino


Thank you.
« Last Edit: September 13, 2013, 03:00:16 am by chinni4292 » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8096
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unless your entire file is less than 32 characters long you should probably process the file one character at a time:
Code:
#include <SD.h>
#include <SPI.h>

File file;

void setup() {
  Serial.begin(57600);    //Begin serial communication
  SPI.begin();
  pinMode (10,OUTPUT);
  if (!SD.begin(9)) {     //Check that SD card is initialized
    Serial.println("SD not initialized");
    return;
  }
  else {
    Serial.println("SD initialized");
  }
  file = SD.open("logdata.txt");  //Open file
  if(file) {
    Serial.println("File opened"); 
    while (file.available()) {
      char c = file.read();
      Serial.write(c);
      digitalWrite(SS, LOW);
      SPI.transfer(c);
      digitalWrite(SS, HIGH);
    }
  }
  else
    Serial.println("File failed to open"); 
}

void loop() {}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you.
I think your code does my first half of my work...i really appreciate if u give any hint how to send data(which is read from text file) from sd card to arduino using spi.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8096
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think your code does my first half of my work...i really appreciate if u give any hint how to send data(which is read from text file) from sd card to arduino using spi.

That's this part:
Code:
      digitalWrite(SS, LOW);
      SPI.transfer(c);
      digitalWrite(SS, HIGH);
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok thanks
how to send this data to another arduino which is a slave
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <SD.h>
#include <SPI.h>

File file;
const int chipSelect = 10;  /*for arduino*/
const int ss = 9; /*for sdcard*/

void setup() {
  Serial.begin(57600);    //Begin serial communication
  SPI.begin();
  pinMode (10,OUTPUT);
  if (!SD.begin(9)) {     //Check that SD card is initialized
    Serial.println("SD not initialized");
    return;
  }
  else {
    Serial.println("SD initialized");
  }
  file = SD.open("logdata.txt");  //Open file
  if(file) {
    Serial.println("File opened"); 
    while (file.available()) {
      char c = file.read();
      Serial.write(c);
      digitalWrite(SS, LOW);
      SPI.transfer(c);
      digitalWrite(SS, HIGH);
    }
  }
  else
    Serial.println("File failed to open"); 
}



void loop() {

digitalWrite(chipselect, HIGH);  // ensure SS stays high

  // Put SCK, MOSI, SS pins into output mode
  // also put SCK, MOSI into LOW state, and SS into HIGH state.
  // Then put SPI hardware into Master mode and turn SPI on
  SPI.begin ();

  delay (5000);  // 5 seconds delay to start logic analyser.

  char c;
 
  // enable Slave Select
  digitalWrite(chipselect, LOW);    // SS is pin 10
 
  // send test string
  for (const char * p = "h/n" ; c = *p; p++)
    SPI.transfer (c);

 // disable Slave Select
 digitalWrite(chipselect, HIGH);

 // turn SPI hardware off
 SPI.end ();
 
 while (1);  //loop

}


is this correct?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46204
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is this correct?
If it works, it is. If it doesn't it isn't. It isn't rocket science to tell the difference.
Logged

Pages: [1]   Go Up
Jump to: