Assignment: Sensing PART II: Force sensors and photocells
Collaborators:
For this project, we used photo cells to measure light and force sensors to measure pressure. For my project I wanted to create a pong game controlled by alternative controllers, such as force or light.
The code for the arduino sends information about how much pressure is on the pad to the computer. The computer program (in processing) interprets this information and uses it to control the paddle.
/*
* one pot fades one led
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int potPin = 0; // select the input pin for the potentiometer
int ledPin = 11; // select the 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(potPin); // read the value from the sensor, between 0 - 1024
Serial.println(val);
analogWrite(ledPin, val/4); // analogWrite can be between 0-255
}
The Processing code works as follows:
/*
* Arduino Pong, By Sean Carey
* I doubt anyone will care to copy it.
* This program takes input from the Serial to control a virtual paddle on the
* screen. It essentially works like pong. The code is modified from Arduino
* Ball Paint by Tod E. Kurt
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A70061U5"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int balla=2; //velocity of the ball in the x direciton
int ballb=2; //velocity of the ball in the y direction
int ballx=150; //position of the ball
int bally=100; //position of the bal
int h=0; // position of paddle
void setup() {
size(300,150);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
}
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++ ) {
ballx += balla;
if (ballx > 295 || ballx<20 && bally< h+40 && bally>h){
balla=-1*balla;
}
if (bally>145 || bally<5){
ballb=-1*ballb;
}
if (ballx < -10){
ballx=150;
bally=100;
}
bally += ballb;
fill(255,255,255);
ellipse(x,y,r,r);
//}
}
// Draw paddle
void drawpaddle (int h){
fill(204, 102, 0);
rect(5, h, 5, 40);
}
// 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);
background(40,40,40); // erase screen
drawball(ballx,bally,10);
drawpaddle(val);
h=val;
buf = "";
}
}
My hope was to have one pot control the position of the paddle while the force sensor controlled how hard the ball was hit
For the second part of the project, I was curious to see how much force was required to click the mouse button, and if it varied between trackpad button and external mouse. Using the same arduino code from the pong program, i found some values for:
Macbook Trackpad Button: 340
Mighty Mouse Button: 630
Apple Desktop Bus Mouse Button: 240
I am surprised that the Mighty Mouse required so much force to click! My Apple Desktop Bus Mouse may have been worn in and therefore required less force to click. Still, the Mighty mouse required more than 2x the force for the trackpad button, and almost 3x the force for the old ADB mouse.