top drop menu

Recent Post

화요일, 11월 8

프로세싱(4) - 아두이노 가변저항으로 프로세싱 화면 제어하기

가변 저항을 연결해서 프로세싱의 도형의 크기나 칼라, 위치값을 변경해서 움직임을 줘 보자.


이 작업을 위해서 Firmata라는 것을 사용했다. 아두이노와 프로세싱간의 통신을 위해 미리 만들어 놓은 라이브러리인것 같다. 아우디노에서는 예제에 있는 Firmata > StandardFirmata 를 열어서 아두이노에 올려 놓는다. 보드레이드 속도를 잘 확인해 놓자. 기본 57600으로 잡혀 있다고 한다. 아두이노에서 할것은 없다. 이제 프로세싱에서 코드를 작성해 주면 된다. 상단에는 아두이노에서 가져온 가변저항의 오리지날 값고 프로세싱에서 사용하기 위한 매핑한 값을 출력하게 했다. 그리고 도형 세개를 그리도록 해 놓았다. 이 작업전에 아두이노에는 Firmata 라이브러리가 프로세싱에는 Arduino 라이브러리가 설치 되어 있어야 한다.

import processing.serial.*; import cc.arduino.*; Arduino arduino; int led = 11; //arduino analog input pin int input0 = 0; int input1 = 1; int input2 = 2; color bgColor = color(75,115,85); void setup() { //println(Arduino.list()); size(600, 400); arduino = new Arduino(this, Arduino.list()[1], 57600); arduino.pinMode(led, Arduino.OUTPUT); //원하는 글꼴 지정하고 폰트 출력 PFont font = createFont("NanumGothic", 12); textFont(font); } void draw() { background(bgColor); //led blink //arduino.digitalWrite(led, Arduino.HIGH); //delay(1000); //arduino.digitalWrite(led, Arduino.LOW); //delay(500); //potentiometer value int analog0 = arduino.analogRead(input0); int analog1 = arduino.analogRead(input1); int analog2 = arduino.analogRead(input2); // 제어값 재설정 int myRadius = int(map(analog0, 0, 1023, 0, 400)); int myColor = int(map(analog1, 0, 1023, 0, 255)); int mySpeed = int(map(analog2, 0, 1023, 0, 200)); //도형 그리기 //noFill(); fill(myColor,119,myColor, 90); ellipse(mySpeed+100,height/2, myRadius,myRadius); noStroke(); fill(myColor,49,133, 120); ellipse(mySpeed+200,height/2, myRadius,myRadius); fill(23, 50,myColor,180); ellipse(mySpeed+300, height/2, myRadius, myRadius); //화면에 오리지날 값 출력 fill(255); text("input0 = " + analog0, 10, 20); text("input1 = " + analog1, 10, 40); text("input2 = " + analog2, 10, 60); //화면에 제어값 출력 fill(255); text("크기 = " + myRadius, 100, 20); text("칼라 = " + myColor, 100, 40); text("속도 = " + mySpeed, 100, 60); //delay(10); }
Blogger Widget