Lab 4

Submitted by zwasson on Wed, 02/27/2013 - 10:11

 

Description

The purpose of this lab was to explore other sensor inputs, such as force sensors and light sensors, as well as to experiment with Processing. For this lab, I looked at the obvious extension of the sensors to the digital world; the idea being that I wanted to have a very natural way of interacting with the image in Processing. Starting with a simple square on a dark background, you can manipulate it via the three sensors (force sensor, light sensor, and potentiometer). The potentiometer is used to rotate the square about the origin, and the force sensor scales it (putting pressure "flattens" it and makes it larger). The light sensor controls the color of the square; when it is fully covered, the square will completely fade into the background.

Components

  • Arduino Uno
  • Potentiometer
  • Force Sensor
  • Light Sensor
  • Sponge

Code

/*
  Lab4
  This lab manipulates a square using sensors from an Arduino
  
  Zach Wasson
 */
 
import processing.serial.*;
String portname = "COM3";
Serial port;
String buffer = "";
 
// shape parameters
int x = -50;
int y = -50;
int x_length = 100;
int y_length = 100;
 
void setup() {
  size(300,300);
  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
  }
}
 
 
void serialEvent(Serial p) {
  int c = port.read();
  if (c != 10 && c != 13) {
    buffer += char(c);
  }
  if (c == 10) {
    // structure is (light,force,potentiometer)
    int[] sensor_data = int(split(buffer, ','));
 
    println("light = " + sensor_data[0]
          + "\nforce = " + sensor_data[1]
          + "\npot = " + sensor_data[2]);
    
    float scale_value = sensor_data[1]/512.0;
    int color_value = sensor_data[0];
    float rotate_value = sensor_data[2]/1024.0*TWO_PI;
          
    background(40,40,40);     
    translate(height/2, width/2); 
    rotate(rotate_value);
    if(scale_value < 0.1)
      scale_value = 0.1;
    scale(scale_value); 
    if(color_value > 255)
      color_value = 255; 
    if(color_value < 40)
      color_value = 40;  
    fill(color_value);
    rect(x,y,x_length,y_length);
    buffer = "";
  }
}
 
/*
  Lab4
  This lab uses potentiometers, light sensors, and FSRs
  This sketch is meant to be used with Processing
  
  Zach Wasson
 */
 
// pin definitions
int red_led = 11;
int green_led = 10;
int blue_led = 9;
int light_sensor = A0;
int force_sensor = A1;
int potentiometer = A2;
 
// RGB values
int red_val = 0;
int green_val = 0;
int blue_val = 0;
 
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pins as outputs.
  pinMode(red_led, OUTPUT);  
  pinMode(green_led, OUTPUT); 
  pinMode(blue_led, OUTPUT); 
 
  Serial.begin(9600);
}
 
// the loop routine runs over and over again forever:
void loop() {
  int light = analogRead(light_sensor);
  int force = analogRead(force_sensor);
  int pot = analogRead(potentiometer);
 
  Serial.print(light);
  Serial.print(",");
  Serial.print(force);
  Serial.print(",");
  Serial.print(pot);
  Serial.println();
  delay(100);
}
 

 

circuit.jpg
demo.png
0
Your rating: None
Drupal theme by Kiwi Themes.