
Processing Code:
import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
Balloon b1, b2;
PImage Back;
void setup()
{
size(1024, 530);
b1 = new Balloon(200,300,false);
b2 = new Balloon(800,300,true);
background(0);
frameRate(10);
// Open the port that the board is connected to and use the same speed (9600 bps)
println(Serial.list());
String portName = Serial.list()[1];
port = new Serial(this, portName, 9600);
}
void draw()
{
println(val);
if (0 < port.available())
{ // If data is available,
val = port.read(); // read it and store it in val
}
if(b1.state==0)
{
if(val==50)
b1.increaseBalloon();
if(val==48)
b1.decreaseBalloon();
}
else if(b1.state==1)
{
//b1.increaseCircle();
}
if(b2.state==0)
{
if(val==51)
b2.increaseBalloon();
if(val==48)
b2.decreaseBalloon();
}
else if(b2.state==1)
{
//b2.increaseCircle();
}
}
class Balloon
{
public int Xpos=470;
public int Ypos=330;
public int BalloonSize=20;
public int state=0;
public int CicleSize=20;
public boolean isRightHanded;
public Balloon(int X, int Y, boolean rightHanded)
{
this.Xpos = X;
this.Ypos = Y;
this.isRightHanded = rightHanded;
}
public void increaseBalloon()
{
BalloonSize+=20;
if(BalloonSize>300)
{
state=1;
}
drawBalloon();
}
{
if(BalloonSize<0)
{
BalloonSize=1;
}
BalloonSize-=20;
drawBalloon();
}
private void drawBalloon()
{
background(0);
if(isRightHanded)
ellipse(Xpos-BalloonSize/2, Ypos, BalloonSize, BalloonSize);
else
ellipse(Xpos+BalloonSize/2, Ypos, BalloonSize, BalloonSize);
}
public void increaseCircle()
{
CicleSize+=40;
drawExplode();
}
public void drawExplode()
{
background(Back);
noFill();
ellipse(Xpos, Ypos, CicleSize, CicleSize);
}
}
Arduino Code:
int switchPin = 4; // Switch connected to pin 4
int switchPin2 = 6;
int var=0;
void setup()
{
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop()
{
if(digitalRead(switchPin) == HIGH && digitalRead(switchPin2) ==HIGH)
{
var=1;
}
else if(digitalRead(switchPin) == HIGH && digitalRead(switchPin2) ==LOW)
{
var=2;
}
else if(digitalRead(switchPin) == LOW && digitalRead(switchPin2) ==HIGH)
{
var=3;
}
else if(digitalRead(switchPin) == LOW && digitalRead(switchPin2) ==LOW)
{
var=0;
}
Serial.print(var);
delay(100); // Wait 100 milliseconds
}
沒有留言:
張貼留言