Assignment 9

I further developed the tickle project that I did for last week by making ‘Yoon Hee’ run away from the hands that are trying to tickle it. The further from the centre of the word, the faster it moves away.

I used two potentiometers as the controllers to replace the cursor and change the x, y positions of the hands.

Here is the code for Arduino:

void setup() {
// put your setup code here, to run once:
pinMode(button, INPUT);
Serial.begin(9600);
Serial.println("0,0");
}

void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();

int sensor = analogRead(A0);
delay(0);
int sensor2 = analogRead(A1);
delay(0);
Serial.print(sensor);
Serial.print(',');
Serial.println(sensor2);
}
}

 

Here is the code for Processing:

import processing.serial.*;

Serial myPort;
PFont f;
String word = "Yoon Hee";
PImage img;

float x = 190;
float y = 180;
float speedX, speedY, xAccel, yAccel;

void setup() {
size(640, 360);
printArray(Serial.list());
String portName = Serial.list()[3];
println(portName);
myPort = new Serial (this, portName, 9600);
myPort.clear();
myPort.bufferUntil('\n');
background(255);
f = createFont("Monaco", 50);
textFont(f, 50);
noStroke();
speedX = speedY = xAccel = yAccel= 0;
img = loadImage("hand.png");
}

void draw() {
fill(204);
rect(0, 0, width, height);
fill(0);

image(img, mouseX-25, mouseY-10, 50, 50);

float textwidth = textWidth(word);
float centerX, centerY;
centerX = x + textwidth/2;
centerY = y - 35/2;
float dirX = centerX - mouseX;
float dirY = centerY - mouseY;

boolean inside =false;
if ((mouseX >= x && mouseX <= x+textwidth) &&
(mouseY >= y-35 && mouseY <= y)) {
inside=true;
}

PVector distance = new PVector(dirX, dirY);
float d = distance.mag();

println(distance +" norm");
if (d<textwidth*.75) {
distance.normalize();
xAccel = dirX*.005*(1/distance.mag());
yAccel = dirY*.005*(1/distance.mag());
}

boolean tickle=false;
if (mousePressed == true && inside == true) {
tickle=true;
xAccel=speedX=0;
yAccel=speedY=0;
}

for (int i=0; i<word.length(); i++) {
char c = word.charAt(i);

float letterX=x+textWidth(c)*i;
float tickleX = 0;
float tickleY = 0;
if (tickle) {
//tickleX=noise();
tickleY= noise((.5*frameCount+i)*.5)*30-15; //sin(frameCount*0.2+(i+1))*5;
}
text(c, letterX+tickleX, y+tickleY);
}

//text(word, x, y);
x += speedX;//random(-2, 2);
y += speedY;//random(-2, 2);
speedX *= .8;
speedY *= .8;

speedX += xAccel;
speedY += yAccel;
xAccel = yAccel= 0;

//bounce off the edges
if (y<35)
speedY*=-1;
if (y>height)
speedY*=-1;
if (x<0)
speedX*=-1;
if (x>width-textwidth)
speedX*=-1;
}

void serialEvent(Serial myPort) {
String x = myPort.readStringUntil('\n');
x = trim(x);
if (x!= null) {
int values[] = int(split(x, ','));
mouseX = (int)map(values[0], 0, 1023, 0, width);
mouseY = (int)map(values[1], 0, 1023, 0, height);
}

myPort.write('0');
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *