import processing.serial.*;
Serial port;
// port 정의 : port를 굳이 여러 개 사용할 필요는 없구나
boolean flag = true;
void setup() {
size(800, 800); // 화면 크기 설정
port = 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 (2 < slope) { //기울기가 2보다 크거나 같을 때 actuator는 7->4->1 순서로 동작
float time_set1 = millis(); // 첫 번째 actuator의 시작 시간
float duration = 160; // 각 actuator의 동작 기간
float SOA = 50.5; // 각 actuator 간의 간격
while (millis() <= time_set1 + duration){
float beta = 1/slope;
float A2 = sqrt(((beta)/(1-beta))*pow(255,2));
port.write('A');
port.write(byte(A2));
println("Actuator3/5 on");
println(int(A2));
}
port.write('Z'); // off
println("Actuator3/5 off");
customDelay(100);
}
}
}
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; // 함수 바꾸고 싶으면 여기 수정하면 된다.
}
void customDelay(float duration) {
float startTime = millis();
while (millis() - startTime < duration) { // if
// 원하는 동작 수행
float mouseXValue = mouseX - width/2;
float mouseYValue = height - quadraticFunction(mouseXValue);
float slope = 2 * 0.1 * mouseXValue + 2;
// 출력
fill(255, 0, 0);
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) + ")";
String slopeText = "Slope: " + nf(slope, 0, 2);
text(coordinateText, 10, 20);
text(slopeText, 10, 40);
}
}
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
if(Serial.available()>0){
char val = Serial.read(); // read 후에 텅~
if (val == 'A') {
if(Serial.available()>0){ // 다시 뭐가 생기면
int input = Serial.read(); // 새로운 값을 읽어!
analogWrite(3,input);
analogWrite(5,255);
}
}
if (val == 'Z') {
analogWrite(3,0);
analogWrite(5,0);
}
}
}