Stupid Pet Trick

It took me a while to come up with an idea for my project. I took out all of the sensors that I had in my tool box and tried to think of away to incorporate them into a project. The photocell stood out too me. It's easy to control light. I was also interested in learning how to combine arduino with processing. I decided to make a game that has the player use a photocell as his or her controller. I broke my project up into 3 steps.


Step 1) Figure out how to get the photocell to dim the LED light
I needed processing to be able to read a range of numbers coming from the photocell. I wanted the player to be able to move back and fourth. It was easy figuring out how to turn an LED on and off using a photocell but dimming the LED was more of a challenge. At first I tried using Firmata to get my arduino board to communicate with processing but I found it too difficult to work with. I used serial communication instead. I used this YouTube video as a reference.




Step 2) Move a ball using a photocell



This step turned out to be the hardest. I decided to use a sketch that I had already made in IMM120. My original sketch moved a picture of a smiley face across the screen. I had to figure out how to make the smiley face move based on data processing received from the photocell. This involved a lot of trouble shooting. I had to make sure my arduino code, my processing code and my breadboard were all correct. My proccessing code gave me the most issues. "Learning Processing:" by Daniel Shiffman gave me a lot more information on serial communication.

Step 4) Create game.



It took me a lot more time to figure out how to move the smiley face than I had originally planned. I had planned to make a much more complex game but I couldn't due to time constraints. The object of the game is to catch as many circles as you can with your paddle. Every circle you catch raises your score two points.

Controller
When I presented my game to the class I ran into some issues. My game was designed based on the dim lighting in my room. The classroom had much more brighter lights. I had difficulty controlling the data from the photocell when I played the game in the classroom. I had to alter some of the code in processing to accommodate the brighter lighting. I realized how annoying it would be for the user to constantly have to change code depending on what room they were in. I created a small box in order to make the game a little more universal. The box goes over the photocell. The box helps to limit the amount of lighting the photocell reads. It works best in brightly lit rooms. If you play the game in a dimly lit room it is not needed.

In the future I hope to create a more complex game using the photocell controller. I am working on a bird game. The user would have to control the flight of he bird by flapping his or her hand over the photocell.


Processing Code
import processing.serial.*;
import cc.arduino.*;
Serial myPort; // The serial port
PFont f;

ArrayList smiles;
int score = 0;
void setup()
{
size(600,620);

//arduino
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil('\n');;
//arduino
smiles = new ArrayList ();
smiles.add(new Smile());

f = createFont("Futura-Medium", 24, true);

}
void draw()
{

//arduino
String myString = myPort.readStringUntil('\n');
if (myString != null) {
String[] sensors = split(myString, ',');
float inByte = float(sensors[0]);
println(inByte);
inByte = map(inByte, 0, 1023, 0, height);

//background
background(38,171,23);
strokeWeight(4);
stroke(color(255,255,255));
line(300,620 , 300,1);

//paddle
fill(color(2,0,0));
stroke(color(2,0,0));
rect(20, inByte,15,110);


// Display score
textFont(f, 24);
fill(0);
text("Score: " + score, 25, 35);

int hatch = (int) random(1, 5);

if((hatch == 1) && (smiles.size() < 50))

{

smiles.add(new Smile());
}

for (int i = 0; i < smiles.size(); i++)

{
Smile smile = (Smile) smiles.get(i);
smile.move();
smile.display();
if(smile.Xpos < 0)
{
// Remove enemy
smiles.remove(i);
}

float distance = dist(smile.Xpos,smile.Ypos, 15,inByte);

if (distance < 50)

{
score = score +2;
smiles.remove(i);
}

}
}


}

smile class

class Smile

{
//set up our properties


float n = random(0, height);
float Xpos;
float Ypos;
float Xspeed;
float g;
boolean isAlive = true;
PImage Smile;
PImage Angry;
//show= display
int show = 0;
//totaltime= tttime
int TTime =1000;
//dtime=savedtime
int dTime = millis();


//populate our properties in the constructor

Smile()
{

Xpos = width ;
Ypos = random (0, height);
Xspeed = random(1, 5);

g = 1.5;
}

void move ()
{
n = n + 0.002;
Xpos = Xpos - Xspeed;
Ypos = noise(n)*height;

int goneTime = millis() - dTime;
if (goneTime > TTime)
{
show++;
dTime = millis();
}
//every 20 seconds speed of drop bomb goes up
if (show== 20)
{
g = g++;
show = 0;
}

}


void display()
{

ellipse(Xpos, Ypos, 15, 15);

}

}

Arduino Code
void setup() {
// initialize the serial communication:
Serial.begin(9600);
}

void loop() {
// send the value of analog input 0:
Serial.println(analogRead(A0));

delay(10);
}












No comments:

Post a Comment