将Arduino传感器数据保存到文本文件

问题描述:

我一直在努力将数据从加速度传感器保存到文本文件。但在搜索了几个小时之后,我发现的方法没有奏效。这是我现在的计划。将Arduino传感器数据保存到文本文件

// Accelerometer sensor program 

const int ar1 = A5; 
const int ar2 = A4; 
const int ar3 = A3; 

int x = 0;   
int ov1 = 0;  
int y = 0;  
int ov2= 0;  
int z = 0;  
int ov3= 0;  

void setup() { 
    // initialize the communications 
    Serial.begin(9600); 

} 

void loop() { 
    analogReference(EXTERNAL); //connect 3.3v to AREF 

    // X axis 
    // Read the analog 
    x = analogRead(ar1);    
    // Range of analog out 
    ov1 = map(x, 0, 1023, 0, 255); 
    delay(2);      

    // Y axis 
    y = analogRead(ar2);    
    ov2 = map(y, 0, 1023, 0, 255); 

// Z axis 
    delay(2);     
    z = analogRead(ar3);    
    ov3 = map(z, 0, 1023, 0, 255); 

// Should print to the monitor 
// Output for X axis 
    Serial.print("Xsensor1 = ");      
    Serial.print(x);  
    Serial.print("\t output1 = ");  
    Serial.println(ov1); 

// Output for Y axis 
    Serial.print("Ysensor2 = ");      
    Serial.print(y);  
    Serial.print("\t output2 = ");  
    Serial.println(ov2); 

// Output for Z axis 
    Serial.print("Zsensor3 = ");      
    Serial.print(z);  
    Serial.print("\t output3 = ");  
    Serial.println(ov3); 

// Should repeat every second 
    delay(1000); 

什么我发现似乎做我想做什么,这个问题是Arduino的不希望运行它,说“进口”的范围未声明。我已经尝试安装一些库,但是我找不到导入是其中一部分的确切库,或者PrintWriter。以下是导致问题的代码:

import processing.serial.*; 
Serial mySerial; 
PrintWriter output; 
void setup() { 
    mySerial = new Serial(this, Serial.list()[0], 9600); 
    output = createWriter("data.txt"); 
} 
void draw() { 
    if (mySerial.available() > 0) { 
     String value = mySerial.readString(); 
     if (value != null) { 
       output.println(value); 
     } 
    } 
} 

void keyPressed() { 
    output.flush(); // Writes the remaining data to the file 
    output.close(); // Finishes the file 
    exit(); // Stops the program 
} 

在此先感谢。

您拥有的代码不是用于Arduino,而是用一种名为Processing的语言编写的。它将运行在您的PC上并从Arduino收集数据以保存到文件中。您需要下载Processing IDE来编译代码并运行它。