SD卡写入失败

时间:2018-12-06 02:31:52

标签: c++

我有一个朋友,他的Adruino代码无法写入SD卡,并且正在寻求帮助。这是代码,非常感谢!如果您发现任何问题,请让我知道。

/* FSR testing sketch, log data to SD card 

Connect one end of FSR to power, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground 

For more information see www.ladyada.net/learn/sensors/fsr.html */

int fsrPin = 0;                 // the FSR and 10K pulldown are connected to a0
int fsrReading = 0;             // the analog reading from the FSR resistor divider
int fsrVoltage = 0;             // the analog reading converted to voltage
unsigned long fsrResistance;    // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance;
long fsrForce;                  // Finally, the resistance converted to force

#include <SD.h>                 //Load SD card library
#include<SPI.h>                 //Load SPI Library

#include "Wire.h"               // imports the wire library for talking over I2C
long Adafruit_P1075A = fsrForce;        // create sensor object called fsrForce

int chipSelect = 4;             //chipSelect pin for the SD card Reader
File fsrForceData;              //Data object you will write your sensor data to

void setup ( void )
{

// fsrForce.begin();   //initialize pressure sensor fsrForce
    Serial.begin ( 9600 );      // We'll send debugging information via the Serial monitor

    pinMode ( 10, OUTPUT );     //Must declare 10 an output and reserve it
    SD.begin ( 4 );             //Initialize the SD card reader
}

void loop ( void )
{
    fsrReading = analogRead ( fsrPin );
    Serial.print ( "Analog reading = " );
    Serial.println ( fsrReading );

    // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
    fsrVoltage = map ( fsrReading, 0, 1023, 0, 5000 );
    Serial.print ( "Voltage reading in mV = " );
    Serial.println ( fsrVoltage );

    if ( fsrVoltage == 0 )
    {
        Serial.println ( "No pressure" );
    }
    else
    {
        // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
        // so FSR = ((Vcc - V) * R) / V        yay math!
        fsrResistance = 5000 - fsrVoltage;      // fsrVoltage is in millivolts so 5V = 5000mV
        fsrResistance *= 10000; // 10K resistor
        fsrResistance /= fsrVoltage;
        Serial.print ( "FSR resistance in ohms = " );
        Serial.println ( fsrResistance );

        fsrConductance = 1000000;       // we measure in micromhos so 
        fsrConductance /= fsrResistance;
        Serial.print ( "Conductance in microMhos: " );
        Serial.println ( fsrConductance );
    }
    // Use the two FSR guide graphs to approximate the force
    if ( fsrConductance <= 1000 )
    {
        fsrForce = fsrConductance / 80;
        Serial.print ( "Force in Newtons: " );
        Serial.println ( fsrForce );
    }
    else
    {
        fsrForce = fsrConductance - 1000;
        fsrForce /= 30;
        Serial.print ( "Force in Newtons: " );
        Serial.println ( fsrForce );
    }
    {
        //   pressure=fsrForce.readPressure(); //Read Pressure

        fsrForceData = SD.open ( "PTData.txt", FILE_WRITE );
        if ( fsrForceData )
        {
            fsrForceData.print ( "The Force is: " );
            fsrForceData.print ( fsrForce );
            fsrForceData.println ( " N." );
            fsrForceData.println ( "" );
            delay ( 250 );      //Pause between readings.

            fsrForceData.println ( "--------------------" );
            delay ( 1000 );

            fsrForceData.close (  );    //close the file
        }
    }
}

这表示我需要发布更多详细信息,但其内容可以自我解释。再次感谢任何反馈。

0 个答案:

没有答案