2016 :: 아두이노 기초

프로젝트:: 완성

 

최종코드

 

import processing.serial.*;

Serial myPort;
String myString = null;
int angle = 0;
int distance = 0;

void setup(){
  size(1200, 700);
  background(0);
  myPort = new Serial(this, "COM14", 9600);
}

void draw(){
  noStroke();
  fill(0,7);
  rect(0,0,width,height);
  drawRader();
  drawLine();
  drawObject();
}

void serialEvent(Serial p){
  try{
    myString = p.readStringUntil('.');
    String[] list = split(myString, ',');
    angle = int(list[0]);
    distance = int(list[1].replace(".",""));
  }catch(Exception e){
    print(e);
  }
}

void drawRader(){
  pushMatrix();
  translate(width/2, height);
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);
  
  // draw the arc lines
  arc(0, 0, width, width, PI, TWO_PI);
  arc(0, 0, width*2/3, width*2/3, PI, TWO_PI);
  arc(0, 0, width*1/3, width*1/3, PI, TWO_PI);
  
  // draw the angle lines
  line(0, 0, width/2*cos(radians(30)), -width/2*sin(radians(30)));
  line(0, 0, width/2*cos(radians(60)), -width/2*sin(radians(60)));
  line(0, 0, width/2*cos(radians(90)), -width/2*sin(radians(90)));
  line(0, 0, width/2*cos(radians(120)), -width/2*sin(radians(120)));
  line(0, 0, width/2*cos(radians(150)), -width/2*sin(radians(150)));
  
  // draw text
  textSize(15);
  fill(98, 245, 31);
  textAlign(RIGHT);
  text("10cm", width*1/6, 0);
  text("20cm", width*2/6, 0);
  text("30cm", width*3/6, 0);

  popMatrix();
}

void drawLine(){
  pushMatrix();
  translate(width/2, height);
  strokeWeight(4);
  stroke(98, 245, 31);
  line(0, 0, width/2*cos(radians(angle)), -width/2*sin(radians(angle)));
  popMatrix();
}

void drawObject(){
  pushMatrix();
  translate(width/2, height);
  strokeWeight(4);
  stroke(255, 10, 10); // red color
  float d = (width/2.0/30.0)*(float)distance;
  
  if( d < width/2)
    line(d*cos(radians(angle)), -d*sin(radians(angle)), width/2*cos(radians(angle)), -width/2*sin(radians(angle)));
  popMatrix();
}

테스트 영상

 

댓글

댓글 본문
  1. Daknichu
    완료까지 쉽고 재미있게 강의 해주셔서 너무 감사합니다.
  2. 김준성
    좀더 원론적인 개념들에 대해서 공부하고 싶네요.
    감사합니다.
    이런 강좌를 오프라인에서 배울 만한 곳은 없나요?
  3. supersonic
    하드웨어 제어에 평소 궁금증이 많았는데 알기쉽게 이론과 실기를 설명해주셔서 매우 도움이 되었습니다. 좀더 복잡한 프로젝트를 접하기 전에 개념을 잡는데 매우 유용한 자료입니다. 감사합니다.
  4. JustStudy
    이런 훌륭한 강의를 만나게 되어서 행운입니다.
    당장 아두이노를 실습하겠습니다.
    그리고 더욱 열심히 하겠습니다.
    큰 덕을 베풀어 주셔서 고맙습니다.
    (1 차 학습 완료)
버전 관리
YJ
현재 버전
선택 버전
graphittie 자세히 보기