Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 我的代码没有';I don’我不能进入if循环将数据输入我的SD卡,为什么会这样?_Arduino - Fatal编程技术网

Arduino 我的代码没有';I don’我不能进入if循环将数据输入我的SD卡,为什么会这样?

Arduino 我的代码没有';I don’我不能进入if循环将数据输入我的SD卡,为什么会这样?,arduino,Arduino,我不明白为什么我的代码没有写入SD卡。我尝试了各种其他方法来获取卡上的数据。代码没有进入页面底部的IF循环。可能是我输入了错误的pin以获取数据吗?我使用Arduino mega上的引脚RX3 15获取数据?该插脚会被视为插脚63吗?我在网上看了一张图表,上面是这么说的 #include <SD.h> #include <SPI.h> #include <Wire.h> File mySensorData; int chipSelect = 63; St

我不明白为什么我的代码没有写入SD卡。我尝试了各种其他方法来获取卡上的数据。代码没有进入页面底部的IF循环。可能是我输入了错误的pin以获取数据吗?我使用Arduino mega上的引脚RX3 15获取数据?该插脚会被视为插脚63吗?我在网上看了一张图表,上面是这么说的

#include <SD.h>
#include <SPI.h>
#include <Wire.h>
File mySensorData;
int chipSelect = 63; 


String inputstring = "";                              //a string to hold incoming data from the PC
String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false;                //have we received all the data from the PC
boolean sensor_string_complete = false;               //have we received all the data from the Atlas Scientific product


void setup() {    //set up the hardware
  SD.begin(chipSelect);
  pinMode(10, OUTPUT);

  Serial.begin(9600);                                 //set baud rate for the hardware serial port_0 to 9600
  Serial3.begin(9600);                                //set baud rate for software serial port_3 to 9600
  inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
  sensorstring.reserve(30);                           //set aside some bytes for receiving data from Atlas Scientific product
}


void serialEvent() {                                  //if the hardware serial port_0 receives a char
  inputstring = Serial.readStringUntil(13);           //read the string until we see a <CR>
  input_string_complete = true;                       //set the flag used to tell if we have received a completed string from the PC
}


void serialEvent3() {                                 //if the hardware serial port_3 receives a char
  sensorstring = Serial3.readStringUntil(13);         //read the string until we see a <CR>
  sensor_string_complete = true;                      //set the flag used to tell if we have received a completed string from the PC
}


void loop() {                                         //here we go...


  if (input_string_complete == true) {                //if a string from the PC has been received in its entirety
    Serial3.print(inputstring);                       //send that string to the Atlas Scientific product
    Serial3.print('\r');                              //add a <CR> to the end of the string
    inputstring = "";                                 //clear the string
    input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
  }

  if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
    if (isdigit(sensorstring[0]) == false) {          //if the first character in the string is a digit
      Serial.println(sensorstring);                   //send that string to the PC's serial monitor
    }
    else                                              //if the first character in the string is NOT a digit
    {
      print_EC_data();                                //then call this function
    }
    sensorstring = "";                                //clear the string
    sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  }
}


void print_EC_data(void) {                            //this function will pars the string

  char sensorstring_array[30];                        //we make a char array
  char *EC;                                           //char pointer used in string parsing
  char *TDS;                                          //char pointer used in string parsing
  char *SAL;                                          //char pointer used in string parsing
  char *GRAV;                                         //char pointer used in string parsing
  float f_ec;                                         //used to hold a floating point number that is the EC

  sensorstring.toCharArray(sensorstring_array, 30);   //convert the string to a char array
  EC = strtok(sensorstring_array, ",");               //let's pars the array at each comma
  TDS = strtok(NULL, ",");                            //let's pars the array at each comma
  SAL = strtok(NULL, ",");                            //let's pars the array at each comma
  GRAV = strtok(NULL, ",");                           //let's pars the array at each comma

if(mySensorData){
  Serial.print("EC:");                                //we now print each value we parsed separately
  Serial.println(EC);                                 //this is the EC value

  Serial.print("TDS:");                               //we now print each value we parsed separately
  Serial.println(TDS);                                //this is the TDS value

  Serial.print("SAL:");                               //we now print each value we parsed separately
  Serial.println(SAL);                                //this is the salinity value

  Serial.print("GRAV:");                              //we now print each value we parsed separately
  Serial.println(GRAV);                               //this is the specific gravity
  Serial.println();                                   //this just makes the output easier to read


mySensorData= SD.open("CPData", FILE_WRITE);
mySensorData.print(EC);
mySensorData.close();
}
}
#包括
#包括
#包括
文件mySensorData;
int-chipSelect=63;
字符串inputstring=“”//用于保存来自PC的传入数据的字符串
字符串传感器字符串=”//用于保存Atlas科学产品数据的字符串
布尔输入\字符串\完成=假//我们是否已收到来自PC的所有数据
布尔传感器字符串完成=假//我们是否收到了Atlas科学产品的所有数据
void setup(){//设置硬件
SD.begin(芯片选择);
pinMode(10,输出);
Serial.begin(9600);//将硬件串行端口0的波特率设置为9600
Serial3.begin(9600);//将软件串行端口_3的波特率设置为9600
inputstring.reserve(10);//留出一些字节用于从PC接收数据
sensorstring.reserve(30);//留出一些字节用于从Atlas Scientific product接收数据
}
void serialEvent(){//如果硬件串行端口_0接收到字符
inputstring=Serial.readStringUntil(13);//读取字符串直到看到
input_string_complete=true;//设置用于指示是否已从PC收到已完成字符串的标志
}
void serialEvent3(){//如果硬件串行端口_3接收到字符
sensorstring=Serial3.readStringUntil(13);//读取字符串直到看到
sensor_string_complete=true;//设置用于指示我们是否已从PC收到完整字符串的标志
}
void loop(){//我们开始。。。
if(input_string_complete==true){//如果从PC接收到完整的字符串
Serial3.print(inputstring);//将该字符串发送到Atlas Scientific产品
Serial3.print('\r');//在字符串末尾添加一个
inputstring=”“;//清除该字符串
input_string_complete=false;//重置用于指示我们是否已从PC收到完整字符串的标志
}
if(sensor_string_complete==true){//如果已收到Atlas Scientific产品的全部字符串
if(isdigit(sensorstring[0])==false){//如果字符串中的第一个字符是数字
Serial.println(sensorstring);//将该字符串发送到PC的串行监视器
}
else//如果字符串中的第一个字符不是数字
{
print_EC_data();//然后调用此函数
}
sensorstring=“;//清除该字符串
sensor_string_complete=false;//重置用于告知我们是否已从Atlas Scientific产品收到完整字符串的标志
}
}
void print_EC_data(void){//此函数将对字符串进行pars
char sensorstring_数组[30];//我们制作一个char数组
char*EC;//字符串解析中使用的char指针
char*TDS;//字符串解析中使用的char指针
char*SAL;//字符串解析中使用的字符指针
char*GRAV;//字符串解析中使用的char指针
float f_ec;//用于保存作为ec的浮点数
toCharArray(sensorstring_数组,30);//将字符串转换为字符数组
EC=strtok(sensorstring_数组,“”;//让我们在每个逗号处对数组进行PAR
TDS=strtok(NULL,“”;//让我们在每个逗号处对数组进行PAR
SAL=strtok(NULL,“”;//让我们在每个逗号处对数组进行PAR
GRAV=strtok(NULL,“”;//让我们在每个逗号处对数组进行PAR
if(mySensorData){
Serial.print(“EC:”;//我们现在分别打印解析的每个值
Serial.println(EC);//这是EC值
Serial.print(“TDS:”);//我们现在分别打印解析的每个值
Serial.println(TDS);//这是TDS值
Serial.print(“SAL:”;//我们现在分别打印解析的每个值
Serial.println(SAL);//这是盐度值
Serial.print(“GRAV:);//现在我们分别打印解析的每个值
Serial.println(GRAV);//这是比重
Serial.println();//这只会使输出更易于阅读
mySensorData=SD.open(“CPData”,文件写入);
mySensorData.print(EC);
mySensorData.close();
}
}

如果(mySensorData)你不需要在
之前打开一个文件吗?我不这么认为。你是说我把[mySensorData=SD.open(“CPData”,FILE_WRITE);]放在[if(mySensorData)]之前吗?如果你不调用
mySensorData=SD.open()
mySensorData
可能为空。如果为空,
If(mySensorData)
的计算结果总是
false
。希望我能理解您的意思,但我会