Introduction: Chameleon Light
The Chameleon light is an LED and a simple circuit that changes color based on the average color of your computer screen.
Materials needed:
1.) Arduino Uno
2.) 4 Jumper cables
3.) 5mm RGB LED from radioshack
4.) 3 resistors 1 68 ohm (or closest value) and two 56 ohms (or closest value)
5.) printer cable to connect compter USB to Arduino UNO
Software needed:
1.) Python
2.) Arduino UNO software (download here https://www.arduino.cc/en/Main/Software)
Step 1: STEP 2: Arduino Code
Copy the following code and upload it to your ARDUINO. It should look like the image above.
#include
#include #include #include
int bluePin = 10; int greenPin = 9; int redPin = 11; int blueBrightness = 0; int greenBrightness = 0; int redBrightness = 0;
void setup() { // Setting Up the COM Port Serial.begin(9600); // Changing PIN modes to OUTPUT pinMode(bluePin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(redPin, OUTPUT); }
void loop() { if(Serial.available() >= 4) { if(Serial.read() == 0xFF) { //0, 0, 0 is Black //255, 255, 255 is White redBrightness = Serial.read(); greenBrightness = Serial.read(); blueBrightness = Serial.read(); } } //redBrightness=0; //blueBrightness=255; //greenBrightness=0; /* Since the RGB LED has cathode pins for RGB we need to deduct value from 255 meaning if the brightness is 255 from the PC for a color we need to give 0 so that it will eluminate brightly */ analogWrite(bluePin, 255 - blueBrightness); analogWrite(greenPin, 255 - greenBrightness); analogWrite(redPin, 255 - redBrightness); delay(10); }
Step 2: Step 3: Copy the Python Code to a Text Editor
Copy the following code to a text editor of your choice. It will differ for mac and PCs. Just a note, there may be some issues in copy pasting inserting spaces that python cannot read, in that case, i would suggest hand-typing the code. It should look like the image above.
import sys
import pyscreenshot as S import serial
A = serial.Serial(sys.argv[1])
def do_screen_avg(): image = S.grab() pixels = image.load()
r = g = b = 0 totalpixels = 0; for i in range(0, image.size[0], 2): for j in range(0, image.size[1], 2): pr, pg, pb = pixels[i, j] r = r + pr g = g + pg b = b + pb totalpixels += 1
r = r/totalpixels g = g/totalpixels b = b/totalpixels
return (r, g, b)
def send_info(vals): r, g, b = vals print "Sending = ", r, g, b string = chr(0xFF) + chr(r) + chr(g) + chr(b) A.write(string)
def main(): while True: try: send_info(do_screen_avg()) except KeyboardInterrupt: sys.exit() except: print "error skipping"
if __name__ == "__main__": main() #import time #time.sleep(5) #send_info((255,0,0)) #time.sleep(5) #send_info((0,255,0)) #time.sleep(1) #send_info((0,0,255)) #time.sleep(1) #send_info((0xff, 0xa5, 0x00)) #return
Step 3: STEP 3: RUN IT!!!
Open the terminal and navigate to where your python file is saved (cd Desktop if you have saved it to the Desktop). Type the following command:
1.) python ABEFinal.py /dev/tty.usbmodem1411
NOTE: my python file was located on the desktop (so I typed cd Desktop first)
Another Note: my python file name is ABEFinal.py
Another note: YOUR USBMODEM number may be different than 1411. You must open the serial monitor in the top left of your ARDUINO sketch to find the modem number, and insert it into the command to run properly.
CHEERS!!!
Step 4: STEP 1: SET UP CIRCUIT
1.) Identify the red, voltage in, green and blue pins on your LED. The long lead pin is the positive voltage in ALWAYS. The pin to the left was the red pin on my LED, the two pins to the right of the long lead were green, blue respectively.
2.) calculate your resistances using http://led.linear1.org/1led.wiz and typing in the values from the back of your LED package. DO NOT THROW AWAY THE LED PACKAGING!
3.) Connect the circuit as shown above, with the largest resistor in front of the red node, and the two smaller infront of the green, blue nodes.
4.) Connect pins, 9,10,11 on the Arduino respectively to pins red, blue, green on the LED. (You can change the digital output pins you connect on the Arduino, but make sure you change the code to suit!)
5.) Connect the 5V voltage in pin on ARDUINO to the positive voltage on the LED (shown as the black jumper cable).