and one photocell) and sends their values serially. The Processing program
size and color of the ball in the Bounce program.
by David A. Mellis
/**
* Bounce Plus.
*
* A potentiometer, FSR and photocell are all linked to the Arduino.
* In Processing, a ball in displayed traveling around the window at a fixed
* speed. When the shape hits the edge of the window, it reverses its
* direction.
*
* The colors of the ball are taken from the three inputs: red is from
* the potentiometer, green from the FSR and blue from the photocell.
* The potentiometer also controls the size of the ball and the FSR controls
* its speed.
*
* Based on Bounce, from the Processing library, and the Three Color Mixer.
**/
import processing.serial.*;
float redValue = 0; // red value
float greenValue = 0; // green value
float blueValue = 0; // blue value
float greenSpeed = 0; // create variable for greenSpeed, which will
// be linked to greenValue
int size = 20; // Width of the shape
float xpos, ypos; // Starting position of shape
float xspeed = 3; // Speed of the shape
float yspeed = 3; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
Serial myPort;
void setup()
{
size(600, 350);
noStroke();
frameRate(30);
smooth();
// Set the starting position of the shape
xpos = width/2;
ypos = height/2;
// List all the available serial ports
println(Serial.list());
// Open Serial.list()[0] becausefirst port in the serial list is Arduino.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
}
void draw()
{
background(102);
// Update the position of the shape. Make x and y speed dependent on greenSpeed.
xpos = xpos + ( greenSpeed * xdirection );
ypos = ypos + ( greenSpeed * ydirection );
// Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1. Adjusted
// x and y position by the redValue, which now affects the size of the shape.
if (xpos > width-redValue || xpos < 0) {
xdirection *= -1;
}
if (ypos > height-redValue || ypos < 0) {
ydirection *= -1;
}
// Draw the shape. Made the x and y coordinates of the shape change
// with the redValue (value of potentiometer).
fill(redValue, greenValue, blueValue);
ellipse(xpos+redValue/2, ypos+redValue/2, redValue, redValue);
}
//called whenever serial data arrives
void serialEvent(Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the
// resulting substrings into an integer array:
float[] colors = float(split(inString, ","));
// if the array has at least three elements, you know
// you got the whole thing. Put the numbers in the
// color variables:
if (colors.length >=3) {
// map colors from range 0 to 1023 to range 0-255:
redValue = map(colors[0], 0, 1023, 0, 255);
greenValue = map(colors[1], 0, 1023, 0, 255);
blueValue = map(colors[2], 0, 1023, 0, 255);
// set greenSpeed (x and y speed) equal to 3 + a mapping of greenValue
// to a 0 to 20 scale
greenSpeed = 3 + map(greenValue, 0, 255, 0, 20);
}
}
}