ECE 110/Equipment/Built in RGB LED

From PrattWiki
Revision as of 16:20, 21 August 2022 by DukeEgr93 (talk | contribs) (Operation)
Jump to navigation Jump to search

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: 44
  • BLUE: 46

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 redpin 45
#define greenpin 46
#define bluepin 44

void setup() {
  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);
  analogWrite(redpin, 0);
  analogWrite(greenpin, 255);
  analogWrite(bluepin, 255);
  delay(100);
  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 redpin 45
#define greenpin 46
#define bluepin 44

void setup() {
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  // start with light off
  set_RGB(255, 255, 255);

}

void loop() {
  delay(100);
  set_RGB(0, 255, 255);
  delay(100);
  set_RGB(255, 255, 255);
}

void set_RGB(int r, int g, int b){
  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(255-r, 255-g, 255-b);
}

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

#define redpin 45
#define greenpin 46
#define bluepin 44

void setup() {
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  // start with light off
  set_RGBi(0, 0, 0);

}

void loop() {
  delay(100);
  set_RGBi(255, 0, 0);
  delay(100);
  set_RGBi(0, 0, 0);
}

void set_RGB(int r, int g, int b){
  analogWrite(redpin, r);
  analogWrite(greenpin, g);
  analogWrite(bluepin, b);
}

void set_RGBi(int r, int g, int b){
  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