# Spaceship bouncing

## Description:

For this exercise I worked with a force sensor and LED light to create a processing visual. The force sensor, when pressed, turns off the LED light. The visual is of a bouncing ball, and the gravity of how much the ball is able to bounce is increased when the sensor is pressed. My diffuser is a spaceship.

My force sensor is much like “turning off” the thruster on a spaceship – when the force sensor is on, the spaceship thruster (LED light) is off, and the ball falls to the floor. When the force sensor is not pressed, the ball bounces normally and the spaceship “thrusters” are on.

I tried to have my visual design be a series of cubes that would increase based on the force sensor input, but I found that working with classes in processing is difficult, so instead I opted to edit the bouncing ball example and adjust the gravity based on sensor input.

## Components:

• Arduino Uno
• Breadboard
• 1x force sensor
• 1x LEDs (red)
• 1x 220Ω resistors
• 1x 10Ω resistor
• jumper wires
• USB cable
• computer

## Arduino Code:

``````
int forcePin = A0; // the cell and 10K pulldown are connected to a0
int forceReading; // the analog reading from the sensor divider
int LEDpin = 13; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}

void loop(void) {
forceReading = analogRead(forcePin);

//Serial.print("Analog reading = ");
//Serial.println(forceReading); // the raw analog reading

// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
forceReading = 1023 - forceReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(forceReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness*4);
Serial.println(forceReading);
}
``````

## Processing Code:

``````
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodem1421"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;
int serial_factor = 0;
PVector location; // Location of shape
PVector velocity; // Velocity of shape
PVector gravity; // Gravity acts at the shape's acceleration

void setup() {
size(640,640);
location = new PVector(100,100);
velocity = new PVector(1.5,2.1);
gravity = new PVector(0,0.2);
port = new Serial(this, portname, 9600);
}

void draw() {
background(0);

// Add velocity to the location.
location.add(velocity);
// Add gravity to velocity
velocity.add(gravity);

// Bounce off edges
if ((location.x &gt; width) || (location.x &lt; 0)) {
velocity.x = velocity.x * -1;
}
if (location.y &gt; height) {
// We're reducing velocity ever so slightly
// when it hits the bottom of the window
velocity.y = velocity.y * -0.95;
serial_factor = serialVal/30000;
if (serialVal &gt; 1000) {
gravity.y = .3;
}
if (serialVal &lt; 1000 ) {
gravity.y = 1.2;
}
location.y = height;
}

// Display circle at location vector
stroke(255);
strokeWeight(2);
fill(127);
ellipse(location.x,location.y,48,48);
}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf &amp;&amp; c != cr) {
buf += char(c);
}
if (c == lf) {
serialVal = int(buf);
println("val="+serialVal);
buf = "";
}
}
``````