为什么 Arduino int 不能超过 14464?

Why Arduino int can't count over 14464?

我在从MPU6050读取然后写入SD卡时遇到了一些问题。

其实我可以成功读写,但是我发现Arduino UNO数不过14464!?

我将数据的每一行设置为:

count, time(millis()), ax, ay, az, gx, gy, gz

它可以记录数据直到计数到14464(I)并且它会自动结束循环。

这真的很困扰我...而且以前似乎没有人遇到过这个问题。

这是我的代码:

#include "I2Cdev.h"
#include "MPU6050.h"

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif

MPU6050 accelgyro;

//SD card here
#include <SPI.h>
#include <SD.h>
File myFile;

//////////Global Variable Here//////////
int16_t ax, ay, az;
int16_t gx, gy, gz;
int count = 1;

//set sec & count_limit
int set_time = 1000 * 60;
int count_limit = 80000;
int BTN = 7;

// uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated
// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
// not so easy to parse, and slow(er) over UART.
#define OUTPUT_READABLE_ACCELGYRO

//Set LED
#define R_PIN 8
#define G_PIN 9
bool blinkState_R = false;
bool blinkState_G = false;

void setup() {

    // configure Arduino LED for
    pinMode(R_PIN, OUTPUT);
    pinMode(G_PIN, OUTPUT);
    pinMode(BTN, INPUT);

    digitalWrite(G_PIN, HIGH);

    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
    Fastwire::setup(400, true);
    #endif

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(38400);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();
    accelgyro.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

    // use the code below to change accel/gyro offset values
    accelgyro.setXGyroOffset(59);
    accelgyro.setYGyroOffset(42);
    accelgyro.setZGyroOffset(-8);
    accelgyro.setXAccelOffset(1359);
    accelgyro.setYAccelOffset(-1620);
    accelgyro.setZAccelOffset(1917); 

    /////////////////////////////////////////////////////////////////////
    //SD card Initailize
    /////////////////////////////////////////////////////////////////////

    Serial.print("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("initialization failed!");
        digitalWrite(R_PIN, HIGH);
        return;
    }
    Serial.println("initialization done.");
    digitalWrite(R_PIN, LOW);


    if (SD.exists("example.txt")) {
        Serial.println("example.txt exists.");
    }
    else {
        Serial.println("example.txt doesn't exist.");
    }

    // open a new file and immediately close it:
    Serial.println("Creating example.txt...");
    myFile = SD.open("example.txt", FILE_WRITE);
    myFile.close();

    // Check to see if the file exists:
    if (SD.exists("example.txt")) {
        Serial.println("example.txt exists.");
    }
    else {
        Serial.println("example.txt doesn't exist.");
    }

    // delete the file:
    Serial.println("Removing example.txt...");
    SD.remove("example.txt");

    if (SD.exists("example.txt")) {
        Serial.println("example.txt exists.");
    }
    else {
        Serial.println("example.txt doesn't exist.");
    }
    delay(3000);

    ////////////////////////////////////////////////////////////////////////////////
    //SD END
    //////////////////////////////////////////////////////////////////////////////// 

    digitalWrite(G_PIN, LOW);
}

void loop() {

    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    blinkState_R = !blinkState_R;
    digitalWrite(R_PIN, blinkState_R);
    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    //accelgyro.getRotation(&gx, &gy, &gz);


    ////////////////////////////////////////////
    // Write to SD Card
    ///////////////////////////////////////////

    // write data to file

    if(count <= count_limit ){
        myFile = SD.open("IMU_LOG.txt", FILE_WRITE);
        Serial.print(count);Serial.print("\t"); myFile.print(count); myFile.print("\t");
        Serial.print(millis()); Serial.print("\t"); myFile.print(millis()); myFile.print("\t");
        Serial.print(ax); Serial.print("\t"); myFile.print(ax); myFile.print("\t");
        Serial.print(ay); Serial.print("\t"); myFile.print(ay); myFile.print("\t");
        Serial.print(az); Serial.print("\t"); myFile.print(az); myFile.print("\t");
        Serial.print(gx); Serial.print("\t"); myFile.print(gx); myFile.print("\t");
        Serial.print(gy); Serial.print("\t"); myFile.print(gy); myFile.print("\t");
        Serial.print(gz); Serial.print("\n"); myFile.print(gz); myFile.print("\n");
        myFile.close();
        delay(5);


        blinkState_G = !blinkState_G;
        digitalWrite(G_PIN, blinkState_G);
    }else{
        while(1){
            Serial.print("Process done.\n");
            digitalWrite(G_PIN, OUTPUT);
            delay(2000);
        }

        count= count + 1 ;
    }

您应该在此处收到编译器警告。

int count_limit = 80000;

您平台上int的最大值为32,767。当您将 int 设置为更大的值时,行为是未定义的,这是个坏消息,因为这意味着您的程序不正确。

在这种特殊情况下,您可能会注意到 80000 = 14464 + 216,这解释了为什么它在 14464 处停止,如果 int 是 16 位长。

如果你想计算大于 65,535,你将需要使用 long

long count_limit = 80000L;
long count = 1;