Description
Controls RGB values of LEDs and ball on screen with analog input from one
potentiometer. Controls the brightness of the LEDs and the size of the ball on screen with analog input from
another potentiometer.
Components Used
red LED
blue LED
green LED
(3) 220 resistors
(2) potentiometers
Arduino Code
/*
* Adapted from code by:
* Clay Shirky <clay.shirky@nyu.edu>
* Found at:
* http://www.arduino.cc/en/Tutorial/LEDCross-fadesWithpot1entiometer
*/
// INPUT: pot2entiometer should be connected to 5V and GND
int pot1Pin = 0; // select the input pin for pot1
int pot2Pin = 5; // select the input pin for photopot2
float pot1Val = 0; // variable to store the value coming from pot1
float pot2Val = 0; // variable to store the value coming from photopot2
// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED's cathodes should be connected to digital GND
int redPin = 9; // Red LED, connected to digital pin 9
int grnPin = 10; // Green LED, connected to digital pin 10
int bluPin = 11; // Blue LED, connected to digital pin 11
// Program variables
int redVal = 0; // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;
int serVal = 0; // Variable to store the value to send to the serial port
void setup()
{
Serial.begin(9600); // initializes the serial port
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}
// Main program
void loop()
{
pot1Val = analogRead(pot2Pin); // read the value of pot1 (for brightness of LEDs/size of ball), between 0 - 1024
pot1Val = (pot1Val/1024); // turn the value of pot1 into a decimal
serVal = analogRead(pot1Pin); // read the value of pot1 to send to the serial port
pot2Val = analogRead(pot2Pin); // read the value of pot2 (for color), between 0 - 1024
if (pot2Val < 341) // Lowest third of the pot2's range (0-340)
{
pot2Val = ((pot2Val * 3) / 4); // Normalize to 0-255
redVal = (256 - pot2Val) * pot1Val; // Red from full to off
grnVal = pot2Val * pot1Val; // Green from off to full
bluVal = 0; // Blue off
}
else if (pot2Val < 682) // Middle third of pot2's range (341-681)
{
pot2Val = ( (pot2Val-341) * 3) / 4; // Normalize to 0-255
redVal = 0; // Red off
grnVal = (256 - pot2Val) * pot1Val; // Green from full to off
bluVal = pot2Val * pot1Val; // Blue from off to full
}
else // Upper third of pot2's range (682-1023)
{
pot2Val = ( (pot2Val-683) * 3) / 4; // Normalize to 0-255
redVal = pot2Val * pot1Val; // Red from off to full
grnVal = 0; // Green off
bluVal = (256 - pot2Val) * pot1Val; // Blue from full to off
}
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
Serial.print(redVal);
Serial.print(",");
Serial.print(grnVal);
Serial.print(",");
Serial.print(bluVal);
Serial.print(",");
Serial.println(serVal/4);
delay(1000);
}
Processing Code
/*
* Arduino Ball Paint
* (Arduino Ball, modified)
* ----------------------
*
* Draw balls randomly on the screen, size controlled by a device
* on a serial port. Press space bar to clear screen, or any
* other key to generate fixed-size random balls.
*
* Receives an ASCII number over the serial port,
* terminated with a carriage return (ascii 13) then newline (10).
*
* This matches what Arduino's " Serial.println(val)" function
* puts out.
*
* Created 25 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
import processing.serial.*;
// Change the line below to match the portname for your Arduino board
String portname = "/dev/tty.usbserial-A40015ui";
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
size(300,300);
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, 90, 90, 90);
}
}
// draw balls
void drawball(int x, int y, int z, int rd, int gr, int bl) {
for (int i=0; i<100; i++ ) {
fill(rd, gr, bl);
ellipse(x,y,z,z);
}
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
String ser[] = split (buf, ",");
int r = int(ser[3]);
println("radius = "+r);
int x = int(random(0,width));
int y = int(random(0,height));
drawball(x, y, r, int(ser[0]), int(ser[1]), int(ser[2]));
buf = "";
//background(40,40,40); // erase screen
}
}
Mechanical Portion
An FSR beneath a wine bottle coaster controls the color of red and blue LEDs, slowly transitioning the color from completely blue to completely blue to completely red as the weight of the wine bottle becomes less.
Comments
Alana, sounds cute, but I
Alana, sounds cute, but I don't see any pictures! Incidentally, I am enjoying a nice glass of white zin as I type this. I hope me not seeing your photos isn't the wine's doing!