Saturday 8 August 2020

Arduino SD Card Module , to read SD card data then led on

 IncomingByte

https://www.arduino.cc/reference/en/language/functions/communication/serial/available/

upload below sketch then Serial Monitor

SD card text.txt file Example 88 then Serial Monitor to check will show56 

    // read from the file until there's nothing else in it:

    while (myFile.available()) {

      Serial.write(myFile.read());

    incomingByte = myFile.read();

    Serial.println(incomingByte,DEC);

    if (incomingByte == 56) {

    digitalWrite(ledG, HIGH);

    delay(10000);

    digitalWrite(ledG, LOW);

    delay(10000);

    }








/*SD卡

CS   --> to Arduino pin4 

SCK  --> to Arduino pin13

MOSI --> to Arduino pin11

MISO --> to Arduino pin12

VCC  --> to Arduino 5V

GND  --> to Arduino GND

*/

#include <SPI.h>

#include <SD.h>

File myFile;

String filename = "test.txt";     //要寫入的檔案名稱

int i = 0;

int ledG = 7; // 綠燈 正極 --> pin 7,  負極 --> GND 

int ledR = 8; // 紅燈 正極 --> pin 8,  負極 --> GND 

int incomingByte = 0;


void setup() {

  pinMode(ledR, OUTPUT); 

  pinMode(ledG, OUTPUT);

  pinMode(10,OUTPUT);    //保留pin10, SD Library需要使用

  

  Serial.begin(9600);

  while (!Serial) {}


  digitalWrite(ledR, HIGH);

  Serial.print("Initializing SD card...");  //SD卡初始化

  if (!SD.begin(4)) {

    Serial.println("initialization failed!");

    return;

  }

  Serial.println("initialization done.");

}


void loop()

{

  digitalWrite(ledG, HIGH);

  digitalWrite(ledR, LOW);


  /*


   此範圍可以加入其他程式碼



  */


  delay (3000);



  for (int i=0; i <= 10; i++){   //燈號警告,此段時間可以關閉電源、再拔除SD卡。

    digitalWrite(ledG, HIGH);

    delay(100);

    digitalWrite(ledG, LOW);

    delay(100);

  } 

  digitalWrite(ledR, HIGH);     //下面的程式是寫入SD卡,此段時間內請勿關閉電源、拔取SD卡。


  myFile = SD.open(filename, FILE_WRITE);

  if (myFile) {

    Serial.print("Writing to test.txt...");

    //myFile.print(millis());                   //寫入SD卡的時間,單位是1/1000秒。

    //myFile.print(",");

    

    //myFile.print("3");    //要寫入SD卡數據時,這一行請刪掉。

    

    //myFile.print(0);                  //要寫入SD卡數據時,請以逗號分隔數據。

    //myFile.print(",");

    //myFile.print(1);

    //myFile.print(",");

    //myFile.print(2);

    // ...

    //myFile.println();


    // close the file:

    myFile.close();

    Serial.println("done.");

  } else {

    // if the file didn't open, print an error:

    Serial.println("error opening test.txt");

  }

  // re-open the file for reading:

  myFile = SD.open(filename);

  if (myFile) {

    Serial.println("test.txt:");


    // read from the file until there's nothing else in it:

    while (myFile.available()) {

      Serial.write(myFile.read());

    incomingByte = myFile.read();

    Serial.println(incomingByte,DEC);

    if (incomingByte == 56) {

    digitalWrite(ledG, HIGH);

    delay(10000);

    digitalWrite(ledG, LOW);

    delay(10000);

    }

    

    }

    // close the file:

    myFile.close();

  } else {

    // if the file didn't open, print an error:

    Serial.println("error opening test.txt");

  }

  delay (3000);

}








ANOTHER Sketch


/*SD卡

CS   --> to Arduino pin4 

SCK  --> to Arduino pin13

MOSI --> to Arduino pin11

MISO --> to Arduino pin12

VCC  --> to Arduino 5V

GND  --> to Arduino GND

*/

#include <SPI.h>

#include <SD.h>

File myFile;

String filename = "test.txt";     //要寫入的檔案名稱

int i = 0;

int ledG = 8; // 綠燈 正極 --> pin 7,  負極 --> GND 

int ledR = 7; // 紅燈 正極 --> pin 8,  負極 --> GND 

int incomingByte = 0;


void setup() {

  pinMode(ledR, OUTPUT); 

  //pinMode(ledG, OUTPUT);

  pinMode(10,OUTPUT);    //保留pin10, SD Library需要使用

  

  Serial.begin(9600);

  while (!Serial) {}


  digitalWrite(ledR, HIGH);

  Serial.print("Initializing SD card...");  //SD卡初始化

  if (!SD.begin(4)) {

    Serial.println("initialization failed!");

    return;

  }

  Serial.println("initialization done.");

}


void loop()

{

  //digitalWrite(ledG, HIGH);

  digitalWrite(ledR, LOW);


  /*


   此範圍可以加入其他程式碼



  */


  delay (3);



  for (int i=0; i <= 10; i++){   //燈號警告,此段時間可以關閉電源、再拔除SD卡。

    //digitalWrite(ledG, HIGH);

    delay(100);

    //digitalWrite(ledG, LOW);

    delay(100);

  } 

  digitalWrite(ledR, HIGH);     //下面的程式是寫入SD卡,此段時間內請勿關閉電源、拔取SD卡。


  myFile = SD.open(filename, FILE_WRITE);

  if (myFile) {

    Serial.print("Writing to test.txt...");

    //myFile.print(millis());                   //寫入SD卡的時間,單位是1/1000秒。

    //myFile.print(",");

    

    //myFile.print("3");    //要寫入SD卡數據時,這一行請刪掉。

    

    //myFile.print(0);                  //要寫入SD卡數據時,請以逗號分隔數據。

    //myFile.print(",");

    //myFile.print(1);

    //myFile.print(",");

    //myFile.print(2);

    // ...

    //myFile.println();


    // close the file:

    myFile.close();

    Serial.println("done.");

  } else {

    // if the file didn't open, print an error:

    Serial.println("error opening test.txt");

  }

  // re-open the file for reading:

  myFile = SD.open(filename);

  if (myFile) {

    Serial.println("test.txt:");

pinMode(ledG, OUTPUT);

    // read from the file until there's nothing else in it:

    while (myFile.available()) {

      Serial.write(myFile.read());

    incomingByte = myFile.read();

    Serial.println(incomingByte,DEC);

    Serial.println(incomingByte,DEC);

hello:    

    if (incomingByte == 57) {

    digitalWrite(ledG, HIGH);

    delay(10000);

    goto hello;

    }

    

    }

    // close the file:

    myFile.close();

  } else {

    // if the file didn't open, print an error:

    Serial.println("error opening test.txt");

  }

  

}

No comments:

Post a Comment