프로세싱코드
import processing.serial.*;
Serial port;
void setup()
{
size(999,999);
port = new Serial(this, Serial.list()[0],9600);
}
int a;
int b;
int c;
int d;
int e;
int f;
int g;
void draw()
{
background(255);
if(mousePressed)
{
g = 1;
fill(255,0,0);
ellipse(mouseX,mouseY,10,10);
}
else
{
g = 0;
fill(255,255,255);
ellipse(mouseX,mouseY,10,10);
}
int x = mouseX;
int y = mouseY;
a = x/100;
b = x/10%10;
c = x%10;
a += 48;
b += 48;
c += 48;
d = y/100;
e = y/10%10;
f = y%10;
d += 48;
e += 48;
f += 48;
g += 48;
println(a, b, c,",",d,e,f);
port.write(a);
port.write(b);
port.write(c);
port.write(d);
port.write(e);
port.write(f);
port.write(g);
port.write(10);
}
아두이노 코드
#include <Servo.h>
long data = 0;
Servo xservo;
Servo yservo;
int laser = 12;
void setup()
{
Serial.begin(9600) ;
xservo.attach(11);
yservo.attach(5);
pinMode(laser, OUTPUT);
}
void loop()
{
if(Serial.available())
{
int x = Serial.read();
if(isDigit(x))
{
data = ((data * 10) + (x - 48));
}
else if(x == 10)
{
int a = data/10000;
int b = data%10000/10;
int c = data%10;
if(c == 0)
{
digitalWrite(laser, LOW);
}
else if(c == 1)
{
digitalWrite(laser, HIGH);
}
Serial.println(data);
a = map (a, 0, 999, 0, 180);
b = map(b, 0,999, 0, 180);
xservo.write(a);
yservo.write(b);
data = 0;
}
}
}
마우스 움직임에 따라 x축 서보 y축 서보가 움직이며 클릭하면 레이저를 발사하는 작동을 함
코드 중간중간에 48을 빼거나 더하는게 있는데
프로세싱 ->아두이노 로 데이터를 전송하는 부분에 있어서 약간 변환이 필요한 것 같음...
ascii코드랑 관련이 있는건데
정수를 아스키로 바꿔준담에 받아서 다시 정수형으로 바꿔주는거라면
그 과정을 생략해도 될 거 같은데 잘 안됩니다... 왜그런지.. 참..
만약 int 값이 255이하(마우스 조종창이 256픽셀보다 작을경우) 보내면 바로 받아지는데... 참 희한하죠..
이거 시리얼 통신할 때 변수의 형식에 따라서 뭔가 달라지는것 같아 그 부분은 연구중.. 이유발견하면 더 쓸 예정