Description
In this lab we learned about photocells and force sensitive resistors and worked with Processing. With photocells, increasing brightness lowers the resistance. FSRs change resistance depending on the amount of pressure applied. The more pressure, the lower the resistance. In class we experimented with connecting both photocells and FSRs to the Arduino, using them to control blinking and brightness of LEDs.
For our homework we worked with the Processing programming language and Arduino. We were provided with some Arduino code that changes the brightness of an LED and records the values. Through serial communication, the computer receives this data and my Processing code makes changes on the screen depending on the serial data.
In honor of the baseball postseason I decided to cut a squishy baseball in half and connect it to the FSR. Pressing down with more force on the baseball causes different words to appear on the screen. Just a little bit of force results in the text "OUT!" Additional force can add the words "BASE HIT!" or "HOME RUN!"
Components Used
1- Arduino UNO
1- Breadboard
1- 220 ohm resistor
1- 10 ohm resistor
1- blue LED
1- FSR
1/2- squishy baseball
wires
Code
Processing Code
/*
* Receives values (0-255) from serial port. Based on this value, a message will appear on the
* screen. Applying less force to FSR will result in the words "OUT!" or "BASE HIT!" appearing. Greater force
* will show the words "HOME RUN!"
* Pressing the space bar will clear words from the screen.
*
* some modification from Arduino Ball Paint
* Created 25 October 2006, copyleft 2006 Tod E. Kurt, http://todbot.com/
*
* Sydney Mayes, 10/8/13
*/
import processing.serial.*;
String portname = Serial.list()[0];
Serial port;
String buf="";
int cr = 13;
int lf = 10;
PFont myFont;
void setup() {
size(700, 500);
background(40, 40, 40);
myFont=createFont("Georgia", 42);
textFont(myFont);
port = new Serial(this, portname, 9600);
println(Serial.list());
}
void draw() {
}
void keyPressed() {
if(key == ' ') {
background(40,40,40); // erase screen
}
else {
hit(0);}
}
// text to display
void hit(int r) {
if (r>20 && r<=85){
text("OUT!", 250, 50);}
if (r>120 && r<=170){
text("BASE HIT!", 250, 200);}
if (r>170 && r<=255)
{text("HOME RUN!", 250, 350);}
}
// 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);
hit(val);
buf="";
}
Provided 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...
}
- Login to post comments