L4 - Force sensors and photocells

Posted by rowyn

rowyn's picture

Description

1. Programming: Program Arduino to read variable resistance values (from pot, photocell, or FSR) and output them to serial.  Then use Processing to draw a spinning color wheel whose rate of rotation is based on the serial values.

2. Mechanical: I attached my FSR to a magnet, so that when another magnet or magnetic metal object is touching it, the FSR measures the amount of pressure between the two objects, and thus indirectly the force of magnetic attraction between them.

Components Used

  • Light Emitting Diodes (LEDs)
  • Resistors (220 ohms and 10k ohms)
  • Force-sensitive resistor
  • Various magnets and metallic objects

Processing Code

/**
 * Rotating Color Wheel
 * by Rowyn McDonald.
 * 
 * Adapted from Subtractive Color Wheel, by Ira Greenberg and Arduino Ball Paint, by Tod E. Kurt.
 * 
 * A subtractive color wheel can be made to rotate at various speeds based on serial input, 
 * such as from a variable resistor attached to a microcontroller.
 */
 
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "COM3";
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
 
int segs = 12;
int steps = 1;
float rotAdjust = TWO_PI / segs / 2;
float radius;
float segWidth;
float interval = TWO_PI / segs;
 
void setup() {
  size(1000, 750);
  background(127);
  frameRate(30);
  smooth();
  ellipseMode(RADIUS);
  noStroke();
  port = new Serial(this, portname, 9600); 
  drawShadeWheel();
}
 
 
void drawShadeWheel() {
  // make the diameter 90% of the sketch area
  radius = min(width, height) * 0.45;
  segWidth = radius / steps;
  for (int j = 0; j < steps; j++) {
    color[] cols = { 
      color(255-(255/steps)*j, 255-(255/steps)*j, 0), 
      color(255-(255/steps)*j, (255/1.5)-((255/1.5)/steps)*j, 0), 
      color(255-(255/steps)*j, (255/2)-((255/2)/steps)*j, 0), 
      color(255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j, 0), 
      color(255-(255/steps)*j, 0, 0), 
      color(255-(255/steps)*j, 0, (255/2)-((255/2)/steps)*j), 
      color(255-(255/steps)*j, 0, 255-(255/steps)*j), 
      color((255/2)-((255/2)/steps)*j, 0, 255-(255/steps)*j), 
      color(0, 0, 255-(255/steps)*j),
      color(0, 255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j), 
      color(0, 255-(255/steps)*j, 0), 
      color((255/2)-((255/2)/steps)*j, 255-(255/steps)*j, 0) 
    };
    for (int i = 0; i < segs; i++) {
      fill(cols[i]);
      arc(width/2, height/2, radius, radius, 
          interval*i+rotAdjust, interval*(i+1)+rotAdjust);
    }
    radius -= segWidth;
  }
}
 
void draw(){
  drawShadeWheel();
}
 
void serialEvent(Serial p) {
  int c = port.read();
  if (c != lf && c != cr) {
    buf += char(c);
  }
  if (c == lf) {
    int val = int(buf);
    println(val);
    rotAdjust = (rotAdjust + val*PI/pow(2, 12)) % TWO_PI;
    buf = "";
  }
}

You can't really tell, but the brightness of the LED in the background increases with each progressively stronger magnet.

FSR Attached to Magnet
Color Wheel Screenshot
Circuit
0
Your rating: None