https://blog.naver.com/tndk4997/221769188778
아두이노&프로세싱으로 마술지팡이 만들기
이번 기말 텀은 조별과제로 아두이노를 활용해서 뭔가를 만드는거였다 나는 뭔가 영상을 만들거나, 스토리...
blog.naver.com
예전에 블로그에 올렸던 코드
코드를 정리를 안해두는 바람에 최종 코드가 뭔지 잘 모르겠고, 코드가 상-당히 스파게티 상태 + 비효율 + 읽기 힘듦
사용센서
아두이노 우노, 자석센서
아두이노
int inPin = 7;
int inPinPin = 6;
int inPinPinPin = 5;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(digitalRead(inPin)!=LOW&&digitalRead(inPinPin)!=LOW){
Serial.println("non");
Serial.write(5);
}
//마법진
if(digitalRead(inPin)==LOW){
Serial.println("마법진");
Serial.write(1);
}
// 무한대
if(digitalRead(inPinPin)==LOW){
Serial.println("fire");
//int infinte = digitalRead(6);
//Serial.println(infinte);
Serial.write(2);
}
//눈내리는 코드에 삽입할거
if(digitalRead(inPinPinPin)==LOW){
Serial.println("snow");
// int snow = digitalReda(5);
// Serial.println(눈);
Serial.write(3);
}
//if(digitalRead(inPinPin==LOW)){
// Serial.println("fire");
// Serial.write(2);
// }
delay(100);
}
세부적으로
int inPin = 7;
int inPinPin = 6;
int inPinPinPin = 5;
void setup()
{
Serial.begin(9600);
}
연결된 센서는 총 3개로 inPin / inPininPin / inPininPininPin으로 구분했다... 왜 그랬지
void loop()
{
if(digitalRead(inPin)!=LOW&&digitalRead(inPinPin)!=LOW){
Serial.println("non");
Serial.write(5);
}
//마법진
if(digitalRead(inPin)==LOW){
Serial.println("마법진");
Serial.write(1);
}
// 무한대
if(digitalRead(inPinPin)==LOW){
Serial.println("fire");
Serial.write(2);
}
//눈내리는 코드에 삽입할거
if(digitalRead(inPinPinPin)==LOW){
Serial.println("snow");
Serial.write(3);
}
delay(100);
}
loop() 안에서 센서가 반응하면 각각에 맞는 신호를 write로 프로세싱으로 보낸다. 신호를 받는동안 delay값을 100으로 주었다.
프로세싱
import processing.serial.*;
import processing.video.*;
import processing.sound.*;
SoundFile song;
Serial port;
Movie fire;
Movie snow;
Sound s;
void setup()
{
maxR = 400;
size(1040,640);
background(0);
port=new Serial(this,"COM3",9600);
}
void draw()
{
background(0);
if(val != 5){
play();
}
if(val == 5){
fire = new Movie(this,"fire.mp4");
snow = new Movie(this,"snow.mp4");
song = new SoundFile(this,"magic_circle.mp3");
}
}
프로세싱 오디오 라이브러리는 minim도 있지만 sound를 사용하였다.
https://openprocessing.org/sketch/531428
Magic Circle - OpenProcessing
Draw a magic circle randomly
openprocessing.org
마법진 코드는 삭제하고 업로드 하였다.
마법진 코드는 해당 코드를 조금씩 수정하였다.
play()에서 마법진을 불러온다.
void play(){
if(val == 0){
background(0);
}
if(val == 1){
pushMatrix();
s = new Sound(this);
song.play();
popMatrix();
if(val == 5){
song.pause();
}
}
if(val == 2){
fire.play();
image(fire,0,0,width,height);
if(val == 5){
fire.pause();
}
}
if(val == 3){
snow.play();
image(snow,0,0,width,height);
if(val == 5){
snow.pause();
}
}
}
만약 val이 5 일경우 == 3개의 센서에 아무 값이 없는 경우 영상이 종료된다.
val == 1 은 마법진 코드가 작성되었던 곳 원래는 loop함수 안에 작성했었는데 그렇게 하면 마법진이 처음에 생성 된 후 다시 신호를 주어도 똑같은 마법진이 나타나서 play()로 옮겨놨다.
이렇게 하면 마법진과 연결이 되어있는 자석센서에 자석이 닿일 때 마다 새로운 마법진이 생성된다.
void serialEvent(Serial port)
{
val = port.read();
old_val = val;
}
void movieEvent(Movie m){
m.read();
}
전 값을 가지고 오는 코드와
비디오를 읽어오는 코드