Lab4: Sensor 2: Force sensors and photocells

alex_gaysinsky's picture

Mechanical part - donation piggy bank that calculate money in the bank according to the weight (FSR)

and displays it as strength of a led

Arduino Side
/*
 * send data from FSR and Photo senses to a serial port.
 * lights a led as a result of darkness
 */
int fsrPin = 0;   // select the input pin for the potentiometer
int photoPin = 1;   // select the input pin for the potentiometer
 
int ledPin = 13;   // select the pin for the LED
int fsrVal = 0;      // variable to store the value coming from the sensor
int photoVal = 0;      // variable to store the value coming from the sensor
 
void setup() {
  Serial.begin(9600);
}
void loop() {
  fsrVal = analogRead(fsrPin);            // read the value from the sensor, between 0 - 1024
  photoVal = analogRead(photoPin);        // read the value from the sensor, between 0 - 1024
  if (fsrVal>=4) Serial.println(String("")+fsrVal + "," + photoVal);
  analogWrite(ledPin, (1024-photoVal)/4); // analogWrite can be between 0-255
}
 
Processing Side - circles painted according to the pressure (circle size) and light (circle color)
/*
 * 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.usbmodemfd131"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
void setup() {
  size(300,300);
  frameRate(10);
  smooth();
  background(40,40,40);
  noStroke();
  port = new Serial(this, portname, 9600); 
}
void draw() {
}
void keyPressed() {
  background(40,40,40);  // erase screen
}
 
// draw balls
void drawball(int x, int y, int c,int r) {
  for (int i=0; i<100; i++ ) {
    fill(255-c,c,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 comma = buf.indexOf(",");
    
    int fsrVal = int(buf.substring(0, comma));
    int photoVal = int(buf.substring(comma+1));
    
    if (fsrVal > 0) {
      int x = int(random(0,width));
      int y = int(random(0,height));
      drawball(x,y,photoVal/4,fsrVal);
    }
    
    buf = "";
  }
}
 
photo+fsr
part1: photo sense
IMG_20110927_231223.jpg
fsr
0
Your rating: None