0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

micro:bitとProcessingでSerial通信して可視化する2

Last updated at Posted at 2020-07-29

以前書いた記事より簡単にまとめました.
micro:bitの加速度をProcessingへ送り,画面サイズにmapしellipseで描画します.

1つの値を送る

micro:bitコード

スクリーンショット 2021-08-26 153153.png

Processingコード

import processing.serial.*;
Serial microbit;
float x = 0;

void setup() {
  size(640, 480);
  String []portName = Serial.list();
  println(portName);
  microbit = new Serial(this, portName[0], 115200);
}

void draw() {
  background(0);
  if (microbit.available() > 0) {
      int val = int(microbit.readString());
      x = map(val, -1023, 1023, 0, 640);
  }
  ellipse(x, height/2, 30,30);
}

複数の値を送る

micri:bitコード

スクリーンショット 2021-08-26 153021.png

Processingコード

import processing.serial.*;
Serial microbit;
float x = 0;
float y = 0;

void setup() {
  size(640, 480);
  String []portName = Serial.list();
  println(portName);
  microbit = new Serial(this, portName[0], 115200);
  microbit.bufferUntil(10);
}

void draw() {
  background(0);
  ellipse(x, y, 30, 30);
}

void serialEvent(Serial microbit) {
  String str = microbit.readStringUntil('\n');
  if (str != null) {
    str = trim(str);
    float[] sensors = float(split(str, ','));
    if(sensors.length > 1){
    x = map(sensors[0], -1023, 1023, 0,width);
    y = map(sensors[1], -1023, 1023, 0, height);
    }
  }
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?