Description
In this lab we used two new analog sensors – photocell and force sensitive resistor. Each of these acts like a variable resistor (similar to pot). The photocell changes resistance with changes in light intensity while the FSR changes resistance due to varying forces. The circuit from the previous lab was utilized except the variable resistor was replaced with a photocell and FSR. These sensors were used to control LED functions.
We also learned about the Processing environment and its platform to utilize a variety of programming languages. We also learned to use the serial communication between the Arduino and the Processing software. Using an example open-source program I modified it to create a soccer game. The user presses the force sensor to shoot the ball. There is a goalie to block your shots. I you make a goal, the goalie moves faster and the game become more difficult. You have 3 misses before the game is over.
For the mechanical section of the homework, I constructed a force-sensing watch. Everytime you tap the watch the ball is shot.
Components
*1- Arduino Uno
*1- Breadboard
1- 220ohmResistors
*1- 10KohmResistors
1- LEDs
*1- USB Cable
1- Potentiometer
*1- Force Sensitive Resistor
1- Photocells
* Needed to play soccer game.
Arduino Code:
int val;
void setup() {
// initialize the serial communication:
Serial.begin(9600);}
void loop() {
val = analogRead(0);
Serial.println(val);
delay(10);
}
Processing Code:
// Credit: Evan Slachter
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "COM5"; // or "COM5"
Serial port;
String buf="";
int val = 0;
float goalieX;
float xspeed = 4;
float ballX= 200;
float ballY= 200;
float ballYspeed=0;
int score=0;
int miss=3;
float goalieL=20;
void setup()
{
size (400, 350);
background (35, 193, 34);
goalieX=200;
noStroke();
port = new Serial(this, portname, 9600);
}
void draw()
{
// creates soccer field
background (35, 193, 34);
strokeWeight(2);
line(120, 50, 280, 50);
line(120, 50, 120, 90);
line(280, 50, 280, 90);
stroke(255, 255, 255);
line(0, 90, 400, 90);
line(65, 90, 65, 140);
line(65, 140, 333, 140);
line(333, 140, 333, 90);
line(0, 255, 600, 255);
fill(255, 255, 255);
ellipse(200, 200, 8, 8);
fill(0);
textAlign(CENTER);
textSize(10);
textSize(20);
text("Goals Scored: "+score, 80, 275);
text("Balls Left: " + miss, 60, 300);
stroke(0);
strokeWeight(3);
//goalie moving back and forth
line(goalieX, 90, goalieX+goalieL, 90);
if (goalieX >= 280-goalieL)
{
xspeed *= -1;
goalieX= 280-goalieL;
}
if ( goalieX <= 120)
{
xspeed *= -1;
}
goalieX=goalieX+xspeed;
//soccer ball
drawball(ballX,ballY, 20);
ballY=ballY+ballYspeed;
if (ballY <=90)
{
ballYspeed = 0;
ballY = 200
if (goalieX <= 200 && goalieX >= 200-goalieL)
{
miss--;
}
else
{
score=score+1;
if ( abs(xspeed) < 20)
{
xspeed=xspeed*1.1;
goalieL= goalieL+1;
}
}
}
if (miss==0)
{
fill(35, 193, 34);
rect(0,0,width,height);
fill(255,255,255);
text("End Game", 200, 200);
text ("Reset: Spacebar", 200,250);
text("Score: " + score, 200,300);
noLoop();
}
}
void Shoot()
{
ballYspeed = -7;
}
void keyPressed()
{
if (key == ' ')
{
loop();
ballY = 200;
goalieL=20;
}
if (key==' ')
{
score=0;
miss=3;
ballY=200;
xspeed=4;
goalieX=width/2;
}
}
void drawball(float x, float y, int r) {
for (int i=0; i<100; i++ ) {
fill(255-i,i,240);
ellipse(x,y,r,r);
}
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int val = port.read();
println(val);
if (val>50)
{
Shoot();
}
}
- Login to post comments