Description:
The goal of this lab was to learn to use two new analog sensors (photocell & FSR) and also the programming language 'Processing.'
In Part 1 of the lab assignment, the circuit from the previous lab was modified to include a photocell. In Part 2, a FSR was added to the circuit.
In Part 3, "Processing" was downloaded and the example program "Bounce" was run. Example code provided for the class was used. I modified the code so that it communicates with the correct serial port.
In Part 4, I modified the example code for 'Bounching Bubbles' so that the color of the balls changes from black to white. The original example code was not designed to take inputs from the keyboard or arduino. In future work with this I'd like for the balls to change from different colors.
For a mechanical construction for the FSR that distributes the physical force that is applied, I used a pocket pack of tissues. I embedded the FSR in the middle of the layers.
Note: due to the limitation of colors of the wire, in the pictured circuit the green wire connects GND to the breadboard for the FSR and the orange wire connects the 5V supply to the breadboard for the FSR.
Components Used:
1 - Arduino Uno
1 - Breadboard
1 - 220-ohm Resistor
1 - 10K Resistor
1 - LED
1 - FSR or Photocell
wires
Code:
Part 3:
/*
* Arduino Ball Paint
* (Arduino Ball, modified 2008)
* ----------------------
*
* Draw balls randomly on the screen, size controlled by a device
* on a serial port. Press space bar to clear screen, or any
* other key to generate fixed-size random balls.
*
* Receives an ASCII number over the serial port,
* terminated with a carriage return (ascii 13) then newline (10).
*
* This matches what Arduino's " Serial.println(val)" function
* puts out.
*
* The code in this lab assignment is modified from code created 25 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
import processing.serial.*;
String buf="/dev/tty.usbmodem1421";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
size(600,600);
frameRate(10); //this is saying 10 times per second, raise this value
smooth();
background(40,40,40);
noStroke();
println(Serial.list());
port = new Serial(this, Serial.list()[6], 9600);
}
void draw() {
}
void keyPressed() {
if(key == ' ') {
background(40,40,40); // erase screen
}
else {
int x = int(random(0,width));
int y = int(random(0,height));
drawball(x,y, 50);
}
}
// draw balls
void drawball(int x, int 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 c = port.read();
/* this print statement will allow you to see whether or not Processing can "hear" anything that
Arduino is sending. It should print all the changing values of the FSR or Photocell in your circuit
If nothing is printing, you probably picked the wrong serial port.
*/
println(c);
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
int val = int(buf);
println("val="+val);
int x = int(random(0,width));
int y = int(random(0,height));
drawball(x,y,val);
buf = "";
background(40,40,40); // erase screen
}
}
Part 4:
import processing.serial.*;
/*The code in this part of the lab assignment is modified from:
* Bouncy Bubbles
* based on code from Keith Peters.
*
* Multiple-object collision.
*/
Serial port;
int FSRvalue = 0;
int numBalls = 12;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
void setup() {
size(640, 360);
println(Serial.list());
port = new Serial(this, Serial.list()[6], 9600);
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls);
}
noStroke();
fill(255, 204);
}
void draw() {
background(0);
for (int i = 0; i < numBalls; i++) {
balls[i].collide();
balls[i].move();
balls[i].display(FSRvalue);
}
}
// called whenever serial data arrives
void serialEvent(Serial p) {
FSRvalue = port.read();
println(FSRvalue);
}
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(int c) {
fill (c);//this is where color is defined, right now it's only changing from black to white
ellipse(x, y, diameter, diameter);
}
}
- Login to post comments