Making Things Interactive

January 30, 2008

Tutorials: RGB Mood Light

Filed under: 3: LadyAda Tutorials,Jesse Chorng — Jesse @ 3:13 am

The pulsing LEDs started giving me a headache while I was learning the tutorials. I then started thinking about how I could write something smoother and thus be gentler on my nogin. After looking around and finding something on MAKE (always do), I was able to see how they created gradual increases/decreases in light.  Moreover, I spent much more time than I had expected to finding stuff and fixing code, which I guess was the point of the tutorials and getting to know the Arduino.  Here’s a video of it in action:

 

int count = 0;                    // Sets up a counter for loops
int intredPin = 11;                   // Red LED connected to digital pin 11
int greenPin = 10;                    // Green LED connected to digital pin 10
int bluePin = 9;                      // Blue LED connected to digital pin 9

void setup()
{
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   pinMode(bluePin, OUTPUT);
}

void loop()
{    
   for (count = 0 ; count <= 255 ; count -= 5)    // Count up by 5 to gradually increase steps
   {
   analogWrite(redPin, count);                    // Gradually increases
   digitalWrite(greenPin, HIGH);                  // On
   digitalWrite(bluePin, LOW);                    // Off
   delay(50);
   }    

   for (count = 255 ; count >= 0 ; count -= 5)    // Continues pattern over and over
   {
   digitalWrite(redPin, HIGH );                   // Remains on
   analogWrite(greenPin, count);                  // Gradually decreases
   digitalWrite(bluePin, LOW);                    // Remains off
   delay(50);
   }
 
   for (count = 0 ; count <= 255 ; count -= 5)    
   {
   digitalWrite(redPin, HIGH);                    
   digitalWrite(greenPin, LOW);
   analogWrite(bluePin, count);
   delay(50);
   }    

   for (count = 255 ; count >= 0 ; count -= 5)
   {
   analogWrite(redPin, count);
   digitalWrite(greenPin, LOW);
   digitalWrite(bluePin, HIGH);
   delay(50);
   }
   
   for (count = 0 ; count <= 255 ; count -= 5)
   {
   digitalWrite(redPin, LOW);
   analogWrite(greenPin, count);
   digitalWrite(bluePin, HIGH);
   delay(50);
   }    

   for (count = 255 ; count >= 0 ; count -= 5)
   {
   digitalWrite(redPin, LOW);
   digitalWrite(greenPin, HIGH);
   analogWrite(bluePin, count);
   delay(50);
   }
}
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: