Go Down

Topic: Need code to send block of data present in text file of sd card to arduino (Read 1 time) previous topic - next topic

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: [Select]
#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.

johnwasser

Unless your entire file is less than 32 characters long you should probably process the file one character at a time:
Code: [Select]

#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() {}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

ashritha

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.

johnwasser


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: [Select]

      digitalWrite(SS, LOW);
      SPI.transfer(c);
      digitalWrite(SS, HIGH);
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

ashritha

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

ashritha

Code: [Select]

#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?

PaulS

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.

Go Up