0

Processing と Arduino Uno を使用して、2 つのポテンショメータを使用して画面上の円の位置を制御しています。Arduino とコンピュータは Bluetooth 経由で通信します。処理スケッチのコードは次のとおりです。

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, page 62) に基づいたハンドシェイク プロトコルを実装しようとしました。コメントアウトされた行はそれを行うことになっています。しかし、それらがコメント解除されると、赤い点は表示されなくなり、処理スケッチはコマンド println("here") に到達しません。

32 ビットの Processing 2.0.1 を使用しています。

4

1 に答える 1

2

Arduino スケッチは、データを送信する前にデータを受信するまで待機します。したがって、Processing スケッチは、最初に何かをシリアル経由で 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
}
于 2013-08-02T16:56:47.020 に答える