【问题标题】:My Arduino and my computer won't shake hands我的 Arduino 和我的电脑不会握手
【发布时间】:2023-04-06 17:59:02
【问题描述】:

我正在使用 Processing 和 Arduino Uno 使用两个电位器来控制屏幕上圆圈的位置。 Arduino 和计算机通过蓝牙进行通信。这是处理草图的代码:

import processing.serial.*;

Serial myPort;
int x, y;

void setup() {
  size(400, 400);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 115200);
  myPort.bufferUntil('\n');
  background(255);
  noStroke();
}

void draw() {
}

void serialEvent(Serial myPort) {
  println("here");
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    inString = trim(inString);
    String items[] = split(inString, ',');
    if (items.length > 1) {
      float a = float(items[0]);
      float b= float(items[1]);
      x = (int) map(a, 0, 1023, 0, width);
      y = (int) map(b, 0, 1023, 0, height);
      background(255);
      fill(255, 0, 0);
      ellipse(x, y, 10, 10);
    }
  }
  //myPort.write('\r');
}

这是 Arduino 的代码:

const int left_pot = A2;
const int right_pot = A3;
int x;
int y;

void setup(){
  Serial.begin(115200);
 /* while (Serial.available()<=0){
    Serial.println("hello?");
  }*/
}

void loop(){
  //if (Serial.available() > 0) {
    int inByte = Serial.read();
    x = analogRead(left_pot);
    y = analogRead(right_pot);
    Serial.print(x);
    Serial.print(", ");
    Serial.println(y);
    delay(2);
  //}
}

正如发布的那样,代码有效,但屏幕上的点非常紧张。所以我尝试实现一个基于“How Things Talk”(Igoe,第 62 页)的握手协议。注释掉的行应该这样做。但是当它们被取消注释时,红点不再显示并且处理草图永远不会到达命令 println("here")。

我使用的是 32 位处理 2.0.1。

【问题讨论】:

    标签: bluetooth serial-port arduino processing


    【解决方案1】:

    您的 Arduino 草图会等到它收到一些数据后再发送数据。因此,您的处理草图必须首先通过串行向 Arduino 发送一些东西。目前它没有。尝试添加一些内容以使其打印:

    void setup() {
    size(400, 400);
    println(Serial.list());
    myPort = new Serial(this, Serial.list()[0], 115200);
    myPort.bufferUntil('\n');
    background(255);
    noStroke();
    myPort.write('\r');  //Get the arduino to reply
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 2018-06-11
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      相关资源
      最近更新 更多