Description:
Using the force center, I created a circuit in which m LED lights will respond to touch input. When no force is applied, the LEDs will remain off. When force is aplied, all three LEDs will flicker, acting like a signal. For the mechanical part of the assigment, I used a hackeysack, which gave tactile feedback when compressed, unlike the flat force sensor. I then hooked up my arduino to my processing code, creating an application which generates circles whose sizes are determined on the value of input sensed by the force sensor. The circles are distributed randomly on the page, to create a colorful assortment resembling bubbles. As the screen fills up, it can easily be cleared by hitting the space bar.
Components:
1 Arduino Uno
3 LEDs
3 220 ohm resistors
1 force sensor
various wires
1 hackeysack
Code:
Arduino:
/*
* "Coffee-cup" Color Mixer:
* Code for mixing and reporting PWM-mediated color
* Assumes Arduino 0004 or higher, as it uses Serial.begin()-style communication
*
* Control 3 LEDs with 3 potentiometers
* If the LEDs are different colors, and are directed at diffusing surface (stuck in a
* a Ping-Pong ball, or placed in a paper coffee cup with a cut-out bottom and
* a white plastic lid), the colors will mix together.
*
* When you mix a color you like, stop adjusting the pots.
* The mix values that create that color will be reported via serial out.
*
* Standard colors for light mixing are Red, Green, and Blue, though you can mix
* with any three colors; Red + Blue + White would let you mix shades of red,
* blue, and purple (though no yellow, orange, green, or blue-green.)
*
* Put 220 Ohm resistors in line with pots, to prevent circuit from
* grounding out when the pots are at zero
*/
// Analog pin settings
int sensorPin = A0;
int sensorVal = 0;
boolean aOn = false;
boolean bOn = false;
boolean cOn = false;
/*int aIn = 0; // Potentiometers connected to analog pins 0, 1, and 2
int bIn = 1; // (Connect power to 5V and ground to analog ground)
int cIn = 2; */
// Digital pin settings
int aOut = 9; // LEDs connected to digital pins 9, 10 and 11
int bOut = 10; // (Connect cathodes to digital ground)
int cOut = 11;
// Values
int aVal = 0; // values to write out to each LED
int bVal = 120;
int cVal = 200;
void setup()
{
pinMode(aOut, OUTPUT); // sets the digital pins as output
pinMode(bOut, OUTPUT);
pinMode(cOut, OUTPUT);
pinMode(sensorPin, INPUT);
Serial.begin(9600); // Open serial communication for reporting
}
void loop(){
sensorVal = analogRead(sensorPin);
while (sensorVal > 1010) {
sensorVal = analogRead(sensorPin);
Serial.println(sensorVal);
if (sensorVal < 1000) {
break;
}
if (aVal > 250) {
aOn = true;
aVal -= 10;
} else if (aVal < 20) {
aOn = false;
aVal += 10;
} else if (!aOn) {
aVal += 10;
} else if (aOn) {
aVal -= 10;
}
analogWrite(aOut, aVal);
if (bVal > 250) {
bOn = true;
bVal -= 10;
} else if (bVal < 20) {
bOn = false;
bVal += 10;
} else if (!bOn) {
bVal += 10;
} else if (bOn) {
bVal -= 10;
}
analogWrite(bOut, bVal);
if (cVal > 250) {
cOn = true;
cVal -= 10;
} else if (cVal < 20) {
cOn = false;
cVal += 10;
} else if (!cOn) {
cVal += 10;
} else if (aOn) {
cVal -= 10;
}
analogWrite(cOut, cVal);
}
aVal = 0;
bVal = 0;
cVal = 0;
analogWrite(aOut, aVal);
analogWrite(bOut, bVal);
analogWrite(cOut, cVal);
}
Processing:
import processing.serial.*;
int val;
Serial port;
String buf ="";
int cr=13;
int lf=10;
float bounds = 300;
void setup() {
size(600,600);
frameRate(60);
smooth();
colorMode(RGB);
background(40,40,40);
noStroke();
port = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
}
void keyPressed() {
if (key== ' ') {
background(40,40,40);
}
}
// draw balls
void drawball(int x, int y, int r) {
fill(int(random(0, 255)),255-int(random(0, 255)),240);
ellipse(x,y,r,r);
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
println(c);
drawball(int(random(0,600)), int(random(0,600)), c);
}
- Login to post comments