Description:
I developed code which visualizes a simple flock of birds (or shooting stars) that converge at a point. The point is controlled with values from two sensors, giving an x (from sensor 1) and y value (from sensor 2) for the xy-point.
I also found a spray mechanism to use as the mechanical construction to use with the FSR, where the force applied is the top where you push the mechanism to spray.
Components used:
-
2 photocells
-
2 10k ohm resistors
Code:
Processing
/*
* Jennifer Wang
* HW 4 Processing visualization with arduino sensor input
* 09/21/11
*
* Takes in values from 2 sensors, and produces a simple bird simulation where the "birds"
* follow and converge at a point. Both sensors determine the point on the screen,
* by providing an x (from sensor 1) and a y value (from sensor 2).
*
* Bird simulation adapted from Brendan Till.
*
* First, the sensors must be calibrated by the user, since the environment may change
* the range of the sensors. This is done hard-coded in the code below.
*
* Use with the arduino code "hw4_twosensorreader" for correct serial input.
*
*/
import processing.serial.*;
String portname = "/dev/tty.usbmodem411"; // or "COM5"
Serial myPort;
int[] sensorVals; // 2-dimensional array to hold raw x and y values coming from sensors
float convergeX; // calibrated x value of converging point
float convergeY; // calibrated x value of converging point
int nBirds; // number of birds
Bird[] birdArray; // holds all the birds
int birdSize; // length of bird
// ** be sure to calibrate low and high values of the sensor HERE **
int sensor1Low = 200;
int sensor1High = 800;
int sensor2Low = 150;
int sensor2High = 750;
void setup() {
size(300,300); // set size of window -- used for calibration of sensor input values
frameRate(10);
smooth();
background(0);
noStroke();
myPort = new Serial(this, portname, 9600);
// converge point starts in the middle
convergeX = width/2;
convergeY = height/2;
birdSize = 3;
nBirds = 30; // start with 30 birds
birdArray = new Bird[nBirds];
for (int i = 0; i < nBirds; i++) {
birdArray[i] = new Bird();
}
}
void draw() {
background(0);
convergePoint(convergeX, convergeY);
}
void keyPressed() {
if(key == ' ') {
background(0); // erase screen
}
}
// draws the point to converge at (yellow dot) and all the birds going toward that point
void convergePoint(float x, float y) {
// create point where birds are converging
noStroke();
fill(150, 150, 0); // colors point yellow
ellipse(x, y, 10, 10);
// draw birds
for (int i = 0; i < nBirds; i++) {
birdArray[i].display();
}
}
void serialEvent(Serial p) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to integer array that carries two ints for valx and valy
// raw sensor values: sensorVals[0] is from sensor 1, sensorVals[1] is from sensor 2
sensorVals = int(split(inString, ","));
println(sensorVals[0]+","+sensorVals[1]);
// calibrating values to fill full range of screen
// sensor 1
if (sensorVals[0] > sensor1High) { // above max of sensor value range
sensorVals[0] = sensor1High;
}
if (sensorVals[0] < sensor1Low) { // below min of sensor value range
sensorVals[0] = sensor1Low;
}
// sensor 2
if (sensorVals[1] > sensor2High) { // above max of sensor value range
sensorVals[1] = sensor2High;
}
if (sensorVals[1] < sensor2Low) { // below min of sensor value range
sensorVals[1] = sensor2Low;
}
// x position from sensor 1
convergeX = width*(sensorVals[0] - sensor1Low)/(sensor1High - sensor1Low);
// y position from sensor 2
convergeY = height*(sensorVals[1] - sensor2Low)/(sensor2High - sensor2Low);
}
}
class Bird {
float x;
float y;
float direction;
Bird() {
x = random(0, width);
y = random(0, height);
direction = random(-PI, PI);
}
void display() {
// if position of bird is furher than 2 pixels from converge point
// make bird move towards converge point
if (abs(x - convergeX) > 2 || abs(y - convergeY) > 2) {
direction = atan2(convergeY - y, convergeX - x);
y += sin(direction);
x += cos(direction);
stroke(255);
line(x, y, x - birdSize*cos(direction), y - birdSize*sin(direction));
}
// if position of bird is within 2 pixels of converge point, make it
// disappear and reappear in a random place
else {
x = random(0, width);
y = random(0, height);
direction = random(-PI, PI);
}
}
}
Arduino
/*
* Sensor reader
* Jennifer Wang
* September 21, 2011
*
* Reads value from sensor at pin 0
*
*/
int sensorPin1 = 0; // select the input pin for the potentiometer
int sensorPin2 = 1;
int valx = 0; // variable to store the value coming from the sensor
int valy = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
valx = analogRead(sensorPin1); // read the value from the sensor, between 0 - 1024
valy = analogRead(sensorPin2); // read the value from the sensor, between 0 - 1024
Serial.print(valx);
Serial.print(",");
Serial.println(valy);
//Serial.println(valx+","+valy);
}