Lab 4: FSR Pac-Man Display
This lab uses a force sensitive resistor embedded in a stuffed animal to register serial input. When the animal is squeezed, a red LED on the breadboard blinks in direct proportion to how hard the user squeezes to the toy: the harder the user squeezes, the longer the interval between blinks.
Serial output is then transferred to Processing, which uses to amount of pressure applied to draw a Pac-Man shape near the center of the display (modification of the "Ball Paint" code distributed in lab last week.) The harder the user squeezes the toy, the larger Pac-Man becomes.
Components Used:
1 - Arduino UNO
1 - Solderless breadboard
1 - Red LED
1 - 220-ohm resistor (used with LED)
1 - Force Sensitive Resistor
1 - 10k-ohm resistor (used with FSR)
1 - Adventure Time "Jake" (Cartoon Network) plush toy
1 - USB Power Supply
Code:
Arduino Code
int led = 10;
int sensorpin = A0;
int sensorval = 0;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
sensorval = analogRead(sensorpin);
Serial.println( sensorval);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(sensorval); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(sensorval); // wait for a second
sensorval = analogRead(sensorpin);
}
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 = "COM3"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
size(1000,700);
frameRate(25);
smooth();
background(0,0,0);
noStroke();
port = new Serial(this, portname, 9600);
}
void draw() {
}
void keyPressed() {
if(key == ' ') {
background(0,0,0); // erase screen
}
else {
int x = int(random(450,550));
int y = int(random(300,400));
drawball(x,y, 25);
}
}
// draw balls
void drawball(int x, int y, int r) {
for (int i=0; i<100; i++ ) {
fill(255-i,255-i,0);
arc(x,y,r,r,PI/4,7*PI/4);
}
}
// 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);
int x = int(random(450,550));
int y = int(random(300,400));
drawball(x,y,val);
buf = "";
background(0,0,0); // erase screen
}
}
- Login to post comments