For this assignment all wee need to hook up some analog sensors and get values from them. I worked wit Asli for this one. I used an accelerometer (x-y) and a piezo. To make it more fun, I put the piezo on my Adams apple and accelerometer to my head and create a game. You need to get as many target as you can get in a minute. It is very simple when you start making sounds, your Adams apple vibrates and piezo gets values, this moves your cursor to right. and if you move your head up and down it moves your cursor down, and if you shake your head it picks up the target. If you are in the wrong place it creates a new target and your cursor goes to its initial point.

initial game screen (cursor is the white ellipse on the first circle)

trying to hit the target (cursor is on the 3rd row and 4th column)

piezo sensor

accelerometer
This is the arduino code for sensors:
/////////////Rest of You Assignemt 1////////////////////
/*
by Mustafa Bagdatli
09/21/2009
ITP fall 2009
*/
int analogPin = 0;
int analogValue = 0; // outgoing ADC value
int analogPin2 = 1;
int analogValue2 = 0; // outgoing ADC value
int analogPin3 = 2;
int analogValue3 = 0; // outgoing ADC value
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read analog input, divide by 4 to make the range 0-255:
analogValue = analogRead(analogPin);
Serial.print(analogValue, DEC);
analogValue2 = analogRead(analogPin2);
Serial.print(“,”);
Serial.print(analogValue2, DEC);
analogValue3 = analogRead(analogPin3);
Serial.print(“,”);
Serial.print(analogValue3, DEC);
Serial.println();
// pause for 10 milliseconds:
// delay(10);
}
and this is the processing code for the game:
/////////////Rest of You Assignemt 1////////////////////
/*
by Mustafa Bagdatli
09/21/2009
ITP fall 2009
*/
import processing.serial.*;
Serial port; // The serial port
color c1;
int check = 0;
int preCheck = 0;
int[] sensorValues = new int[3]; // array to hold the sensor values
int d = 100;
int numXCircle;
int numYCircle;
int[] xCoor;
int[] yCoor ;
PFont font;
int xChoose;
int yChoose;
// The font must be located in the sketch’s
// “data” directory to load successfully
Boolean start = false;
color[] colorFill;
float xControl = 0.0;
float yControl = 0.0;
Boolean hit = false;
Boolean hitCheck = false;
int hitTime = 0;
int m = 60;
void setup() {
size(800, 650); // Stage size
font = loadFont(“Helvetica-20.vlw”);
textFont(font);
port = new Serial(this, Serial.list()[0], 9600); //you can pull the name out of the list
port.bufferUntil(‘\n’);
// clear the serial buffer:
port.clear();
// Print a list of the serial ports, for debugging purposes to find out what your ports are called:
//println(Serial.list()); //
//noFill();
numXCircle = width / d;
numYCircle = (height / d);
xCoor = new int [numXCircle];
yCoor = new int[numYCircle];
colorFill = new color[numXCircle*numYCircle];
int x = d/2;
int y = d;
int yNum = 0;
int num = 0;
for(int i = 0; i < numXCircle; i++){
colorFill[num] = color(random(0,230),random(0,230),random(0,230));
fill(colorFill[num]);
ellipse(x, y, d, d);
num++;
xCoor[i] = x;
yCoor[yNum] = y;
x = x+d;
if( i+1 == numXCircle && yNum != numYCircle ){
yNum++;
if(yNum != numYCircle){
i = -1 ;
}
y = y+d;
x = d/2;
}
}
//fill(255);
xChoose = (int)random(0, numXCircle);
yChoose = (int)random(0, numYCircle);
println(xChoose);
text(“x”, xCoor[xChoose]-5, yCoor[yChoose]+5);
smooth();
}
void draw() {
control();
int num = 0;
background(0);
if(m ==0){
text(“Game Over”, width – 400, 30);
xControl = 0;
yControl = 0;
}
else{
m = (int)(60 – millis()*0.001);
}
fill(255);
text(“time: “+m, width-200, 30);
text(“score: “+ hitTime, width- 100, 30);
// if( start == false){
int x = d/2;
int y = d;
int yNum = 0;
for(int i = 0; i < numXCircle; i++){
// fill(random(0,230),random(0,230),random(0,230));
fill(colorFill[num]);
ellipse(x, y, d, d);
num++;
x = x+d;
if( i+1 == numXCircle && yNum != numYCircle){
yNum++;
if(yNum != numYCircle){
i = -1 ;
}
y = y+d;
x = d/2;
}
}
fill(255);
text(“x”, xCoor[xChoose]-5, yCoor[yChoose]+5);
//start = true;
//}
fill(240);
ellipse((d/2)+xControl,d+yControl,10,10);
float xEl = (d/2)+xControl;
float yEl = d+yControl;
if(hitCheck == true){
if(abs(xCoor[xChoose] – xEl) < 20 && abs(yCoor[yChoose]- yEl)<20){
hit = true;
}
}
if(hitCheck == true){
if(hit == true){
hitTime ++;
xControl = 0;
yControl = 0;
xChoose = (int)random(0, numXCircle);
yChoose = (int)random(0, numYCircle);
}
else{
xControl = 0;
yControl = 0;
xChoose = (int)random(0, numXCircle);
yChoose = (int)random(0, numYCircle);
hit = false;
}
println(hitCheck);
}
preCheck = check ;
hitCheck = false;
}
void serialEvent(Serial port) {
// read the serial buffer:
String myString = port.readStringUntil(‘\n’);
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ‘,’));
// make sure you have enough readings:
if (sensors.length >= sensorValues.length) {
// put the readings into the global array:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
if (sensorNum < sensorValues.length) {
sensorValues[sensorNum] = sensors[sensorNum];
}
}
}
}
}
void control() {
println(sensorValues[2]);
if (sensorValues[1] > 600) {
yControl = yControl + 5;/////sensor val1
}
else if(sensorValues[0] > 0){
xControl = xControl +2*sensorValues[0];//////sensor Val0
}
else if (sensorValues[2] > 900){
hitCheck = true;////sensor val2
}
}