【问题标题】:Arduino Serial.write to Processing returning 0?Arduino Serial.write 到处理返回 0?
【发布时间】:2016-08-12 01:33:03
【问题描述】:

我目前有一个 Arduino 程序通过串行事件通信加速度计值,以便完美处理。我正在尝试在我的设置中添加一个温度计,但是处理仅从读取引脚接收到 0。如果我 Serial.print 设置中的读数,它确实可以很好地打印到串行监视器,但是我无法让它在我的加速度计读数旁边发送正确的值。

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)) 给了我正确的值,所以温度计肯定在工作。

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

【问题讨论】:

  • > // 如果我们有 3 个字节......我们还没有温度 ;)

标签: events serialization arduino processing communication


【解决方案1】:

在这一行,

if (serialCount > 2) {

if (serialCount >= 4) {

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

温度;

【讨论】:

  • 谢谢!这与将数组的长度移动到略大于我收到的数据量一起帮助!出于某种奇怪的原因,如果我只需要 4 时将数组长度推到 5,则 temp 开始正确读取。还设法添加了一个光电管,因此 5 个输入工作得很好。谢谢!
  • 我确实有一个问题,那就是我的光电传感器在处理过程中只发送 0–255 的值(当我 serial.print 时,arduino 串行监视器读取为 700+)即使我m 使用 Serial.write 将光电池值发送到处理。知道如何解决这个问题吗?
猜你喜欢
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多