Description
I wanted to make a diffuser out of cloth, for a soft appearance, so I made a diffuser by stitching a cotton pad into a sack shape.
Components Used
1- Arduino Pro Mini 3.3V
1- Breadboard
3- 220 Ω Resistor
1- Red LED
1- Green LED
1- Blue LED
1- Cotton pad
String
Code
/*
* Keypress Colors
* ---------------
* Keypresses control the brightness of R,G,B LEDs
*
* Pressing the 'r', 'g', and 'b' keys will cycle the brighness of the corresponding LEDs upwards by 10%.
*
* Meena Vempaty
*/
char* colorCode;
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int red = 0;
int green = 0;
int blue = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
Serial.println("enter color command 'r', 'g', or 'b' :");
}
void loop () {
// clear the string
memset(colorCode, 0, 1);
//read the serial port and create a string out of what you read
readSerialString(colorCode);
char color = *colorCode;
if( color == 'r' || color == 'g' || color == 'b' ) {
Serial.print("setting color ");
Serial.print(color);
Serial.print(" to ");
if(color == 'r') {
analogWrite(redPin, increment(&red));
Serial.print(red);
}
else if(color == 'g') {
analogWrite(greenPin, increment(&green));
Serial.print(green);
}
else if(color == 'b') {
analogWrite(bluePin, increment(&blue));
Serial.print(blue);
}
Serial.println("%");
colorCode = 0; // indicates we've used this string
}
delay(100); // wait a bit, for serial data
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
if(!Serial.available()) {
return;
}
*strArray = Serial.read();
}
int increment(int* color) {
*color += 10;
if(*color > 100) {
*color = 0;
}
int value = (((double) *color)/100)*255;
return value;
}
Images
LED
Bonus!
I added a very rudimentary capacitive touch sensor, a paperclip wrapped around the LED. The LED now cycles colors when it (well, the paperclip) is touched. I used code from http://playground.arduino.cc/Code/CapacitiveSensor to get touch input.
image: http://i41.tinypic.com/2yuegrb.jpg
code:
/*
* Touch Colors
* ---------------
* A capacitive touch sensor controls when R,G,B LEDs cycle.
*
* Meena Vempaty
*/
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int touchPin = 2;
int color = 0;
uint8_t readCapacitivePin(int pinToMeasure) {
// Variables used to translate from Arduino to AVR pin naming
volatile uint8_t* port;
volatile uint8_t* ddr;
volatile uint8_t* pin;
// Here we translate the input pin number from
// Arduino pin number to the AVR PORT, PIN, DDR,
// and which bit of those registers we care about.
byte bitmask;
port = portOutputRegister(digitalPinToPort(pinToMeasure));
ddr = portModeRegister(digitalPinToPort(pinToMeasure));
bitmask = digitalPinToBitMask(pinToMeasure);
pin = portInputRegister(digitalPinToPort(pinToMeasure));
// Discharge the pin first by setting it low and output
*port &= ~(bitmask);
*ddr |= bitmask;
delay(1);
// Prevent the timer IRQ from disturbing our measurement
noInterrupts();
// Make the pin an input with the internal pull-up on
*ddr &= ~(bitmask);
*port |= bitmask;
// Now see how long the pin to get pulled up. This manual unrolling of the loop
// decreases the number of hardware cycles between each read of the pin,
// thus increasing sensitivity.
uint8_t cycles = 17;
if (*pin & bitmask) { cycles = 0;}
else if (*pin & bitmask) { cycles = 1;}
else if (*pin & bitmask) { cycles = 2;}
else if (*pin & bitmask) { cycles = 3;}
else if (*pin & bitmask) { cycles = 4;}
else if (*pin & bitmask) { cycles = 5;}
else if (*pin & bitmask) { cycles = 6;}
else if (*pin & bitmask) { cycles = 7;}
else if (*pin & bitmask) { cycles = 8;}
else if (*pin & bitmask) { cycles = 9;}
else if (*pin & bitmask) { cycles = 10;}
else if (*pin & bitmask) { cycles = 11;}
else if (*pin & bitmask) { cycles = 12;}
else if (*pin & bitmask) { cycles = 13;}
else if (*pin & bitmask) { cycles = 14;}
else if (*pin & bitmask) { cycles = 15;}
else if (*pin & bitmask) { cycles = 16;}
// End of timing-critical section
interrupts();
// Discharge the pin again by setting it low and output
// It's important to leave the pins low if you want to
// be able to touch more than 1 sensor at a time - if
// the sensor is left pulled high, when you touch
// two sensors, your body will transfer the charge between
// sensors.
*port &= ~(bitmask);
*ddr |= bitmask;
return cycles;
}
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void loop () {
int increment = readCapacitivePin(touchPin) - 1;
color = (color + increment)%766;
Serial.println(color);
setColor(color);
delay(25);
}
void setColor(int color) {
int red;
int green;
int blue;
if(color < 256) {
red = 255 - color;
green = color;
blue = 0;
} else if(color < 511) {
color = color%256;
green = 255 - color;
blue = color;
red = 0;
} else {
color = color%256;
blue = 255 - color;
red = color;
green = 0;
}
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
- Login to post comments