FSR Lab - DJK

Submitted by derek on Wed, 02/27/2013 - 00:00

Materials:

(2) 10k pots

(1) FSR

(1) Arduino Uno

I built a simple drawing program which resembles the popular toy "Etch-a-sketch".  The program (code below) receives three sensor inputs.  The first from the FSR and the other two from the pots.  Processing reads in the values which are grouped and seperated by commas and ended with a newline character. The pots control the position, x and y, of a circle drawn on the screen.  The FSR allows the drawer to reset the screen.  

Arduino Code:

 

/*
 * Resistive Sensor Input
 * Takes the input from a resistive sensor, e.g., FSR or photocell
 * Dims the LED accordingly, and sends the value (0-255) to the serial port
 */
int FSRpin = 0;  // select the input pin for the sensor
int pot1pin = 1; 
int pot2pin = 2;
int ledPin = 11;    // select the output pin for the LED
int FSRval = 0;        // variable to store the value coming from the sensor
int pot1val = 0;
int pot2val = 0;
 
void setup() {
  Serial.begin(9600);
}
void loop() {
  FSRval = analogRead(FSRpin); // read the value from the sensor, 0-1023
  pot1val = analogRead(pot1pin);
  pot2val = analogRead(pot2pin);
  analogWrite(ledPin, FSRpin/4);  // analogWrite (dimming the LED) can be between 0-255
  Serial.print(FSRval/4, DEC);       // writing the value to the PC via serial connection 
  Serial.print(',');
  Serial.print(pot1val/2, DEC);
  Serial.print(',');
  Serial.println(pot2val/2, DEC);
  delay(50);                   // rest a little...
}
 
Processing Code:
 
/*
 * 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.usbmodem1421"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
int val = 0;
float bgcolor = 0;               // Background color
float fgcolor = 255;               // Fill color
int reset = 0;
float xpos, ypos;            // Starting position of the ball
 
 
void setup() {
  size(510,510);
  frameRate(60);
  smooth();
  background(bgcolor);
  noStroke();
  port = new Serial(this, portname, 9600); 
  port.bufferUntil('\n');
}
void draw() {
    //int x = int(random(0,width));
    //int y = int(random(0,height));
    //drawball(x,y,val);
    if(reset > 150) {
      background(bgcolor);
    }
  fill(fgcolor);
  // Draw the shape
  ellipse(xpos, ypos, 20, 20);
}
 
/*
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) {
  for (int i=0; i<100; i++ ) {
    fill(255-i,i,240);
    ellipse(x,y,r,r);
  }
 
}
// called whenever serial data arrives
void serialEvent(Serial p) {
 
   // read the serial buffer:
  String myString = port.readStringUntil('\n');
  if (myString != null) {
    //println(myString);
  }
  
  myString = trim(myString);
 
    // split the string at the commas
    // and convert the sections into integers:
    int sensors[] = int(split(myString, ','));
    // print out the values you got:
    for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
      print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t"); 
    }
    // add a linefeed after all the sensor values are printed:
    println();
    
    // make sure there are three values before you use them:
 if (sensors.length > 1) {
      xpos = sensors[2];
      ypos = sensors[1];
      // the switch values are 0 and 1.  This makes them 0 and 255:
      reset = sensors[0];
    }
    
  /*
  if (c != lf && c != cr) {
    buf += char(c);
  }
  if (c == lf) {
    val = int(buf);
    println("val="+val); 
    buf = "";
    background(40,40,40);  // erase screen
    
  }*/
}
circuit setup
Processing output
0
Your rating: None
Drupal theme by Kiwi Themes.