Processing code
/*
Modified code based on source from
http://cs.unk.edu/~csis495/2011/02/helicopter-game-workshop/
*/
import processing.serial.*;
boolean OUT_OF_BOUNDS = false;
final int w = 1000; // Width of the window
final int h = 400; // Height of the window
// Floor and ceiling lines
final int lineLength = w; // One value per pixel
int[] upperLine = new int[w]; // Line for ceiling
int[] lowerLine = new int[w]; // Line for floor
// Helicopter
PImage chopper; // Kickass chopper sprite
int chopperX = w/2; // X position of helicopter
int chopperY = 80; // Y position of helicopter
Serial port; // Create object from Serial class
int pValue = 0; // Value of SoftPot
int previousPValue = 0; // Previous potValue for comparison
void setup() {
size(w,h);
// Load the helicopter sprite
chopper = loadImage("chopper.png");
// Generate a new terrain
for(int i=0; i<lineLength; i++)
shiftTerrain();
String portName = "COM3";
port = new Serial(this, portName, 57600);
port.bufferUntil('\n');
}
void draw() {
if( OUT_OF_BOUNDS ) background(255,0,0);
else background(0);
drawTerrain(); // Draw the terrain
shiftTerrain(); // Move the terrain to the left
// Draw the helicopter
image(chopper, chopperX, chopperY);
// Check that the chopper is between the lines
if( (chopperY+25 > lowerLine[chopperX+48]) || (chopperY < upperLine[chopperX+48]) )
OUT_OF_BOUNDS = true;
else
OUT_OF_BOUNDS = false;
chopperY = pValue;
}
// Shifts the entire terrain to the left, adds new entry to right
void shiftTerrain() {
// Shift the lines to the left one entry
for(int i=0; i<lineLength-1; i++) {
upperLine[i] = upperLine[i+1];
lowerLine[i] = lowerLine[i+1];
}
// Add a new entry at the end of each line (note the +=)
upperLine[lineLength-1] += (int) random(-3,3);
lowerLine[lineLength-1] += (int) random(-3,3);
// Position the lines on the screen
upperLine[lineLength-1] = constrain(upperLine[lineLength-1],50,95);
lowerLine[lineLength-1] = constrain(lowerLine[lineLength-1],125,155);
}
// Renders the terrain
void drawTerrain() {
// Draw the ceiling and floor
for(int i=0; i<lineLength-2; i++) {
// Draw vertical lines (blue)
stroke(100,150,255);
line(i,upperLine[i], i, 0);
line(i,lowerLine[i], i, h);
// Draw white border
stroke(255);
line(i, upperLine[i], i+1, upperLine[i+1]);
line(i, lowerLine[i], i+1, lowerLine[i+1]);
}
}
void serialEvent(Serial port)
{
pValue = (int)map(float(port.readStringUntil('\n')), 0, 1775, 40, 60);
}
Arduino code
/*
* force sensor with smooth sampling
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
# define SAMPLES 10
int potPin = 0; // input pin for the FSR
int ledPin = 11; // the pin for the LED
void setup() {
Serial.begin(57600);
}
void loop() {
int val = 0; // variable to store the value coming from the sensor
for(int i=0; i<SAMPLES; i++) {
val += analogRead(potPin);
}
Serial.println(val);
val = map(val/SAMPLES, 0, 1024, 0, 255);
analogWrite(ledPin, val); // analogWrite can be between 0-1024*SAMPLES
delay(50);
}