Description:
The fourth lab assignment contains two parts:
a) FSR
In order to put the FSR to use, I attached it to a (kitchen) tong. It now can be used to measure the strength when gripping something with a tong. The FSR was attached to the tong using tape, and is protected by a layer of toilet paper that also serves as a diffusor. (Also see image attached.)
b) Photocells
As an interesting visualization using a photocell, I created a game called "Light Chaser". The user has to follow a white circle on the screen (on a black background) with a cup that is equipped with the photocell. If the user manages to follow the circle, it increases its speed and the score increases. If the user is too slow, the circle slows down and the background turns red as a warning for the user. As a visual effect, I added a trace of past circles. (See images attached.)
Components used:
- (1) Arduino Uno
- (1) Breadboard
- (1) Photo cell
- (1) 10k ohm resistor
- (several) cables
- (1) Paper cup
Code:
/*
* Light Chaser by Clemens Meyer
* The goal of this game is to follow the white circle on the screen with a pointer
* (equipped with a photocell to measure the brightness). The circle will move at increasing
* speed, if the player manages to follow the circle. If not, the game will warn the player
* by turning the background red.
*
* The bouncing of the circle was adapted from the Bounce example that is part of Processing.
*/
import processing.serial.*;
import cc.arduino.*;
// Arduino variables
Arduino arduino;
int inputPin = 1;
float brightness;
float brightnessThreshold = 700;
// Game variables
int score;
boolean scoreOnCircle = false; // If true, will print score on circle; if false, else in top right corner
// Background colors
int bg_r = 0;
int bg_g = 0;
int bg_b = 0;
int colorShift = 5;
// Circle position and movement
int circle_r = 150;
float circle_x;
float circle_y;
float xspeed;
float yspeed;
int xdirection = 1;
int ydirection = 1;
// Trace
float trace_x[];
float trace_y[];
int trace_number = 20;
void setup() {
size(1440, 800);
noStroke();
//frameRate(20);
smooth();
background(0);
// Set initial position, speed, and score
circle_x = width/2;
circle_y = height/2;
xspeed = 2;
yspeed = 2;
score = 0;
// Create arrays for trace of circle and fill with 0
trace_x = new float [trace_number];
for (int i=0; i<trace_x.length; i++) { trace_x[i] = 0;}
trace_y = new float [trace_number];
for (int i=0; i<trace_y.length; i++) { trace_y[i] = 0;}
// Create instance of arduino class
arduino = new Arduino(this, Arduino.list()[8], 57600);
}
void draw() {
brightness = arduino.analogRead(inputPin);
if(brightness < brightnessThreshold) { // If brightness is lower than threshold...
if(bg_r<50) { // ... change background color to red
bg_r += colorShift;
}
if(xspeed>0.3) { xspeed -= 0.3;} // ... decrease speed
if(yspeed>0.3) { yspeed -= 0.3;}
if(xspeed <= 0.3 || yspeed <= 0.3) {
xspeed = 0;
yspeed = 0;
}
} else { // If brightness is higher than threshold...
if(bg_r>colorShift) { // ... change background color to black
bg_r -= colorShift;
}
if(xspeed<40) { xspeed += 0.1;} // ... increase score and speed
if(yspeed<40) { yspeed += 0.1;}
score += 1;
}
// Debugging output
println("Brightness: "+brightness+"\t Speed: "+ xspeed +"\t Red: "+ bg_r +"\t Score: "+ score);
background(bg_r,bg_g,bg_b); // Clear screen for new frame and set background color
// Set new position of circle
circle_x = circle_x + (xspeed * xdirection);
circle_y = circle_y + (yspeed * ydirection);
// Change direction if circle reaches edge of screen
if (circle_x > width-circle_r || circle_x < circle_r) {
xdirection *= -1;
}
if (circle_y > height-circle_r || circle_y < circle_r) {
ydirection *= -1;
}
// Draw trace
// Move everything down in the trace arrays
for (int i=0; i<trace_x.length-1; i++) {
trace_x[i] = trace_x[i+1];
}
for (int i=0; i<trace_y.length-1; i++) {
trace_y[i] = trace_y[i+1];
}
// Add new value to trace
trace_x[trace_x.length-1] = circle_x;
trace_y[trace_y.length-1] = circle_y;
drawtrace (trace_x, trace_y, circle_r);
drawcircle(circle_x,circle_y,circle_r,255);
drawscore(circle_x,circle_y);
}
void drawcircle (float x, float y, float r, int alpha) {
fill(255,255,255,alpha);
ellipse(x,y,r*2,r*2);
}
void drawtrace (float x[], float y[], int r) {
for (int i=0; i<x.length-1;i++) {
float diameter = r*(i+1)/trace_number;
drawcircle(x[i],y[i],diameter,50);
}
}
void drawscore (float x, float y) { // Print score on circle or in corner
if(scoreOnCircle == true) {
fill(0,0,0);
textAlign(CENTER);
textSize(30);
text(score,x,y);
}
else {
fill(255,255,255);
textAlign(RIGHT);
textSize(30);
text(score,width-10,40);
}
}
- Login to post comments