Lab 4 FSR, Bouncy Balls, Music, and Color

Description

Video

I made a bouncy balls animation where the FSR can control the gravity of the bouncy balls. I’ve attached a youtube video. Music is accompanied with the bouncy ball experienced. Also I added features that will let users control the color of the balls through mouse and control the color of backgrounds through keyboards. The user can also control the spring of the balls through up and down keys.

I didn’t focus as much on the mechanical part of the lab because I spent too much time playing with the animation and digging into the tutorial. Right now, I’m just controlling the force through my hands. But I definitely had a lot of fun playing with this lab and am looking forward to apply some mechanical concepts in the future.

Processing Code

import processing.serial.*;
import processing.sound.*;
SoundFile file;

// 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;
float r = 0;
float g = 0;
float b = 0;

int numBalls = 40;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];

int barWidth = 5;
int lastBar = -1;

void setup() {
size(800, 800);
for (int i = 0; i < numBalls; i++) { balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls); } noStroke(); file = new SoundFile(this, "spaceBeat.wav"); file.play(); port = new Serial(this, portname, 9600); } void draw() { background(r, g ,b); for (Ball ball : balls) { ball.collide(); ball.move(); ball.display(); } if (keyPressed == true && keyCode == UP) { spring += 0.01; } else if (keyPressed == true && keyCode == DOWN) { spring -= 0.01; } else if (keyPressed == true && key == 'r') { r += 10; } else if (keyPressed == true && key == 'R') { r -= 10; } else if (keyPressed == true && key == 'g') { g += 10; } else if (keyPressed == true && key == 'G') { g -= 10; } else if (keyPressed == true && key == 'b') { b += 10; } else if (keyPressed == true && key == 'B') { b -= 10; } else if (keyPressed == true && key == 'S' || key == 's') { friction = -0.9; spring = 0.05; r=0; g=0; b=0; } int whichBar = mouseX / barWidth; if (whichBar != lastBar) { int barX = whichBar * barWidth; fill(barX, mouseY, 66); rect(barX, 0, barWidth, height); lastBar = whichBar; } } class Ball { float x, y; float diameter; float vx = 0; float vy = 0; int id; Ball[] others; Ball(float xin, float yin, float din, int idin, Ball[] oin) { x = xin; y = yin; diameter = din; id = idin; others = oin; } void collide() { for (int i = id + 1; i < numBalls; i++) { float dx = others[i].x - x; float dy = others[i].y - y; float distance = sqrt(dx*dx + dy*dy); float minDist = others[i].diameter/2 + diameter/2; if (distance < minDist) { float angle = atan2(dy, dx); float targetX = x + cos(angle) * minDist; float targetY = y + sin(angle) * minDist; float ax = (targetX - others[i].x) * spring; float ay = (targetY - others[i].y) * spring; vx -= ax; vy -= ay; others[i].vx += ax; others[i].vy += ay; } } } void move() { vy += gravity; x += vx; y += vy; if (x + diameter/2 > width) {
x = width - diameter/2;
vx *= friction;
}
else if (x - diameter/2 < 0) { x = diameter/2; vx *= friction; } if (y + diameter/2 > height) {
y = height - diameter/2;
vy *= friction;
}
else if (y - diameter/2 < 0) { y = diameter/2; vy *= friction; } } void display() { ellipse(x, y, diameter, diameter); } } // called whenever serial data arrives void serialEvent(Serial p) { int c = port.read(); if (c != lf && c != cr) { buf += char(c); } if (c == lf) { SerialVal = int(buf); println("val="+SerialVal); gravity = SerialVal/1000; println("gravity="+gravity); buf = ""; } }

Arduino Code

/* Photocell simple testing sketch.

Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 11 through a resistor to ground
For more information see http://learn.adafruit.com/photocells */

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 11; // 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) {
photocellReading = analogRead(photocellPin);

// 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
photocellReading = 1023 - photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 1023, 255, 0);
analogWrite(LEDpin, LEDbrightness);

Serial.println(photocellReading);

delay(100);
}

Leave a Reply