2014-04-22 60 views
0

我正在開發一個項目,我通過藍牙發送一些數字到arduino巨型板,所以當我直接打印發送的項目,他們顯示正確。但是當我將這些數字存儲在數組中並嘗試打印它們時,它們顯示爲不同的數字。這些號碼是從我的智能手機以整數形式發送的。這裏是我的代碼:收到來自藍牙的項目不是打印的項目

#include <SoftwareSerial.h>// import the serial library 

    SoftwareSerial Genotronex(51, 11); // RX, TX 
    int BluetoothData; // the data given from Computer 
    boolean oneTime = true; //ensures data is received only once 
    int myInts[1000]; 
    int counter = 0; // count the number of sent items 

void setup() { 

Genotronex.begin(9600); 
Serial.begin(9600); 
Serial.println("Bluetooth On"); 
pinMode(ledpin,OUTPUT); 

} 


void loop() { 

if (Genotronex.available() && oneTime == true){ 
    BluetoothData=Genotronex.read(); 
    Serial.println(BluetoothData);    // <== This shows the right numbers 
    myInts[counter] = BluetoothData;    //add the integers in an array 
    counter++; 

if (BluetoothData==231){ // to know that the array is fully sent once 
    oneTime = false; 
    Serial.println("Done"); 
    counter--;      //i dont want to print the last element 
    for(int a=0; a < counter; a=a+2){ 
    Serial.print("Bearing: "); 
    Serial.print((myInts[a]));   // <== This shows wrong numbers 
    Serial.print("  "); 
    Serial.print("Distance: "); 
    Serial.println((myInts[a]+2)); 
    } 

} 
} 

delay(100);// prepare for next data ... 


} 

有人可以請幫助我。 謝謝。

回答

0

我犯了一個愚蠢的錯誤

Serial.println((myInts[a]+2)); 

應該

Serial.println((myInts[a+1])); 
0

我覺得

Serial.print((myInts[a])); 

應該

Serial.println((myInts[a])); 

能夠打印整數爲ASCII。

+0

沒有遺憾,這並沒有解決問題。我在第二條印刷線上犯了一個愚蠢的錯誤。 – user3474318