Lab 4: Sensing: Force sensors and photocells 💡
2 min readSep 26, 2023
Abigail Chen. Professor Kimiko Ryokai. INFO 262, Fall 2023.
Description
Our task for this lab was to create an interesting visualization on your computer that is influenced by the input from the sensors you have (pot, photocell, FSR, or a combination of them).
For this, I began by exploring Processing’s example code files. I stumbled upon the RadialGradient one, which I particularly enjoyed. I decided to combine this with the FSR, which would be an interesting visualization since you get to see the distinct color change for how much force is being applied to the sensor.
Components Used
- Arduino
- Breadboard
- Red, black, and yellow wires
- 1000 ohm-resistors
- FSR
Code
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
// Change this pin number to match where you actually have a pot in your
// circuit. Note that pin 0 is the same as pin A0, pin 1 is same as A1, etc.
int fsrPin = 0;
int dim;
void setup() {
size(500, 500);
// Prints out the available serial ports.
println(Arduino.list());
// Modify this line, by changing the "0" to the index of the serial
// port corresponding to your Arduino board (as it appears in the list
// printed by the line above).
arduino = new Arduino(this, Arduino.list()[2], 57600);
dim = width/2;
background(0);
colorMode(HSB, 360, 100, 100);
noStroke();
ellipseMode(RADIUS);
}
void draw() {
background(0);
for (int x = 0; x <= width; x+=dim) {
drawGradient(x, height/2);
}
}
void drawGradient(float x, float y){
// Reads the analog value of the FSR pin from Arduino
int fsrVal = arduino.analogRead(fsrPin);
// Maps value of FSR to a range of 0-255
int brightness = int(map(fsrVal, 0, 1023, 0, 255));
int radius = dim/2;
// Draws ellipse with the corresponding brightness
for (int r = radius; r > 0; --r) {
fill(brightness, 90, 90);
ellipse(x, y, r, r);
brightness = (brightness + 1) % 360;
}
}