Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created July 17, 2010 10:19
Show Gist options
  • Save tatsuro-ueda/479416 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/479416 to your computer and use it in GitHub Desktop.
//シリアルライブラリを取り込む
import processing.serial.*;
//シリアルのオブジェクトmyPortを用意
Serial myPort;
//x,y,zの3個の変数を用意
int x=0;
int y=0;
int z=0;
void setup(){
//3D用の画面サイズとして設定
size(255,255,P3D);
//シリアルポートの設定(「A4001Kjl」は基盤により異なる)
//Windowsの場合は、"COM5"などとなる(前回ブログを参照)
myPort=new Serial(this,"COM9",9600);
//3D図形の塗りは無し(ワイヤーフレーム描画)
noFill();
}
void draw(){
//背景色を白に設定
background(255);
//3Dの位置座標にx,y,z入れる
translate(x,y,128);
//一辺50のボックス(立方体)を描画
box(50);
}
//シリアル通信処理
void serialEvent(Serial p){
//Arduinoから送られてきたデータが
//3個(2より多い)の場合
if(myPort.available()>1){
//x,y,zの順番でデータを読み込む
y=256 - myPort.read();
x=myPort.read();
// z=myPort.read();
//読み込み後、合図データ送信
myPort.write(65);
}
}
//マウスが押されたら通信開始とする
void mousePressed(){
//念のためバッファ領域を空にする
myPort.clear();
//とりあえず65という合図用データを送る
myPort.write(65);
}
/*
* WiiChuckDemo --
*
* 2008 Tod E. Kurt, http://thingm.com/
*
*/
#include <Wire.h>
#include <nunchuck_funcs.h>
int loop_cnt=0;
byte accx,accy,zbut,cbut;
int ledPin = 13;
void setup()
{
Serial.begin(9600);
nunchuck_setpowerpins();
nunchuck_init(); // send the initilization handshake
Serial.print("WiiChuckDemo ready\n");
}
void loop()
{
if( loop_cnt > 100 ) { // every 100 msecs get new data
loop_cnt = 0;
nunchuck_get_data();
accx = nunchuck_accelx(); // ranges from approx 70 - 182
accy = nunchuck_accely(); // ranges from approx 65 - 173
zbut = nunchuck_zbutton();
cbut = nunchuck_cbutton();
if(Serial.available()>0){
Serial.print(accx, BYTE);
Serial.print(accy, BYTE);
Serial.read();
}
// Serial.print("accx: "); Serial.print((byte)accx,DEC);
// Serial.print("\taccy: "); Serial.print((byte)accy,DEC);
// Serial.print("\tzbut: "); Serial.print((byte)zbut,DEC);
// Serial.print("\tcbut: "); Serial.println((byte)cbut,DEC);
}
loop_cnt++;
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment