HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
장지원 페이지/
🗑️
Trash
/
장지원, JANG JIWON
장지원, JANG JIWON
/HCI lab/
Processing

Processing

 
.

3. 아두이노와 연결

 
  • 프로세싱 코드
 
  • 아두이노 코드
 
void setup() { size(800, 800); // 화면 크기 설정 port1 = new Serial(this, Serial.list()[0]); // 객체 생성, 1번 포트 사 } void draw() { background(255); // 배경색 설정 // 이차 함수 그리기 stroke(0); // 선 색상 설정 for (float x = -width/2; x < width/2; x += 0.1) { float y = quadraticFunction(x); // 이차 함수의 y 값 계산 point(x + width/2, height - y); // 그림자표시 } // 마우스 커서 위치에 점 출력 float mouseXValue = mouseX - width/2; // 마우스 커서의 x 위치 계산 float mouseYValue = height - quadraticFunction(mouseXValue); // 이차 함수의 y 값 계산 // 커서가 그래프 선 위에 위치할 때만 빨간 점 출력 (범위 10픽셀) float yOnGraph = height - quadraticFunction(mouseXValue); if (mouseY >= yOnGraph - 80 && mouseY <= yOnGraph + 80) { // 여기서의 숫자가 허용 범위 픽셀 fill(255, 0, 0); // 점의 색상 설정 (빨간색) ellipse(mouseX, yOnGraph, 10, 10); // 점 출력 // 마우스 커서 위치의 좌표 출력 fill(0); // 텍스트 색상 설정 (검은색) String coordinateText = "Coordinate: (" + nf(mouseXValue, 0, 2) + ", " + nf(mouseYValue, 0, 2) + ")"; // 기울기 계산 및 출력 float slope = 2 * 0.1 * mouseXValue + 2; // 이차 함수의 미분 계산 String slopeText = "Slope: " + nf(slope, 0, 2); // nf는 number format을 의미한다. (포맷 할 숫자, 소수점 뒤 자릿 수, 표시할 숫자의 길이) text(coordinateText, 10, 20); text(slopeText, 10, 40); // print를 의미한다. (print할 변수, 표시할 x축 위치, 표시할 y축 위치) if (slope < 90) { port1.write('L'); // 1번 포트를 켜라, 주로 L이 켜는 문자로써 사용됨 println("Actuator1 on"); } } delay(100); // delay } // 이차 함수 정의 float quadraticFunction(float x) { float a = 0.1; // 이차 함수의 계수 float b = 2; // 이차 함수의 계수 float c = 30; // 이차 함수의 상수 // 이차 함수 계산: y = ax^2 + bx + c return a * x * x + b * x + c; // 함수 바꾸고 싶으면 여기 수정하면 된다. }
import processing.serial.*; Serial port; boolean flag = true; void setup(){ size(350,200); port = new Serial(this, Serial.list()[1]); } void draw(){ rect(125,50,100,100); } void mousePressed(){ if(mouseX >= 125 && mouseX <=225 && mouseY >= 50 && mouseY <=150){ if(flag == false){ port.write('L'); println("LED OFF!"); fill(0); flag = true; } else { port.write('H'); println("LED ON!"); fill(254); flag = false; } } delay(100); }
char val; void setup() { Serial.begin(9600); pinMode(12,OUTPUT); } void loop() { if(Serial.available()>0){ val = Serial.read(); if(val == 'H'){ digitalWrite(12, HIGH); } else { digitalWrite(12, LOW); } } }