Assignment 4: The image brigthener

Submitted by christineschantz on Tue, 02/26/2013 - 23:51

Description

In this assignment I used the 'Brightness' example from the Processing Library. I added my own picture to the example, adjusted the values in the code the picture is in colors (instead of black / white) and made the bulb/brightener a fixed value, so I can control it with my FSR. Now, the bulb/brightener change and light up the picture according to the values (or pressure) it receives from the FSR. 
 
Components 
  • Arduino
  • Breadboard
  • USB cable
  • Cables
  • Resistor 
  • Force sensing resistor (FSR)
  • Carton and tape

 

Arduino code

/*
 * one pot fades one led
 * modified version of AnalogInput
 * by DojoDave <http://www.0j0.org>
 * http://www.arduino.cc/en/Tutorial/AnalogInput
 */
int potBrightness = A0; // select the input pin for the potentiometer (styre brightness)
int potBlink = A1; // select the input pin for the potentiometer (styre blink)
int ledBlue = 11;  // select the pin for the LED
int ledGreen = 10;
int ledRed = 9;
int value = 0; // variable to store the value coming from the sensor
 
 
void setup() {
  Serial.begin(9600);
}
void loop() {
  value = analogRead(potBrightness);    // read the value from the sensor, between 0 - 1024
  value = map (value, 0,1023,255,0); //converts value scale 1023 to 255
  Serial.println(value);
  analogWrite(ledBlue, value/4); // analogWrite can be between 0-255
  analogWrite(ledGreen, value/4); // analogWrite can be between 0-255 // turns on, do not need to declare HIGH
  analogWrite(ledRed, value/4); // analogWrite can be between 0-255 // turns on, do not need to declare HIGH
  delay (100);  
 }
 

 

Processing code
import processing.serial.*;
PImage img;
Serial port; 
float inputValue = 0; 
 
void setup()
{
  //size (500,500);
  size(640, 478);
  img = loadImage("light.JPG");
  img.loadPixels();
  // Only need to load the pixels[] array once, because we're only
  // manipulating pixels[] inside draw(), not drawing shapes.
  loadPixels();
  port = new Serial(this, Serial.list()[0], 9600); 
  port.bufferUntil ('\n'); //not sure about this one
}
 
void draw(){
 for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y++ ) {
      // Calculate the 1D location from a 2D grid
      int loc = x + y*img.width;
      // Get the R,G,B values from image
      float r,g,b;
      r = red (img.pixels[loc]);
      g = green (img.pixels[loc]);
      b = blue (img.pixels[loc]);
      // Calculate an amount to change brightness based on proximity to the mouse
      float maxdist = inputValue;//
      float d = dist(x, y, mouseX, mouseY);
      float adjustbrightness = 255*(maxdist-d)/maxdist;
      r += adjustbrightness;
      g += adjustbrightness;
      b += adjustbrightness;
      // Constrain RGB to make sure they are within 0-255 color range
      r = constrain(r, 0, 255);
      g = constrain(g, 0, 255);
      b = constrain(b, 0, 255);
      // Make a new color and set pixel in the window
      color c = color(r, g, b);
      //color c = color(r);
      pixels[y*width + x] = c;
    }
  }
  updatePixels();
}
 
void serialEvent (Serial port)
{
  inputValue = float(port.readStringUntil('\n')); //read value from FSR
  println(inputValue); //print value
}

 

 

1pic: No pressure
2. Medium pressure
3. Max pressure
photo.JPG
0
Your rating: None
Drupal theme by Kiwi Themes.