질문게시판 > (초보)아두이노, 프로세싱 코드

TODAY241 TOTAL2,691,618
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | 질문게시판 | (초보)아두이노, 프로세싱 코드

페이지 정보

작성자 흑흑 작성일2018-12-13 18:12 조회33,420회 댓글8건

본문

	

프로세싱 코드


import processing.serial.*;

Serial myPort;
int val;

ArrayList<Particle> particles;
PImage[] bullets;
PImage red_bullet;
PImage green_bullet;
PImage blue_bullet;

final float GRAVITY = 200;


void setup() 
{
  frameRate(60);
  noStroke();
  size(1920, 1080, P3D);
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

  if (myPort.available() > 0) {
    val = myPort.read();
  }
  if (val == 1) {
    red_bullet = loadImage("a.png");
    green_bullet = loadImage("a2.png");
    blue_bullet = loadImage("a3.png");

    bullets = new PImage[]{red_bullet, green_bullet, blue_bullet};

    PVector GravityVector = new PVector(0, GRAVITY);

    PVector rocket_vel = PVector.fromAngle(-PI/3.0);
    rocket_vel.mult(150.0);

    particles = new ArrayList<Particle>();

    for (float i=0; i<100; i++) { 
      createRocket(i*0.5);
    }
  }
}

void draw()
{
  if (val == 1) {
    background(0);

    blendMode(ADD);

    color black = color(0, 0, 0);

    for (int i=0; i<8; i++) { 
      for (Particle p : particles) {
        float t = get_t() - i * 0.02;
        if (t >= p.initial_t && t < p.end_t) {
          PVector velVector = new PVector(p.x.vel(t), p.y.vel(t));
          pushMatrix();
          translate(p.x.pos(t), p.y.pos(t));
          rotate(velVector.heading() - PI * 0);
          scale(0.2, 0.2);
          drawSpark(p.image);
          popMatrix();
        }
      }
    }
  } else {
    background(255);
  }
}

float get_t() {
  return frameCount/60.0;
}

void createRocket(float time) {
  PVector initial_pos = new PVector((width/2.0), height);
  float burn_time = 1.5 + random(0.4);
  float glide_time = 1.0 + random(0.5);
  float thrust_mag = 400;
  PVector thrust_vector = PVector.fromAngle(3.0*PI/2.0 + random(-0.2, 0.2));
  thrust_vector.setMag(thrust_mag);

  PVector rocket_accel = new PVector(0, GRAVITY);
  rocket_accel.add(thrust_vector);

  Particle rocket_with_thrust = new Particle(
    initial_pos, 
    new PVector(0, 0), 
    rocket_accel, 
    time, 
    time + burn_time, 
    color(255, 100, 100), 
    red_bullet
    );

  Particle rocket_without_thrust = new Particle(
    rocket_with_thrust.pos(time + burn_time), 
    rocket_with_thrust.vel(time + burn_time), 
    new PVector(0, GRAVITY), 
    time + burn_time, 
    time + burn_time + glide_time, 
    color(255, 100, 100), 
    red_bullet
    );  

  particles.add(rocket_with_thrust);
  particles.add(rocket_without_thrust);  

  createExplosion(
    rocket_without_thrust.pos(time + burn_time + glide_time), 
    rocket_without_thrust.vel(time + burn_time + glide_time), 
    time + burn_time + glide_time, 
    bullets[int(random(3))]
    );
}

void createExplosion(PVector position, PVector rocketVector, float time, PImage image) {
  for (int i=0; i<80; i++) {

    float explosion_vel = 150;
    PVector vel;
    vel = PVector.random3D();
    vel.mult(explosion_vel);
    vel.add(rocketVector);

    particles.add(new Particle(
      position, 
      vel, 
      new PVector(0, GRAVITY), 
      time, 
      time + 1.0 + random(0.2) + random(0.2) + random(0.2), 
      color(255, 100, 100), 
      image
      ));
  }
}

void drawSpark(PImage image) {
  image(image, -32, -32);
}

class Particle {
  color baseColor;
  Motion x;
  Motion y;
  float initial_t;
  float end_t;
  PImage image;

  Particle(PVector position, PVector velocity, PVector accel, float initial_t, float end_t, color baseColor, PImage image) {
    this.x = new Motion(position.x, velocity.x, accel.x, initial_t);
    this.y = new Motion(position.y, velocity.y, accel.y, initial_t);
    this.initial_t = initial_t;
    this.end_t = end_t;
    this.baseColor = baseColor;
    this.image = image;
  }

  PVector pos(float t) {
    return new PVector(this.x.pos(t), this.y.pos(t));
  }

  PVector vel(float t) {
    return new PVector(this.x.vel(t), this.y.vel(t));
  }

  PVector accel() {
    return new PVector(this.x.accel, this.y.accel);
  }
}

class Motion {
  float initial_pos;
  float initial_vel;
  float accel;
  float initial_t;
  float t0_pos;
  float t0_vel;

  Motion(float initial_pos, float initial_vel, float accel, float initial_t) {
    this.accel = accel;
    this.t0_vel = 0;
    this.t0_vel = initial_vel - vel(initial_t);
    this.t0_pos = 0;
    this.t0_pos = initial_pos - pos(initial_t);
  }

  public float vel(float t) {
    return this.accel * t + this.t0_vel;
  }

  public float pos(float t) {
    return 0.5 * this.accel * pow(t, 2) + this.t0_vel * t + this.t0_pos;
  }
}


아두이노 코드
int switchPin = 2;                      

void setup() {
  pinMode(switchPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {
    Serial.println(1);
    Serial.write(1);
      delay(1000);
  } else {
    Serial.println(0);
    Serial.write(0);
    delay(1000);
  }
}

아두이노를 통해 1이라는 값을 받았을때 프로세싱에서 1일 경우에만 위 코드가 실행되도록 하고 싶은데 val==1로 실행하면 else 코드만 실행되고 val==0으로 해두면 1일때에도 코드가 멈추지 않아요ㅠㅠ
어떻게 해야할까요 도와주세요ㅠㅠ
  • BASIC4MCU 작성글 SNS에 공유하기
  • 페이스북으로 보내기
  • 트위터로 보내기
  • 구글플러스로 보내기

댓글 8

조회수 33,420

master님의 댓글

master 작성일

  if (val == 1) {
이 코드가 두군데 있군요
한번만 실행 하려면
  if (val == 1) {
  val =0; // <-- 추가하세요

흑흑님의 댓글

흑흑 댓글의 댓글 작성일

이게 불꽃놀이 코든데
setup에 저 코드를 넣지 않으면 실행하자마자 불꽃이 터져요 val=1일때만 불꽃이 날아가서 터졌으면 하는데 어떻게 해야할까요ㅠㅠ? 바쁘신데 귀찮게해서 죄송합니다 좀만 도와주세요ㅜㅜ
draw에 있는걸 빼면 else 코드가 실행이 안되구용..

master님의 댓글

master 작성일


void draw()
{
  if (val == 1) { // <-- 이 코드는 필요 없어서 삭제 했나요?
  val= 0; // 위 코드가 있다면 여기도 추가해주세요
    background(0);

흑흑님의 댓글

흑흑 댓글의 댓글 작성일

앗 그럼
if ( val == 1) {
위 코드가 있는 곳에는
val  = 0;을 넣으면 되는건가요? setup이랑 draw에 모두 필요한거면 두 코드 모두에 넣음 될까요?

master님의 댓글

master 댓글의 댓글 작성일

다른 사람은 돌려볼 수 없으니 디버깅을 하지 못합니다.
돌려보고 문제가 되는 부분을 다시 질문하세요

master님의 댓글

master 작성일

//프로세싱 코드
import processing.serial.*;
Serial myPort;
int val=0; //--------------------------
//생략
//
void setup(){
  frameRate(60);
  noStroke();
  size(1920,1080,P3D);
  String portName=Serial.list()[0];
  myPort=new Serial(this,portName,9600);
  if(myPort.available()>0){
    val=myPort.read();
    if(val==1){ val=0; //--------------------------
      red_bullet=loadImage("a.png");
      //생략
    }
  }
}
//
void draw(){
  if(val==1){ val=0; //--------------------------
    background(0);
    //생략
  }
  if(val==0){ background(255); } //--------------------------
}

흑흑님의 댓글

흑흑 댓글의 댓글 작성일

알려주신 코드대로 정확하게 입력 후 다시 실행했는데요,
오류가 나는 부분은 없지만 시리얼 통신으로 1값을 받았는데도 코드가 실행되지 않고 background(255)만 실행이 됩니다.
흰 화면만 떠요. 아두이노에서 시리얼 모니터로 확인한 결과 0,1 값이 잘 구분이되는데 r이라는 영문이 숫자 앞에 뜹니다.
아두이노에서 값을 정확하게 받아들이지 못하고 있는 중인걸까요?

master님의 댓글

master 댓글의 댓글 작성일

숫자1과 문자'1'은 값이 다릅니다.
r이라는 영문이 아니고 ┌그림문자입니다.

질문게시판HOME > 질문게시판 목록

MCU, AVR, 아두이노 등 전자공학에 관련된 질문을 무료회원가입 후 작성해주시면 전문가가 답변해드립니다.
ATMEGA128PWMLED초음파
아두이노AVR블루투스LCD
UART모터적외선ATMEGA
전체 스위치 센서
질문게시판 목록
제목 작성자 작성일 조회
공지 MCU, AVR, 아두이노 등 전자공학에 관련된 질문은 질문게시판에서만 작성 가능합니다. 스태프 19-01-15 19417
공지 사이트 이용 안내댓글[28] master 17-10-29 34131
질문 stm32f767을 이용해서 자이로가속도 센서의 값 받아오기 새글 rlchwjswk 23-06-03 7
질문 아두이노 모터제어 관련해서 질문드립니다!댓글[1] 이미지새글첨부파일 아두이노어렵잖아 23-06-03 21
질문 atmega128 디지털조도센서 코드오류댓글[1] 이미지새글 까미 23-06-02 25
질문 atmega128 디지털 조도 센서댓글[1] 새글 까미 23-06-02 25
질문 적외선리모콘으로 부저를제어 하는방법 질문입니다.댓글[4] 새글 Tell 23-06-02 18
질문 lora 무선 모듈에 관한 질문입니다.댓글[1] 로이스10 23-06-01 18
질문 적외선 송수신기 DC모터2개 제어 질문입니다.댓글[5] Tell 23-06-01 31
질문 스텝모터 제어 코드 질문댓글[4] pmh11 23-05-31 32
질문 초음파 센서를 이용한 인원 카운팅댓글[1] 초음파야 23-05-31 26
질문 모터 Hall 스위치 연결 문의댓글[1] 오후 23-05-31 21
질문 아두이노 lcd 문자 스크롤디스플레이 wnion 23-05-31 22
답변 답변글 답변 : 아두이노 lcd 문자 스크롤디스플레이댓글[1] master 23-05-31 28
질문 아두이노 타이머 인터럽트 미ㅏㄴㅇ 23-05-30 37
답변 답변글 답변 : 아두이노 타이머 인터럽트댓글[6] master 23-05-30 44
질문 THC-Soil Sensor with TTL 모듈 아두이노 센서값 받아오기댓글[1] ppiickle 23-05-30 30
질문 stm32 psd센서구동 질문댓글[2] 수포자 23-05-29 28
질문 앱인벤터 아두이노 보드 LCD 글씨 나타내기 질문댓글[7] 이미지 당찬병아리 23-05-29 41
질문 atmega128 led와 fan댓글[3] 이라 23-05-28 44
질문 stm32f767 스텝모터 속도 질문있습니다 123132 23-05-27 34
답변 답변글 답변 : stm32f767 스텝모터 속도 질문있습니다 master 23-05-28 30
질문 아트메가 128 코딩 오류 질문입니다.댓글[1] 태태킴 23-05-27 31
질문 스텝모터 제어하는 소스파일인데 질문있습니다. Bs드리프터 23-05-27 38
답변 답변글 답변 : 스텝모터 제어하는 소스파일인데 질문있습니다. master 23-05-27 35
질문 모터 컨트롤러와 웜기어 모터 연결 문의 드립니다댓글[1] 오후에 23-05-27 28
답변 답변글 답변 : 모터 컨트롤러와 웜기어 모터 연결 문의 드립니다댓글[1] 첨부파일 오후에 23-05-27 33
질문 아두이노 스텝모터 초음파 결합댓글[2] 결합기원 23-05-26 55
답변 답변글 답변 : 아두이노 스텝모터 초음파 결합 master 23-05-27 28
질문 piezo부저 연결 방법댓글[2] 이미지첨부파일 djwb 23-05-26 34
게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기