Saturday, May 16, 2015

RGB LED

How to build and control an RGB LED on an Arduino:
by J.B. Wylzan




Project 21:  RGB LED 

This project  shows how to control a RGB Led colors. 
These colors will be used as ROM indicators as we build our Homotronics robots.

Hardware:
RGB led 
2 resistors
connecting wires
breadboard
Arduino R3 UNO board

Block Diagram:




Code # 21:

/*
  iHackLab RGB Colors
  powered by Arduino
  sketched by J.B. Wylzan
  modified by Lawsinium

  The RGB LED will display the colors red, green and blue continuously.
  This example code is public domain.
 */

int redPin = 8;
int greenPin = 7;
int bluePin = 4;
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop()
{
  digitalWrite(redPin, HIGH);                                   // red led
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);
  delay(1000);

  digitalWrite(redPin, LOW);                                   // green led
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, LOW);
  delay(1000);

  digitalWrite(redPin, LOW);                                  // blue led
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, HIGH);
  delay(1000);
}

Challenge: 
Sketch a program that will produce other colors aside from red, green and blue by altering or combining HIGH or LOW inside digitalWrite(). Be surprised what colors will be created. Use the programming procedure technique below to create various colors by replacing r, g, and b with any numbers from 0 to 255. Happy painting ;-).

void displayRGB(int r, int g, int b)
{
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);
}



Actual Layout:



Procedure:
1. Build the prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code # 21 above
5. Paste Code #21
6. Click File > Save
7. Click Verify
8. Click Upload
9. The RGB led will change colors from Red, Green and Blue.


Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email. 

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.

================================================================== 
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®

L.A.W.S.I.N. Educational Production
 ================================================================== 

No comments:

Post a Comment