2016-08-12 153 views
1

我目前有一個Arduino程序通過串行事件傳遞加速度計值,以便處理完美。我正在嘗試將溫度計添加到我的設置中,但處理僅從讀取引腳接收0。如果我在設置中將Serial.print讀取到打印機上,它可以很好地打印到串行監視器上,但是我無法將它發送到加速計讀數旁邊。Arduino Serial.write處理返回0?

Arduino的代碼:

int inByte = 0; 

void setup() { 
    Serial.begin(9600); 
    while (!Serial) { 
    ; // wait for serial port to connect. Needed for native USB port only 
    } 
    establishContact(); // send a byte to establish contact until receiver responds 
} 

void loop() { 

    if (Serial.available() > 0) { 

    // get incoming byte: 
    inByte = Serial.read(); 

    // send sensor values: 
    Serial.write(analogRead(A3)); // X AXIS 
    Serial.write(analogRead(A2)); // Y AXIS 
    Serial.write(analogRead(A1)); // Z AXIS 
    Serial.write(analogRead(A0)); // TEMPERATURE 
    } 
} 

void establishContact() { 
    while (Serial.available() <= 0) { 
    Serial.print('A'); // send a capital A 
    delay(300); 
    } 
} 

處理代碼:

import processing.serial.*; 

Serial myPort;      
int[] serialInArray = new int[4];  
int serialCount = 0;     
int xInput, yInput, zInput; 
float temperature; 
boolean firstContact = false; 

void setup() { 
    size(600, 600, P3D); 
    pixelDensity(2); 
    noStroke(); 
    background(0); 
    printArray(Serial.list()); 
    String portName = Serial.list()[4]; 
    myPort = new Serial(this, portName, 9600); 
} 

void draw() { 
} 

void serialEvent(Serial myPort) { 
    // read a byte from the serial port: 
    int inByte = myPort.read(); 
    // if this is the first byte received, and it's an A, 
    // clear the serial buffer and note that you've 
    // had first contact from the microcontroller. 
    // Otherwise, add the incoming byte to the array: 
    if (firstContact == false) { 
    if (inByte == 'A') { 
     myPort.clear();   // clear the serial port buffer 
     firstContact = true;  // you've had first contact from the microcontroller 
     myPort.write('A');  // ask for more 
    } 
    } else { 
    // Add the latest byte from the serial port to array: 
    serialInArray[serialCount] = inByte; 
    serialCount++; 

    // If we have 3 bytes: 
    if (serialCount > 2) { 

     zInput = serialInArray[0]-80; 
     yInput = serialInArray[1]-80+69; 
     xInput = serialInArray[2]-77; 
     temperature = serialInArray[3]; // should return voltage reading (i.e 16ºc = 130); 
     //println("x = " + xInput + ", y = " + yInput + ", z = " + zInput + ", Temp = " + serialInArray[3]); 

     // Send a capital A to request new sensor readings: 
     myPort.write('A'); 
     // Reset serialCount: 
     serialCount = 0; 
    } 
    } 
} 

加速度計值打印完美,但溫度只是返回0 Serial.print(analogRead(A0))在串行監控給我正確的價值,所以溫度計肯定工作。

任何幫助將不勝感激,謝謝!

+1

> //如果我們有3個字節......我們還沒有溫度;) – datafiddler

回答

1

在這一行,

如果(serialCount> 2){

變化爲

如果(serialCount> = 4){

OR嘗試使用類型轉換或改變溫度爲整數!

int temperature;

+0

謝謝!這同時將陣列的長度改變爲稍大於我接收的數據量!由於一些奇怪的原因,如果我只需要4將數組長度推到5時,temp開始正確讀取。也管理添加一個照片單元,所以5個輸入很好地工作。謝謝! – themessup

+0

雖然我確實有一個問題,那就是我的光電池傳感器在處理過程中只發送0-255的值(即使我使用Serial.write發送時,arduino串行監視器的讀數爲700+)要處理的光電元件值。任何想法如何我可以解決這個問題? – themessup