Events Arduino Serial.write处理返回0?

Events Arduino Serial.write处理返回0?,events,serialization,arduino,processing,communication,Events,Serialization,Arduino,Processing,Communication,我现在有一个Arduino程序,它通过一个串行事件来传递加速计值,以使处理工作正常。我正在尝试将温度计添加到设置中,但处理仅从读取pin接收0。如果我在设置中串行打印读数,它会很好地打印到串行监视器上,但是我无法让它在我的加速计读数旁边发送正确的值 Arduino代码: int inByte = 0; void setup() { Serial.begin(9600); while (!Serial) { ; // wait for serial port to conne

我现在有一个Arduino程序,它通过一个串行事件来传递加速计值,以使处理工作正常。我正在尝试将温度计添加到设置中,但处理仅从读取pin接收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);
  }
}
加速计值打印得很完美,但温度仅返回0。串行监视器中的串行打印(analogRead(A0))为我提供了正确的值,因此温度计肯定工作正常

任何帮助都将不胜感激,谢谢

在这方面

如果(serialCount>2){

换成

如果(串行计数>=4){

或者尝试使用类型转换或更改整数的温度


int-temperature;

>//如果我们有3个字节…我们还没有温度;)谢谢!这与将数组长度移动到略大于我接收的数据量有帮助!出于某种奇怪的原因,如果我在只需要4的时候将数组长度推到5,temp开始正确读取。成功添加了一张照片电池也是如此,所以5个输入工作得很好。谢谢!不过我确实有一个问题,那就是我的光电传感器在处理过程中只发送0-255的值(当我使用serial.print时,arduino串行监视器读取为700+),即使我使用serial.write将光电传感器的值发送到处理过程中。知道如何解决这个问题吗?
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;
    }
  }
}