首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的阿迪诺和我的电脑不会握手

我的阿迪诺和我的电脑不会握手
EN

Stack Overflow用户
提问于 2013-08-02 16:30:14
回答 1查看 236关注 0票数 0

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

代码语言:javascript
复制
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的代码:

代码语言:javascript
复制
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);
  //}
}

正如贴出的,代码工作,但在屏幕上的点是非常紧张。因此,我试图实现一个握手的协议,基于“事情如何交谈”(Igoe,第62页)。注释掉的行应该可以做到这一点。但是,当它们没有注释时,红色的点不再显示,处理草图永远不会到达println命令(“这里”)。

我用的是32位处理器2.0.1。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-02 16:56:47

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

代码语言:javascript
复制
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
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18021912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档