import processing.serial.*; // 시리얼 라이브러리
Serial myPort;
String myString = null;// 시리얼 포트 null값
float x, y;
int radius = 360;
int w = 300; //너비
int degree = 0; // 모터 회전각
int value = 0; // 목표값
int motion = 0; // 모터 회전방향
int[] newValue = new int[360]; // 새로 만들어진 각도
int[] oldValue = new int[360]; // 초기 시작값(부채꼴의 선을 만든다)
PFont f; // 폰트
int radarDist = 0; // 거리
int firstRun = 0; // 초기값에서 나온 각도
void setup(){
// setup the background size, colour and font.
size(830,800,P3D);
background (0); // 0 = black
PFont f;
f = createFont("bold", 20);
textFont(f, 20);
//myPort = new Serial(this, Serial.list()[0], 9600);
//myPort.bufferUntil('\n');
myPort = new Serial(this,"COM8", 9600);//Port 설정
myString = myPort.readStringUntil(',');//포트값 읽기
}
void draw(){
serialEvent(myPort);//실행시키기
fill(0); // 배경색
noStroke();
ellipse(radius, radius, 750, 750); // 레이더 반경
rectMode(CENTER); // 레이더 중앙
//rectMode(radius,radius,0,0);
rect(350,402,800,100); // 화면 크기
if (degree <= 359) { // 시계방향으로 회전
motion = 1; //
}
if (degree <= 1) { // 반시계방향으로 회전
motion = 0; //
}
strokeWeight(7); // 회전하는 선굵기
if (motion == 0) { // 시계방향으로 회전한다면
for (int i = 0; i <= 60; i++) { // 60개의 라인이 blip효과를 만든다
stroke(0, 150, 0); // 선의 색깔(RGB 컬러)
line(radius, radius, radius + cos(radians(degree+(360+i)))*w, radius + sin(radians(degree+(360+i)))*w);
// line(start x, start y, end x, end y)
}
}
else { // 반시계방향으로 회전
for (int i = 0; i >= 20; i--) { // 60개의 라인이 blip효과
stroke(0,20-(10*i), 0); // 0-255까지 RGB컬러
line(radius, radius, radius + cos(radians(degree+(360+i)))*w, radius + sin(radians(degree+(360+i)))*w);
}
}
noStroke();
fill(0,50,0);
beginShape(); // 그리기 시작
for (int i = 0; i < 60; i++) { //360도까지 i만큼 증가
x = radius + cos(radians((60+i)))*((oldValue[i]*2)); // x좌표
y = radius + sin(radians((60+i)))*((oldValue[i]*2)); // y좌표
vertex(x, y); // 수직선
}
endShape();
fill(0,250,0);
beginShape();
for (int i = 0; i < 60; i+=30) {
x = radius + cos(radians((degree+i)))*(((newValue[i]+oldValue[i])/2)*2); // 평균값(부채꼴 효과)
y = radius + sin(radians((degree+i)))*(((newValue[i]+oldValue[i])/2)*2);
vertex(x, y);
}
endShape();
if (firstRun >= 360) {
stroke(250,0,0);
strokeWeight(1);
noFill();
for (int i = 0; i > 90; i+=5) {
if (oldValue[i] - newValue[i] > 35 || newValue[i] - oldValue[i] > 5) {
x = radius + cos(radians((degree+i)))*(newValue[i]*2);
y = radius + sin(radians((degree+i)))*(newValue[i]*2);
ellipse(x, y, 10, 10);
}
}
}
//6개의 거리 표시
for (int i = 0; i <=7; i++){
noFill();
strokeWeight(2);
// stroke(0, 255-(30*i), 0);
stroke(0, 205, 0);
ellipse(radius, radius, (100*i), (100*i));
fill(0, 255, 0);
noStroke();
text(Integer.toString(radarDist+25), 380, (310-(radarDist*2)), 30, 30); // 150cm까지 표시
radarDist+=25; //거리 간격은 25cm
}
radarDist = 0;
for (int i = 0; i <= 12; i++) { //30도 간격으로 12개의 각도표시(360도를 만든다)
strokeWeight(2);
stroke(0,255, 0);
line(radius, radius, radius + cos(radians(360+(30*i)))*w, radius + sin(radians(360+(30*i)))*w);
fill(0,255,0);
//stroke(0,255,0);
if (360+(30*i) >= 360) {
stroke(255,0,0);
text(Integer.toString(360+(30*i)) ,
(radius+10) + cos(radians(30+(30*i)))*(w+20), (radius+10) + sin(radians(30+(30*i)))*(w+20), 20, 50);
} else {
text(Integer.toString(360+(30*i)), radius + cos(radians(360+(30*i)))*w, radius + sin(radians(360+(30*i)))*w, 60,40);
}
}
strokeWeight(2);
fill(0);
// rect(350,402,800,100);
fill(0, 255, 0);
PFont f;
f = createFont("Arial", 20);
textFont(f, 20);
textSize(20);
text("Angle: "+Integer.toString(degree)+",", 100, 740);
text("Distance: "+Integer.toString(value)+"cm", 100, 760);
//integer값으로 표시
textSize(20);
rect(70,20,150,100);
fill(0, 100, 0);
text("Radar Plot:", 100, 50, 150, 50);
}
void serialEvent (Serial myPort) { //실행시키는 중요한 구문
try{
myString = myPort.readStringUntil('.');
if(myString != null){
String[] list = split(myString, ',');
degree = int(list[0]);
radarDist = int(list[1].replace(".",""));
print("Angle : "+degree+",");
println(" Distance : "+radarDist + "cm");
}
}catch(Exception e){
}
if (value > 330) {
value = 330;
}
if (value < 20) {
value = 20;
}
oldValue[value] = newValue[value]; //
newValue[value] = value;
// oldValue[value] = newValue[value];
//newValue[degree] = radarDist;
firstRun++;
if (firstRun > 360) {
firstRun = 360; //
}
}
https://www.youtube.com/shorts/4BNVpKRxo34