Announcements

November 24, 2007
Reading for November 27th, are now posted. Enjoy!

October 2, 2007
To upload your thoughtless acts, create a new assignment page like any other lab. You'll see "Thoughtless Acts" listed as one of the assignment options.

May 24, 2008
This site has been archived and is no longer editable. Stay tuned for the next version, coming in the fall!


Revision of Control Brightness with PWM and Control Color with Hue Value from Wed, 09/19/2007 - 22:50

Project Members: 
Emelie Cheng

Description

I finished two exercises during the two-hour lab, using single pot controls brightness, and using one pot controls blinking and the other controls brightness. I also finish the three pots control the brightness of three LEDs at home, and the potentiometers worked very well. It really was an interesting experience.

For the home work, I recalled my experience of select colors in Photoshop by applying a slider, so I decided to transform this feature into my LED set. On one hand, instead of using the code provided by the course website, I created lines of code to control PWM in order to fake brightness. On the other hand, for the slider feature, I create a function based on the transformation formula of Hue value to RGB at http://en.wikipedia.org/wiki/HSV_color_space.

At the beginning, for some reason, the code didn’t work very well, it looked fine in the green LED, but not in the red and blue ones, say, the red one is always on, and the blues one is always off. I don’t have lots experience in debugging, so I asked help from my friend who is majoring in computer science. He found out that it is necessary to reset some variables as float, and finally, the code works!!!.

Component

  1. Arduino board
  2. White breadboard
  3. Three LED (the red one connects to pin11, the green one connects to pin9, and the blue one connects to pin10)
  4. Three 220-OHM resistors (labeled as red-red-brown-gold)
  5. Wires connect LED to Arduino board (black and 3 short blue ones connect to ground, and 3 yellow connect LEDs and the resistors to pin 9, 10, 11)
  6. 2 potentiometer with wires soldered on
  7. Wires connect potentiometer to Arduino board (black ones connect to ground, red ones connects to 5V, and yellow ones connect to pin 0 and 1 )
  8. 2 rubber bands
  9. USB cable
  10. Arduino environment

Code

/*
* Use one potentiometer to control BRIGHTNESS, and the other to control the Hue Value * Brightness-> Control PWM * Hue Value-> Contver to RGB */ // Debug flag int DEBUG = 0; int i = 0; // loop counter // Potentiameter pin settings; analog inputs; value ranges from 0 - 1023 int aIn = 0; // Control LED Blinkging PWM int bIn = 1; // Control Hue Value // Digital pin settings int rOut = 11; // goes to Red LED int gOut = 9; // goes to Green LED int bOut = 10; // goes to Blue LED // Values int aVal = 0; // Variables to store the input from the potentiometer int bVal = 0; int period = 10; // period of the PWM, millisecond float hue; // Hue Value int R, G, B; void HSB2RGB(float h, float s, float v){ //formula of converting Hue Value to RGB float f, p, q, t, r, g, b; int hi; hi = int(h/60); f = h/60-hi; p = v*(1-s); q = v*(1-f*s); t = v*(1-(1-f)*s); switch (hi){ case 0: r = v; g = t; b = p; break; case 1: r = q; g = v; b = p; break; case 2: r = p; g = v; b = t; break; case 3: r = p; g = q; b = v; break; case 4: r = t; g = p; b = v; break; case 5: r = v; g = p; b = q; break; default: r = 1.0; g = 1.0; b = 1.0; } // debugging output if (DEBUG) { if (i % 200 == 0) { // print the value of B every second Serial.print("r = "); Serial.print((int)r*1000); Serial.print(", g = "); Serial.print((int)g*1000); Serial.print(", b = "); Serial.println((int)b*1000); } } R=r*255; G=g*255; B=b*255; } // end HSB2RGB() void setup() { pinMode(rOut, OUTPUT); pinMode(gOut, OUTPUT); pinMode(bOut, OUTPUT); Serial.begin(9600); } void loop() { float lowtime, hightime; i++; aVal = analogRead(aIn); //read the value from pot 0, between 0 - 1024, for blinking bVal = analogRead(bIn); hue = (float)bVal*360/1024; hightime = (float)aVal*period/1024; lowtime = period - hightime; HSB2RGB(hue, 1.0, 1.0); analogWrite(rOut, R); analogWrite(gOut, G); analogWrite(bOut, B); /* digitalWrite(rOut, HIGH); // turn the LEDs on digitalWrite(gOut, HIGH); digitalWrite(bOut, HIGH); */ delay((int)hightime); // stop the program for some time digitalWrite(rOut, LOW); // turn the LEDs off digitalWrite(gOut, LOW); digitalWrite(bOut, LOW); delay((int)lowtime); // stop the program for some time // debugging output if (DEBUG) { if (i % 200 == 0) { // print the value of B every second Serial.print("aIn = "); Serial.print(aVal); Serial.print(", bIn = "); Serial.print(bVal); Serial.print(", hue = "); Serial.print((int)hue); Serial.print(", R = "); Serial.print(R); Serial.print(", G = "); Serial.print(G); Serial.print(", B = "); Serial.println(B); } } } // end loop()

Photograph


Powered by Drupal - Design by Artinet