Assignment: Sensing PART II: Force Sensitive Resistors and Photocells
Collaborators:
One technique in neuroscience for studying the brain is electrophysiology, in which scientists insert electrodes into the brain to read out brain activity. From the activity, scientists can determine the function of that portion of the brain. The problem with this technique is that it leaves microscopic-sized holes in the brain, illustrated in this homework assignment. Note that the sizes are grossly exaggerated!
Poke the "electrode" - a tube with the force sensing resistor (FSR) taped to the bottom - on your friend's head and create virtual lesions on the image of the brain located on the computer. The position of the lesion does not correspond to where you have poked unfortunately. That would be a future direction. The size of the virtual lesion is bigger with greater exerted force.
Below is my code:
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 sensorPin = 0; // select the input pin for the sensor
int ledPin = 11; // select the output pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(sensorPin); // read the value from the sensor, 0-1023
analogWrite(ledPin, val/4); // analogWrite (dimming the LED) can be between 0-255
Serial.println(val/4); // writing the value to the PC via serial connection
delay(50); // rest a little...
}
Processing code
/* virtual brain lesion
*
* Modified version of...
* Arduino Ball Paint
* (Arduino Ball, modified)
* ----------------------
*
* 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:
// Have USB cable connected. Open Arduino app.
// Go to tools -> Serial Port -> Find /dev/tty.usbserial-<blah> option
String portname = "/dev/tty.usbserial-A9007Llr";
// Orginially set as "COM12", // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
PImage img; // background image
void setup() {
// size(300,300);
frameRate(10);
smooth();
img = loadImage("human_brain_lateral.jpg");
size(img.width, img.height);
noStroke();
port = new Serial(this, portname, 9600);
//noLoop(); // Makes draw() only run once
}
void draw() {
}
/*
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(0,0,0); //fill(255-i,i,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 val = int(buf);
println("val="+val);
if(val > 0)
{
int x = int(random(0,width));
int y = int(random(0,height));
drawball(x,y,val/10);
buf = "";
}
else // No force detected
{
image(img, 0,0, img.width, img.height); // re-set screen to image
// image's top left corner at top left corner of screen
// re-scale image to dimensions listed as the last two parameters
}
}
}