Lab 4

Chelsey's picture

 

Description

  • Used Arduino to control a modified version of the Bounce sketch in Processing using a photocell, an FSR, and a potentiometer. A ball is bouncing around the screen, reversing its direction when it hits walls. The potentiometer controls both the red value of the bouncing ball and the size of the ball. The FSR controls the green value and the speed of the ball. The photocell controls the blue value of the ball color.
  •  
  • I used a sponge to attach to the FSR. I cut a hole laterally into the sponge and slid the FSR in. When a user presses on or below the FSR (towards the wires), it activates. However, when the user presses on the upper half of the sponge (away from the wires), it does not activate.

Components Used

  • Resistors
  • Potentiometer
  • FSR
  • Photocell

Arduino Code

 

/*
  This example reads three analog sensors (one potentiometer, one FSR
  and one photocell) and sends their values serially. The Processing program
  corollary then takes those three values and use them to change the
  size and color of the ball in the Bounce program.
 
 The circuit:
 * potentiometer attached to analog input 0
 * FSR attached to input 3
 * Photocell attached to input 2
 
 Based on:
 http://www.arduino.cc/en/Tutorial/VirtualColorMixer
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe and Scott Fitzgerald
 
 */
 
const int potPin = 0;
const int fsrPin = 3;
const int pcPin = 2;
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  Serial.print(analogRead(potPin));
  Serial.print(",");
  Serial.print(analogRead(fsrPin));
  Serial.print(",");
  Serial.println(analogRead(pcPin));
}
 

Processing Code

/**
 * 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); 
 }
 }
 }
Lab 4 FSR diffuser
0
Your rating: None