如何在arduino中从SD卡创建变量

时间:2016-03-09 23:13:39

标签: c++ arduino arduino-uno

我已将一个号码保存到名为CONFIG.BAT的文件中的SD卡上,它是一个电话号码,格式为" + 441234567890",在串口监视器中我可以打印出来使用

  myFile = SD.open("CONFIG.DAT");

  if (myFile) {

Serial.println("CONFIG.DAT:");

// read from the file until there's nothing else in it:
while (myFile.available()) {

      Serial.write(myFile.read());

}

我得到了这个回应

Initializing SD card...initialization done.
CONFIG.DAT:
+441234567890

我想要做的是将这个数字放入变量" telNo",我试图使用

telNo = (myFile.read());

我得到的回应是

Initializing SD card...initialization done.
CONFIG.DAT: 
10

我不确定这是什么" 10"表示。

我也试过" concat"

while (myFile.available()) {

        number.concat(myFile.read());      

}

我得到的回复是数字的十进制代码,最后有一些数字我不明白

Initializing SD card...initialization done.
CONFIG.DAT: 
48555653515353555354561310

最后四个数字(1310)我不明白

我想要实现的是" telNo = CONFIG.DAT文件中的电话号码,所以如果任何人可以提供帮助我会很感激

这是我的完整草图

#include <SoftwareSerial.h>
//#include <SD.h>
#define SD_CS_PIN SS
#include <SPI.h>
#include "SdFat.h"

SdFat SD;

File myFile;

//SIM800 TX is connected to Arduino D8
#define SIM800_TX_PIN 8

//SIM800 RX is connected to Arduino D7
#define SIM800_RX_PIN 7

//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);


//const int ledPin3 = 11;//Define the Bluetooth led pin
const int ledPin2 = 2;//Define the interupt pin to signify bluetooth connect or disconnect

String readString = "";
String telNoString = "";
String number = "";

volatile int ledonState = 0;
int lastLedonState = 0;


const int thresholdvalue=680;//The threshold to turn the led on


void setup() {
  pinMode (ledPin2, INPUT);//set input for interupt pin
  //pinMode (ledPin3, OUTPUT);//set output for bluetooth pin

  attachInterrupt(0, pin_ISR, CHANGE);

  Serial.begin(9600);

  while(!Serial);

  //Being serial communication witj Arduino and SIM800
  serialSIM800.begin(9600);
  delay(1000);

  SysCall::yield();

  Serial.println("Setup Complete!");

}


void pin_ISR() {
  ledonState = digitalRead(ledPin2);

if (ledonState != lastLedonState) {

  if (ledonState == HIGH) {
        //digitalWrite(ledPin3, HIGH);//turn led on
            Serial.println("HC-05 is now connected");
            //Serial.println();
      }else{
         //digitalWrite(ledPin3, LOW);//turn led off
             Serial.println("HC-05 is now Disconnected");
             //Serial.println();
  }
lastLedonState = ledonState;
}
}


void sound_detect(){

  int sensorValue = analogRead(A0);//use A0 to read the electrical signal

  if(sensorValue > thresholdvalue) {

//  digitalWrite(ledPin1,HIGH);//if the value read from A0 is larger than 400,then light the LED
//  delay(10);
//  digitalWrite(ledPin1,LOW);

 Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.println("initialization done.");  

  if (!SD.exists("CONFIG.DAT")) {

    Serial.println("No Number Exists! Please go to Setup Device to add Number for Alert");

  }else{

  // open the file for reading:
  myFile = SD.open("CONFIG.DAT");

  if (myFile) {

    Serial.println("CONFIG.DAT:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {

            number.concat(myFile.read());      
          //number = (myFile.read());
          //Serial.write(myFile.read());

    }

      //Serial.println(myFile);    

    // close the file:
    myFile.close();

  } else {

    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");

  }

  }


//    //Set SMS format to ASCII
//  serialSIM800.write("AT+CMGF=1\r\n");
//  delay(1000);
// 
//  //Send new SMS command and message number
//  serialSIM800.write("AT+CMGS=\"+44"+number+"\"\r\n");
//  delay(1000);
//   
//  //Send SMS content
//  serialSIM800.write("TEST SMS NOISE DETECT");
//  delay(1000);
//   
//  //Send Ctrl+Z / ESC to denote SMS message is complete
//  serialSIM800.write((char)26);
//  delay(1000);
//     
//  Serial.println("SMS Sent!");
Serial.println("this is the phone number: ");
Serial.print(number);

}

}



void insertNo(){

        while(Serial.available()==0) { // Wait for User to Input Data  
  }

telNoString = Serial.readString();


  Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.println("initialization done.");

  if (SD.exists("CONFIG.DAT")) {

  SD.remove("CONFIG.DAT");
  }

  if (!SD.exists("CONFIG.DAT")) {

  myFile = SD.open("CONFIG.DAT", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to CONFIG.DAT...");

    myFile.println(telNoString);

    // close the file:
    myFile.close();

    Serial.println("done.");

  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");
  } 

  // re-open the file for reading:
  myFile = SD.open("CONFIG.DAT");

  if (myFile) {

    Serial.println("CONFIG.DAT:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {

      Serial.write(myFile.read());

    }

    // close the file:
    myFile.close();

  } else {

    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");

  }     

  }
   return;    
}


void loop() {

 while (Serial.available()) {
          delay(10);       
          char c = Serial.read();
          readString  += c;
 }

    if(readString == "setup device") {

      Serial.println(readString);
      readString = "";



insertNo();

    }else{
      if(readString == "start device") {

      Serial.println(readString);
      readString = "";


    }
 sound_detect();   
    }       
}

注释掉的代码部分是发送短信息的AT命令。

2 个答案:

答案 0 :(得分:1)

telNo = (myFile.read());编译了吗? telNo的类型是什么?如果它是int或其他一些数字,则您无法获得所需的结果,因为read()读取字符而非数字。您看到的10是ascii代码对于newline字符(&#39; \ n&#39;)这是文件中的第一个字符(这就是为什么你的号码低于CONFIG.DAT:)。

read()一次读取一个字节,如果没有,则返回-1。要获取整个内容,您应该使用循环一次读取一个字符并将这些字符附加到char[]

unsigned int MAX_SIZE = 100; 
char string[MAX_SIZE];
unsigned int index = 0;
char next_char;
while (next_char = myfile.read()) string[index++] = next_char;
string[index] = '/0'; // terminate with the null character

现在,如果您真的需要int,则需要过滤掉newline+和任何其他无效数字,然后将您的号码转换为{{1 }}:

int

答案 1 :(得分:0)

我想我找到了如下答案

// read from the file until there's nothing else in it:
while (myFile.available()) {

            char ltr = myFile.read();
            number += ltr;      

} 

这是在Text file on SD to String

找到的