記事一覧

秋月電子のアナログジョイスティックDIP化キットをArduinoとProcessingで使ってみた

Analog Joystick on Arduino x Processing



RoboTakaoです。ようこそ「極力ローコスト ロボット製作 ブログ」にお越しくださいました。
Hello everyone! I’m RoboTakao. Welcome to my robot blog.

今回は先日買った秋月電子通商のアナログジョイスティックDIP化キット(K-10263) をArduinoにつなげつつ、X、Y軸のデータをProcessingに送って、キューブを動かしたいと思います。

In this time, I try to control the cube on Processing by data from Arduino with analog Joystick.

Joystick02.jpg


ジョイスティックの仕様図 Joystick specification sheet
http://akizukidenshi.com/download/JT8P-3.2T-B10K-1-16Y.pdf

Joystick01.jpg

結線図 Connection

Joystick04.png

動作結果 Results

ジィスティックを動かすとProcessing上のキューブを動かすことができました。
I can control the cube on Processing by Arduino from motion of the Joystick.



Arduinoのスケッチ Sketch of Arduino

int joy_x = 0;
int joy_y = 1;
int val0,val1;

void setup() {
Serial.begin(9600);
}

void loop() {
val0 = analogRead(joy_x);
val1 = analogRead(joy_y);

Serial.print("s,");
Serial.print(val0, DEC);
Serial.print(',');
Serial.print(val1, DEC);
Serial.print(',');

delay(50);
}


Processingのコード Code of Processing

import processing.serial.*;

Serial myPort; // Create object from Serial class
String datline;

int Xaxis = 0;
int Yaxis = 0;

void setup() {
size(800, 800, P3D);
frameRate(30);
noStroke();
colorMode(RGB, 1);


// Serial Port
myPort = new Serial(this, "/dev/cu.usbserial-A104WMBI", 9600);
}

void draw() {
background(0.5);

pushMatrix();

translate(width / 2 + Xaxis, height / 2 - Yaxis, -30);

char datline_c;
datline = "";
int i=0;

while(i < 3){
if ( myPort.available() > 0){
datline_c = myPort.readChar();
if(datline_c=='s'){
i=0;
datline = "";
}
if(datline_c==',') i++;
datline = datline + datline_c;
}
print();
}

int[] splitdata = int(datline.split(","));

println(datline);

println(splitdata[1]);
println(splitdata[2]);

Xaxis = (int(splitdata[1])-512)/2;
Yaxis = -(int(splitdata[2])-512)/2;

println(Xaxis);
println(Yaxis);

scale(100);

beginShape(QUADS);

fill(0, 1, 1);
vertex(-1, 1, 1);
fill(1, 1, 1);
vertex( 1, 1, 1);
fill(1, 0, 1);
vertex( 1, -1, 1);
fill(0, 0, 1);
vertex(-1, -1, 1);

fill(1, 1, 1);
vertex( 1, 1, 1);
fill(1, 1, 0);
vertex( 1, 1, -1);
fill(1, 0, 0);
vertex( 1, -1, -1);
fill(1, 0, 1);
vertex( 1, -1, 1);

fill(1, 1, 0);
vertex( 1, 1, -1);
fill(0, 1, 0);
vertex(-1, 1, -1);
fill(0, 0, 0);
vertex(-1, -1, -1);
fill(1, 0, 0);
vertex( 1, -1, -1);

fill(0, 1, 0);
vertex(-1, 1, -1);
fill(0, 1, 1);
vertex(-1, 1, 1);
fill(0, 0, 1);
vertex(-1, -1, 1);
fill(0, 0, 0);
vertex(-1, -1, -1);

fill(0, 1, 0);
vertex(-1, 1, -1);
fill(1, 1, 0);
vertex( 1, 1, -1);
fill(1, 1, 1);
vertex( 1, 1, 1);
fill(0, 1, 1);
vertex(-1, 1, 1);

fill(0, 0, 0);
vertex(-1, -1, -1);
fill(1, 0, 0);
vertex( 1, -1, -1);
fill(1, 0, 1);
vertex( 1, -1, 1);
fill(0, 0, 1);
vertex(-1, -1, 1);

endShape();
popMatrix();
}


それでは今回はこのへんで失礼します。
Good bye!

スポンサードリンク

コメント

コメントの投稿

非公開コメント

プロフィール

RoboTakao

Author:RoboTakao
みなさんご訪問ありがとうございます。ロボット作りたいけどお小遣いそんなにないし、簡単でローコストでロボットを作るための私のプロジェクトを紹介します。

ウェブサイトもありますのでそちらもよろしくお願いします。
http://robotakao.jp/

スポンサーリンク