2016-09-25 513 views
1

我一直在努力將數據從加速度傳感器保存到文本文件。但在搜索了幾個小時之後,我發現的方法沒有奏效。這是我現在的計劃。將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 
} 

在此先感謝。

回答

0

您擁有的代碼不是用於Arduino,而是用一種名爲Processing的語言編寫的。它將運行在您的PC上並從Arduino收集數據以保存到文件中。您需要下載Processing IDE來編譯代碼並運行它。