Difference between revisions of "ECE 110/Equipment/Built in RGB LED"

From PrattWiki
Jump to navigation Jump to search
(Created page with "== Introduction == The CX-Bot has a built-in RGB LED on board. The red, green, and blue leads are connected to analog pins 45, 44, and 46 respectively. There are already pul...")
 
 
(3 intermediate revisions by one other user not shown)
Line 4: Line 4:
 
== Leads ==
 
== Leads ==
 
* RED: 45
 
* RED: 45
* GREEN: 44
+
* GREEN: 46
* BLUE: 46
+
* BLUE: 44
  
 
== Operation ==
 
== Operation ==
You need to define the pins, set the pin mode, and write analog values to the appropriate pins.  As mentioned above, the '''lower''' the value assigned to a pin, the '''brighter''' that channel will be.  Turning the light off thus requires that all three channels are set to 0; setting all channels to 255 results in a bright white light.
+
You need to define the pins, set the pin mode, and write analog values to the appropriate pins.  As mentioned above, the '''lower''' the value assigned to a pin, the '''brighter''' that channel will be.  Turning the light off thus requires that all three channels are set to 255; setting all channels to 0 results in a bright white light.
  
 
== Sample Code ==
 
== Sample Code ==
Line 15: Line 15:
  
 
<syntaxhighlight lang=C++>
 
<syntaxhighlight lang=C++>
 +
// Define pins for built-in RGB LED
 
#define redpin 45
 
#define redpin 45
 
#define greenpin 46
 
#define greenpin 46
Line 20: Line 21:
  
 
void setup() {
 
void setup() {
 +
  // Set pin modes
 
   pinMode(redpin, OUTPUT);
 
   pinMode(redpin, OUTPUT);
 
   pinMode(greenpin, OUTPUT);
 
   pinMode(greenpin, OUTPUT);
 
   pinMode(bluepin, OUTPUT);
 
   pinMode(bluepin, OUTPUT);
 +
 
   // start with light off
 
   // start with light off
 
   analogWrite(redpin, 255);
 
   analogWrite(redpin, 255);
Line 32: Line 35:
 
void loop() {
 
void loop() {
 
   delay(100);
 
   delay(100);
 +
 +
  // Make bright red
 
   analogWrite(redpin, 0);
 
   analogWrite(redpin, 0);
 
   analogWrite(greenpin, 255);
 
   analogWrite(greenpin, 255);
 
   analogWrite(bluepin, 255);
 
   analogWrite(bluepin, 255);
 +
 
   delay(100);
 
   delay(100);
 +
 +
  // Turn all off
 
   analogWrite(redpin, 255);
 
   analogWrite(redpin, 255);
 
   analogWrite(greenpin, 255);
 
   analogWrite(greenpin, 255);
Line 45: Line 53:
 
Here is the same code with the commands for controlling the light in a function.
 
Here is the same code with the commands for controlling the light in a function.
 
<syntaxhighlight lang=C++>
 
<syntaxhighlight lang=C++>
 +
// Define pins for built-in RGB LED
 
#define redpin 45
 
#define redpin 45
 
#define greenpin 46
 
#define greenpin 46
Line 50: Line 59:
  
 
void setup() {
 
void setup() {
 +
  // Set pin modes
 
   pinMode(redpin, OUTPUT);
 
   pinMode(redpin, OUTPUT);
 
   pinMode(greenpin, OUTPUT);
 
   pinMode(greenpin, OUTPUT);
 
   pinMode(bluepin, OUTPUT);
 
   pinMode(bluepin, OUTPUT);
 +
 
   // start with light off
 
   // start with light off
 
   set_RGB(255, 255, 255);
 
   set_RGB(255, 255, 255);
 
 
}
 
}
  
 
void loop() {
 
void loop() {
 
   delay(100);
 
   delay(100);
 +
 +
  // Make bright red
 
   set_RGB(0, 255, 255);
 
   set_RGB(0, 255, 255);
 +
 
   delay(100);
 
   delay(100);
 +
 +
  // Turn all off
 
   set_RGB(255, 255, 255);
 
   set_RGB(255, 255, 255);
 
}
 
}
  
 
void set_RGB(int r, int g, int b){
 
void set_RGB(int r, int g, int b){
 +
  // Set RGB LED pins based on low=bright (default)
 
   analogWrite(redpin, r);
 
   analogWrite(redpin, r);
 
   analogWrite(greenpin, g);
 
   analogWrite(greenpin, g);
Line 75: Line 91:
 
<syntaxhighlight lang=C++>
 
<syntaxhighlight lang=C++>
 
void set_RGBi(int r, int g, int b){
 
void set_RGBi(int r, int g, int b){
 +
  // Set RGB LED pins based on high=bright
 
   set_RGB(255-r, 255-g, 255-b);
 
   set_RGB(255-r, 255-g, 255-b);
 
}
 
}
Line 80: Line 97:
 
and now, to flash a red light, you could write:
 
and now, to flash a red light, you could write:
 
<syntaxhighlight lang=C++>
 
<syntaxhighlight lang=C++>
 +
// Define pins for built-in RGB LED
 
#define redpin 45
 
#define redpin 45
 
#define greenpin 46
 
#define greenpin 46
Line 85: Line 103:
  
 
void setup() {
 
void setup() {
 +
  // Set pin modes
 
   pinMode(redpin, OUTPUT);
 
   pinMode(redpin, OUTPUT);
 
   pinMode(greenpin, OUTPUT);
 
   pinMode(greenpin, OUTPUT);
 
   pinMode(bluepin, OUTPUT);
 
   pinMode(bluepin, OUTPUT);
 +
 
   // start with light off
 
   // start with light off
 
   set_RGBi(0, 0, 0);
 
   set_RGBi(0, 0, 0);
 
 
}
 
}
  
 
void loop() {
 
void loop() {
 
   delay(100);
 
   delay(100);
 +
 +
  // Make bright red
 
   set_RGBi(255, 0, 0);
 
   set_RGBi(255, 0, 0);
 +
 
   delay(100);
 
   delay(100);
 +
 +
  // Turn all off
 
   set_RGBi(0, 0, 0);
 
   set_RGBi(0, 0, 0);
 
}
 
}
  
 
void set_RGB(int r, int g, int b){
 
void set_RGB(int r, int g, int b){
 +
  // Set RGB LED pins based on low=bright (default)
 
   analogWrite(redpin, r);
 
   analogWrite(redpin, r);
 
   analogWrite(greenpin, g);
 
   analogWrite(greenpin, g);
Line 107: Line 132:
  
 
void set_RGBi(int r, int g, int b){
 
void set_RGBi(int r, int g, int b){
 +
  // Set RGB LED pins based on high=bright
 
   set_RGB(255-r, 255-g, 255-b);
 
   set_RGB(255-r, 255-g, 255-b);
 
}
 
}
Line 173: Line 199:
 
   // Prompt the user for a specific channel
 
   // Prompt the user for a specific channel
 
   Serial.print("Enter "); Serial.print(chan); Serial.print(" value: ");
 
   Serial.print("Enter "); Serial.print(chan); Serial.print(" value: ");
 +
 
   // Get the value, display the value, and return the value
 
   // Get the value, display the value, and return the value
 
   int out = get_int();
 
   int out = get_int();
Line 185: Line 212:
  
 
== References ==
 
== References ==
 +
 +
[[Category:ECE 110]]

Latest revision as of 21:45, 16 September 2022

Introduction

The CX-Bot has a built-in RGB LED on board. The red, green, and blue leads are connected to analog pins 45, 44, and 46 respectively. There are already pullup resistors connected to these leads. When the pins are activated in the code, they default to low which means the relevant color will be as bright as possible. If you want to dim a color, you need to set its pin to a higher value. The pins can be assigned values between 0 and 255, which relate to voltages from 0 V to 5 V.

Leads

  • RED: 45
  • GREEN: 46
  • BLUE: 44

Operation

You need to define the pins, set the pin mode, and write analog values to the appropriate pins. As mentioned above, the lower the value assigned to a pin, the brighter that channel will be. Turning the light off thus requires that all three channels are set to 255; setting all channels to 0 results in a bright white light.

Sample Code

Simple

Here is a very simple sample code that makes the light flash red on and off:

// Define pins for built-in RGB LED
#define redpin 45
#define greenpin 46
#define bluepin 44

void setup() {
  // Set pin modes
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);

  // start with light off
  analogWrite(redpin, 255);
  analogWrite(greenpin, 255);
  analogWrite(bluepin, 255);

}

void loop() {
  delay(100);

  // Make bright red
  analogWrite(redpin, 0);
  analogWrite(greenpin, 255);
  analogWrite(bluepin, 255);

  delay(100);

  // Turn all off
  analogWrite(redpin, 255);
  analogWrite(greenpin, 255);
  analogWrite(bluepin, 255);
}

Simple with functions

Here is the same code with the commands for controlling the light in a function.

// Define pins for built-in RGB LED
#define redpin 45
#define greenpin 46
#define bluepin 44

void setup() {
  // Set pin modes
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);

  // start with light off
  set_RGB(255, 255, 255);
}

void loop() {
  delay(100);

  // Make bright red
  set_RGB(0, 255, 255);

  delay(100);

  // Turn all off
  set_RGB(255, 255, 255);
}

void set_RGB(int r, int g, int b){
  // Set RGB LED pins based on low=bright (default)
  analogWrite(redpin, r);
  analogWrite(greenpin, g);
  analogWrite(bluepin, b);
}

Also, if you generally want to follow the typical "more is brighter" idea, you can write a function that sends 255-x to channel x; just add the code:

void set_RGBi(int r, int g, int b){
  // Set RGB LED pins based on high=bright 
  set_RGB(255-r, 255-g, 255-b);
}

and now, to flash a red light, you could write:

// Define pins for built-in RGB LED
#define redpin 45
#define greenpin 46
#define bluepin 44

void setup() {
  // Set pin modes
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);

  // start with light off
  set_RGBi(0, 0, 0);
}

void loop() {
  delay(100);

  // Make bright red
  set_RGBi(255, 0, 0);

  delay(100);

  // Turn all off
  set_RGBi(0, 0, 0);
}

void set_RGB(int r, int g, int b){
  // Set RGB LED pins based on low=bright (default)
  analogWrite(redpin, r);
  analogWrite(greenpin, g);
  analogWrite(bluepin, b);
}

void set_RGBi(int r, int g, int b){
  // Set RGB LED pins based on high=bright 
  set_RGB(255-r, 255-g, 255-b);
}

Interactive Colors

What if you want to play around with different values for the red, green, and blue channels? You could hard-code an array of values, or you could let the user type in values for red, green, and blue and then display that combination! Here is code that does just that - note the use of subfunctions for any operation that is performed more than once!

// Define pins for built-in RGB LED
#define redpin 45
#define greenpin 46
#define bluepin 44

void setup() {
  // Start serial monitor
  Serial.begin(9600);
  
  // Set pin modes
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);

  // Start with light off
  set_RGBi(0, 0, 0);
}

void loop() {
  // Use subfunction to get channel information
  int red   = get_channel("red");
  int green = get_channel("green");
  int blue  = get_channel("blue");

  // Create a buffer for formatted printing later
  char buffer[30];
  
  // Generate and print formatted information
  sprintf(buffer, "Displaying (%d, %d, %d)\n", red, green, blue);
  Serial.println(buffer);
  set_RGBi(red, green, blue);
}

void set_RGB(int r, int g, int b){
  // Set RGB LED pins based on low=bright (default)
  analogWrite(redpin, r);
  analogWrite(greenpin, g);
  analogWrite(bluepin, b);
}

void set_RGBi(int r, int g, int b){
  // Set RGB LED pins based on high=bright 
  set_RGB(255-r, 255-g, 255-b);
}

int get_int(){
  // Get an integer from the serial monitor then clear the entry and return the value
  while(Serial.available() == 0){
  }
  int out = Serial.parseInt();
  Serial.read();
  delay(100);
  return out;
}

int get_channel(char *chan){
  // Prompt the user for a specific channel
  Serial.print("Enter "); Serial.print(chan); Serial.print(" value: ");

  // Get the value, display the value, and return the value
  int out = get_int();
  Serial.println(out);
  return out;
}

Notes

  • Brightness does not change linearly with the channel value!

References