Lighting FSR

Submitted by elee on Wed, 02/27/2013 - 10:05
elee's picture

Description

I used the photocell to determine the color of the LEDs, which in turn are represented in my Processing program. The FSR was used to determine the size of the circle.

http://youtu.be/2Kdq9qOcyDA

 

Materials

  • [1x] Arduino Uno
  • [1x] 10k Resistor
  • [3x] 220 Ohm Resistors
  • [1x] Force-Sensing Resistor (FSR)
  • [1x] Photo-cell
  • DVD + Penny + Soft Spongy Material
  • Many wires.

Code

Arduino:

 

 
// Output
int redPin   = 10;
int greenPin = 11;
int bluePin  = 9;
 
// Input
int sensorPin = A0;
int sensorValue = 0;
 
int fsrPin = A4; // Force Sensitive Resistor
int fsrVal = 0;
 
int photoPin = A0;
int photoVal = 0;
 
int r = 255;
int g = 1;
int b = 1;
 
int i = 0;     // Loop counter
int runningSum = 0;
 
int wait = 10; // 50ms (.05 second) delay; shorten for faster  fades
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial
 
int myMax = 30; // observed 'maximum' value
 
void setup()
{
  pinMode(redPin,   OUTPUT); // these are actually output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT); 
  if (DEBUG) {
  }
  Serial.begin(9600);
}
 
int* photoToRGB(int photoVal) {
  
  
}
 
// Main program
void loop()
{
  // int switchValue = digitalRead(13);
  // Serial.println(switchValue);
  
  int photoVal = analogRead(photoPin);
  
  int r = 1;
  int g = 1;
  int b = 1;
  float fraction = 1;
  if (photoVal < 255) {
    fraction = float(photoVal) / 255;
    g = fraction * 255;
    if (g >= 255) { g = 255; }
    r = 255 - g;
    if (r <= 0) { r = 1; }
    b = 1;
  }
  else if (photoVal < 509) {
    fraction = float(photoVal % 255)/255;
    b = fraction * 255;
    if (b >= 255) { b = 255; }
    g = 255 - b;
    if (g <= 0) { g = 1; }
    r = 1;
  }
  else if (photoVal < 763) {
    fraction = float(photoVal % 255)/255;
    r = fraction * 255;
    if (r >= 255) { r = 255; }
    b = 255 - r;
    if (b <= 0) { b = 1; }
    g = 1;
  }
  else {
    r = 255;
    b = 1;
    g = 1;
  }
  
  fsrVal = analogRead(fsrPin);
  
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);
  
  if (i < 10) {
    runningSum += fsrVal;
    if (float(runningSum) / i < 10) {
      // Serial.print(fsrVal);
      Serial.print(0);
      Serial.print(' ');
      Serial.print(r);
      Serial.print(' ');
      Serial.print(g);
      Serial.print(' ');
      Serial.print(b);
      Serial.println();
    }
    else if (float(runningSum) / i < 70) {
      Serial.print(fsrVal);
      Serial.print(' ');
      Serial.print(r);
      Serial.print(' ');
      Serial.print(g);
      Serial.print(' ');
      Serial.print(b);
      Serial.println();
    }
    i += 1;
  }
  else {
    runningSum = 0;
    i = 0;
  }
  delay(20);
}
 

 

Processing:

 

/*
 * 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.
 *
 * Created 25 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/ 
 */
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbmodem411"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
void setup() {
  size(700,700);
  frameRate(10);
  smooth();
  background(40,40,40);
  noStroke();
  port = new Serial(this, portname, 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, int red, int green, int blue) {
  for (int i=0; i<100; i++ ) {
    fill(red,green,blue);
    //fill(255-i,i,240);
    ellipse(x,y,r,r);
  }
 
}
 
// called whenever serial data arrives
void serialEvent(Serial p) {
  int c = port.read();
  if (c != lf && c != cr) {
    buf += char(c);
  }
  if (c == lf) {
    //int val = int(buf);
    println(buf);
    int x = int(random(0,width));
    int y = int(random(0,height));
    String parts[] = buf.split(" ");
    int force = int(parts[0]);
    int r = int(parts[1]);
    int g = int(parts[2]);
    int b = int(parts[3]);
    if (buf.substring(0,1).equals("0")) {
      drawball(x,y,5,r,g,b);
    }
    else {
      drawball(x,y,force,r,g,b);
    }
    //drawball(x,y,val);
    buf = "";
    //background(40,40,40);  // erase screen
  }
}
 

 

 

 

 

IMG_20130227_134554_0.jpg
IMG_20130227_134606.jpg
IMG_20130227_134621.jpg
IMG_20130227_134629.jpg
IMG_20130227_134637.jpg
0
Your rating: None
Drupal theme by Kiwi Themes.