我正在开发一个项目,我通过蓝牙将一些数字发送到arduino mega board,所以当我直接打印发送的项目时,它们显示正确。但是当我将这些数字存储在一个数组中并尝试打印它们时,它们显示为不同的数字。这些数字是从我的智能手机发送的整数。这是我的代码:
#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 :(得分:0)
我认为
Serial.print((myInts[a]));
应该是
Serial.println((myInts[a]));
能够以ASCII格式打印整数。
答案 1 :(得分:0)
Serial.println((myInts[a]+2));
应该是
Serial.println((myInts[a+1]));