Description
In this assignment, I was inspired by the example made by Jeremy Blum in this video. I have constructed a soft parfume-ish pump. The screen is an empty PacMan board, but when the user presses the pump, a PacMan shape appears, and the size of the PacMan increases, as the pump is pressed harder. Also, the harder the pump is pressed, the PacMan turns into the true yellow color.
Components
-
Arduino
-
Breadboard
-
USB cable
-
Cables
-
Plastic, tape and cotton ball
-
Resistor (10 ohm)
-
Force sensing resistor (FSR)
Challenges
Lots!
Main problem was simply to have Processing receive values that were coming from the Arduino. It took me several attempts. I discarded a number of sketches and finally decided to start all over from scratch.
My solution was simply to follow the exact steps of Jeremy Blum's video to make sure, the serial communication was working. Afterwards, I replaced parts of the code, to customize it to my purpose. In the Arduino i changes the max value range from 1023 to 940 to make it fit with the FSR sensor value range. I found the map() function very useful!
In the Processing code, I uploaded an image as background instead of the blue background. I drew a PacMan-like shape using the arc() function and then let the incoming FSR sensor value determine both the size and the red/green color of the shape.
What I still struggle a little with, is clearing the previous PacMan, so that it will disappear the user lets go og the pump. Without the clearing, though, I played with the fill() as a color mixer (the colorful image attached), which is also cool.
Code
Arduino
/*
This is a simple program to send communication from the Arduino board to the computer:
the value of analog input (A0) is sent out the serial port (serial communication).
Serial communication means thar the connection appears to both the Arduino and the
computer as a serial port. Bytes are sent one after another (serially) from the Arduino
to the computer.
*/
int fsrPin = A0; // defining FSR sensor pin
void setup() {
Serial.begin(9600); // initialize the serial communication
}
void loop() {
int fsrVal = map(analogRead(fsrPin), 0, 940, 0, 255); // using the map-function to convert the FSR sensor value range (0-940) coming in via the analogueRead to the RGB color value range (0-255).
// send the value of analog input 0:
Serial.println(fsrVal); // print the value in the serial monitor
delay(50); // wait a bit for the analog-to-digital converter
}
Processing
import processing.serial.*; //import serial library
Serial port; // creates serial object
float fsrVal = 0; // creates variable to store incoming sensor value
void setup() {
size(618, 618); // sets size of monitor window (same as background image)
PImage img; // creates variable to store background image
img = loadImage("pacmanboard.jpeg"); //loads background image from folder
background(img); // sets img to background
port = new Serial(this, "/dev/tty.usbmodem1411", 9600); // initialize serial port
port.bufferUntil('\n'); // waits until it recieves value to and prints a new line
}
void draw() { // loop that continously updates the screen window
arc(309, 309, fsrVal, fsrVal, 0, PI+HALF_PI+1, PIE); // draws elipse-Pac Man-like shape. Places shape in the middle of the window. The incoming FSR value determines the width and height of the shape.
fill(fsrVal, fsrVal, 0); // determines color fill of the shape. Red and Green values are determined by the FSR value. Blue is 0.
}
void serialEvent (Serial port) { // look for serial event
fsrVal = float(port.readStringUntil('\n')); // put the incoming serialEvent value in the fsrVal variable.
}