只写一组数据点而不是一个

时间:2019-05-09 12:28:36

标签: c++ arduino

我已经为Arduino编写了将压力传感器的数据写入SD卡的代码。但是,当我尝试时,它只会写一个度量。

我尝试更改某些正确和错误的陈述。

#include <Wire.h>
#include <SFE_BMP180.h>
#include <SPI.h>
#include <SD.h>

SFE_BMP180 bmp180;
File logData;

int buttonPin = 3;
int ledPin = 13;

bool logStatus = false;

void setup() {
  Serial.begin(9600);
  bool success = bmp180.begin();

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  if (success) {
    Serial.println("BMP180 init success");
  } else {
    Serial.println("Sensor not initialized!!");
  }

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

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  logData = SD.open("log.txt", FILE_WRITE);
}

void loop() {
  char status;
  double T, P;
  bool success = false;

  if(digitalRead(buttonPin) == LOW){
    if (logStatus == true){
      //logStatus = false;
      logData.println("Groetjes van Jelle ;)");
      logData.close();
      Serial.println("Groetjes van Jelle!");
      digitalWrite(ledPin, LOW);
    } else {
      logStatus = true;
      digitalWrite(ledPin, HIGH);
    }
  }

  status = bmp180.startTemperature();

  if (status != 0) {
    delay(1000);
    status = bmp180.getTemperature(T);

    if (status != 0) {
      status = bmp180.startPressure(3);

      if (status != 0) {
        delay(status);
        status = bmp180.getPressure(P, T);

        if (status != 0 && logStatus == true) { 
          logData.print("Pressure: ");
          logData.print(P);
          logData.println(" hPa");

          logData.print("Temperature: ");
          logData.print(T);
          logData.println(" C");

          Serial.print("Pressure: ");
          Serial.print(P);
          Serial.println(" hPa");

          Serial.print("Temperature: ");
          Serial.print(T);
          Serial.println(" C");
        }
      }
    }
  }
}

我希望这段代码可以将多个测量值写入SD卡,但是只能写入一个测量值。

0 个答案:

没有答案