Description:
Move around with the potentiometer from the left to the right. With the FSR you can shoot. The photocell is used to cheat because covering the cell so that it gets dark would slow down the game while you could still move fast. The object used to distribute the force on the FSR is a sponge, which works surprisingly well.
Parts used:
1 Potentiometer
1 FSR
1 Photocell
3 LEDs
3 220 Ohm resistors
2 10k resistors
The Arduino code:
int lightPin = 0;
int forcePin = 1;
int potPin = 2;
int ledPinRed = 9;
int ledPinGreen = 10;
int ledPinBlue = 11;
int valPot = 0;
int valLight = 0;
int valForce = 0;
int i = 0;
int wait = (1000);
int checkSum = 0; // The sum of the values of the 3 LEDs
int prevCheckSum = 0; // The previous sum of the values of the 3 LEDs
int sens = 3; // A threshold so that it is possible to see whether the values of the LED has changed enough
void setup() {
// Set the LED port
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinGreen, OUTPUT);
pinMode(ledPinBlue, OUTPUT);
// Start Serial to write to the monitor
Serial.begin(9600);
}
void loop() {
i++; // Loop
// Read the value of the pots
valPot = analogRead(potPin) / 2; // Creata a range from 0 to 512
valLight = analogRead(lightPin);
valForce = analogRead(forcePin);
// valLed selects the LED that I want to change
// RED = 0 - 340
// GREEN = 341 - 682
// BLUE = 683 - 1024
if(valPot < 341) {
// Modify the red led
analogWrite(ledPinRed, valLight);
delay(valForce);
analogWrite(ledPinRed, 0);
delay(valForce);
}
else if(valPot >= 341 && valPot < 683) {
// Modify the green led
analogWrite(ledPinGreen, valLight);
delay(valForce);
analogWrite(ledPinGreen, 0);
delay(valForce);
}
else if(valPot >= 683) {
// Modify the blue led
analogWrite(ledPinBlue, valLight);
delay(valForce);
analogWrite(ledPinBlue, 0);
delay(valForce);
}
String myForce = "f" + String(valForce);
Serial.println(myForce);
String myLight = "l" + String(valLight);
Serial.println(myLight);
String myPot = "p" + String(valPot);
Serial.println(myPot);
}
The processing code:
import processing.serial.*;
// Change this to the portname your Arduino board
Serial port;
String buf = "";
char command;
int shoot = 0; // used to shoot bullets
float speed = 0; // used for speed boost
float move = 0; // used to move around from left to right
int score = 0;
int life = 10;
Canon canon;
ArrayList<Bullet> bullets;
ArrayList<Enemy> enemies;
int defaultSpeed = 8;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
PFont f;
void setup() {
size(512,800);
f = createFont("Arial", 16, true);
frameRate(60);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, Serial.list()[14], 9600);
canon = new Canon((width / 2), (height - 60), 30, 60);
bullets = new ArrayList<Bullet>();
enemies = new ArrayList<Enemy>();
}
void createEnemy() {
float x = random(0, (width - 20));
float enemySpeed = random((2 * score), (8 * (score+1)));
if(enemies.size() < (score + 1)) {
Enemy enemy = new Enemy(x, 20, 20, 20, -enemySpeed);
enemies.add(enemy);
}
}
void drawText() {
textFont(f);
fill(255);
text("Life: " + life, 20, 20);
text("Score: " + score, 20, 40);
}
void draw() {
if(life > 0) {
createEnemy();
background(40, 40, 40);
drawText();
canon.update(move);
canon.draw();
for(int i = bullets.size() - 1; i >= 0; i--) {
Bullet bullet = bullets.get(i);
bullet.update(speed);
bullet.draw();
if(!bullet.inField()) {
bullets.remove(i);
}
}
for(int i = enemies.size() -1; i >= 0; i--) {
Enemy enemy = enemies.get(i);
enemy.update(-speed);
enemy.draw();
if(!enemy.inField()) {
enemies.remove(i);
life--;
}
// Check whether one of the bullets hits the enemies
for(int j = bullets.size() - 1; j > 0; j--) {
Bullet bullet = bullets.get(j);
if(enemy.collides(bullet)) {
enemies.remove(i);
bullets.remove(j);
// Increase score
score++;
break;
}
}
}
}
else {
textFont(f, 30);
fill(0, 0, 255);
textAlign(CENTER);
text("Game over", width/2, height/2);
}
}
void shoot() {
// Force is used so we need to create a new bullet
float xPos = move - (canon.getWidth() /2);
float yPos = height - canon.getHeight();
Bullet bullet = new Bullet(xPos, yPos, 5, 5, defaultSpeed);
bullets.add(bullet);
}
// called whenever serial data arrives
void serialEvent(Serial p) {
// Store previous states
int prevShoot = shoot;
// Read anything that is send to the port
int c = port.read();
// if it is not 13 or 10 store the data
if (c != lf && c != cr) {
buf += char(c);
}
// If it is 10, analyze the input
if (c == lf) {
// Only continue when the buffer is nog empty
if(buf.length() > 0) {
// The first character could tell what the sender of the
// data is (pot, light or force)
char command = buf.charAt(0);
// If the force is the sender, it is the trigger to shoot
if(command == 'f') {
shoot = int(buf.substring(1));
}
// if the ligth is the sender it is a speed boost
if(command == 'l') {
// Speed boost varies between 0 and 10
speed = (int(buf.substring(1)) / 102.4);
}
// if the pot is the sender, it is to move around
if(command == 'p') {
move = int(buf.substring(1));
}
if(prevShoot != shoot) {
// Shoot a bullet
shoot();
}
// Clear the buffer
buf = "";
}
}
}
class Object {
float xPos, yPos, mWidth, mHeight;
Object(float x, float y, float w, float h) {
xPos = x;
yPos = y;
mWidth = w;
mHeight = h;
}
public float getWidth() {
return mWidth;
}
public float getHeight() {
return mHeight;
}
public float getPosX() {
return xPos;
}
public float getPosY() {
return yPos;
}
}
class Canon extends Object {
Canon(float x, float y, float w, float h) {
super(x, y, w, h);
}
void update(float move) {
xPos = (move - mWidth);
// Canon should be within the boundries
if((move - mWidth) < 0) {
xPos = 0;
}
if(move > (width - mWidth)) {
xPos = (width - mWidth);
}
}
void draw() {
fill(255);
rect(xPos, yPos, mWidth, mHeight, 0, 0, 12, 12);
}
}
class MovableObject extends Object {
float speed;
MovableObject(float x, float y, float w, float h, float speed) {
super(x, y, w, h);
}
void update(float boost) {
yPos = yPos - (speed + boost);
}
public boolean inField() {
if(yPos < 0 || yPos > height) {
return false;
}
return true;
}
}
class Bullet extends MovableObject {
Bullet(float x, float y, float w, float h, float s) {
super(x, y, w, h, s);
}
void draw() {
fill(155, 100, 240);
ellipse(xPos, yPos, mWidth, mHeight);
}
}
class Enemy extends MovableObject {
Enemy(float x, float y, float w, float h, float s) {
super(x, y, w, h, s);
}
void draw() {
fill(255, 0, 0);
rect(xPos, yPos, mWidth, mHeight);
}
public boolean collides(Bullet bullet) {
if(bullet.getPosX() > this.getPosX() && bullet.getPosX() <
(this.getPosX() + this.getWidth()) && bullet.getPosY() >
this.getPosY() && bullet.getPosY() < (this.getPosY() +
this.getHeight())) {
// The bullet and the enemy intersect
return true;
}
return false;
}
}
- Login to post comments