Force readings from the FSR serve two functions:
1. Harder presses result in larger circles rendered on screen
2. Holding a press in the LOW, MID, or HIGH range will change the color as specified:
Low values (0-80) will set a color weighted heavily with Red.
Medium values (81-160) will set a color weighted towards green.
high values (160+) will set a color weigthed blue.
The current color is always rendered as the most recently drawn ellipse.
The breakdown of the current color is represented in the outer triangles as its CMY parts
Thats pretty much it. I attached a photo of the visualization and of the circuit.
import processing.serial.*;
// Change this to the portname your Arduino board
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int count = 0;
int red = 0;
int grn = 0;
int blu = 0;
int v = 0;
int holdval = 0;
void setup() {
size(600,600);
frameRate(10);
smooth();
background(255, 255, 255);
noStroke();
colorMode(RGB);
/*
Run the code once with this print statement to see a list of available serial ports.
Make a note of the number next to the port you'd like to use
*/
println(Serial.list());
/* Once you know the number of the port you want to use Replace the "0" in the code below
with the port number. For instance, if you wanted to use port number 4 in the list, then
the code should say: port = new Serial(this, Serial.list()[4], 9600);
*/
port = new Serial(this, Serial.list()[1], 9600);
}
void draw() {
}
/*void keyPressed() {
if(key == ' ') {
background(255,255,255); // 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, int c1, int c2, int c3) {
for (int i=0; i<100; i++ ) {
//fill(255-i,i,240);
fill(red, grn, blu);
ellipse(x,y,r,r);
}
}
// draw squares
void drawtriangles() {
fill(red, 255, 255);
triangle(0, 0, 300, 0, 150, 300);
fill(255, grn, 255);
triangle(300, 0, 600, 0, 450, 300);
fill(255, 255, blu);
triangle(150, 300, 450, 300, 300, 600);
//fill(red, grn, blu);
//triangle(300, 0, 150, 300, 450, 300);
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
//println(c);
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
int val = int(buf);
println("val="+val);
int x = int(random(0,width));
int y = int(random(0,height));
//drawball(x,y,val);
drawball(300,200,(val+1)/2, red, grn, blu);
buf = "";
//background(40,40,40); // erase screen
v = int(val);
}
// If serial value hasn't changed after x iterations
// set color based on current value
if (count % 5 == 0) {
if (holdval == v) {
// change color
if (v <= 80) {
red = v * 3;
grn = (grn + 1) / 2;
blu = (blu + 1) / 2;
} else if (v > 80 && v <= 160) {
grn = (v - 80) * 3;
red = (red + 1) / 2;
blu = (blu + 1) / 2;
} else if (v > 160) {
blu = (v - 160) * 3;
grn = (grn + 1) / 2;
red = (red + 1) / 2;
}
drawtriangles();
} else {
holdval = int(v);
}
}
count++;
}
- Login to post comments