Unit 3: Multi Light LEDs

Updated notes at base of page.

 

Code for Sketch One: 


// Define RGB LED pins
const int PIN_RED = 11;    // Red LED pin
const int PIN_GREEN = 10;  // Green LED pin
const int PIN_BLUE = 9;    // Blue LED pin

// Function to set the color of the RGB LED
void setColor(int R, int G, int B) {
  analogWrite(PIN_RED, R);
  analogWrite(PIN_GREEN, G);
  analogWrite(PIN_BLUE, B);
}

void setup() {
  // Set all RGB pins as outputs
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE, OUTPUT);
}

void loop() {
  // Continuous Green state
  setColor(0, 255, 0);     // Solid green
  delay(10000);            // Stay green for 10 seconds

  // Flashing Green (Paused) state
  for (int i = 0; i < 5; i++) {
    setColor(0, 255, 0);   // Green on
    delay(500);            // On for 0.5 seconds
    setColor(0, 0, 0);     // Off
    delay(500);            // Off for 0.5 seconds
  }

  // Flashing Red (Triggered) state
  for (int i = 0; i < 5; i++) {
    setColor(255, 0, 0);   // Red on
    delay(500);            // On for 0.5 seconds
    setColor(0, 0, 0);     // Off
    delay(500);            // Off for 0.5 seconds
  }

  // Continuous Red (Alert) state
  setColor(255, 0, 0);     // Solid red
  delay(10000);            // Stay red for 10 seconds
}

  

Code for Sketch Two, with serial output:

(When using serial output, your board wil remain connected to your computer, and you need to ensure your BAUD rate is set to 9600, or else the output will be garbage).

 


  const int PIN_RED = 11;    // Red LED pin
const int PIN_GREEN = 10;  // Green LED pin
const int PIN_BLUE = 9;    // Blue LED pin

// Function to set the color of the LED
void setColor(int R, int G, int B) {
  analogWrite(PIN_RED, R);
  analogWrite(PIN_GREEN, G);
  analogWrite(PIN_BLUE, B);
}

void setup() {
  // Set all three pins to output mode
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE, OUTPUT);
  
  // Start serial communication
  Serial.begin(9600);
  Serial.println("System started.");
}

void loop() {
  // Continuous Green state
  Serial.println("State: Continuous Green for 10 seconds");
  setColor(0, 255, 0);   // Green
  delay(10000);          // Stay green for 10 seconds

  // Flashing Green (Paused) state
  Serial.println("State: Flashing Green (Paused) for 5 seconds");
  for (int i = 0; i < 5; i++) {
    Serial.print("  Flashing Green ON (");
    Serial.print(i + 1);
    Serial.println("/5)");
    setColor(0, 255, 0); // Green
    delay(500);
    
    Serial.print("  Flashing Green OFF (");
    Serial.print(i + 1);
    Serial.println("/5)");
    setColor(0, 0, 0);   // Off
    delay(500);
  }

  // Flashing Red (Triggered) state
  Serial.println("State: Flashing Red (Triggered) for 5 seconds");
  for (int i = 0; i < 5; i++) {
    Serial.print("  Flashing Red ON (");
    Serial.print(i + 1);
    Serial.println("/5)");
    setColor(255, 0, 0); // Red
    delay(500);
    
    Serial.print("  Flashing Red OFF (");
    Serial.print(i + 1);
    Serial.println("/5)");
    setColor(0, 0, 0);   // Off
    delay(500);
  }

  // Continuous Red (Off) state
  Serial.println("State: Continuous Red for 10 seconds");
  setColor(255, 0, 0);   // Red
  delay(10000);          // Stay red for 10 seconds
}
  

Code for Sketch Two:


  // Define RGB LED pins
const int PIN_RED = 9;     // Red LED pin
const int PIN_GREEN = 10;  // Green LED pin
const int PIN_BLUE = 11;   // Blue LED pin

// Function to set the color of the RGB LED
void setColor(int R, int G, int B) {
  analogWrite(PIN_RED, R);
  analogWrite(PIN_GREEN, G);
  analogWrite(PIN_BLUE, B);
}

void setup() {
  // Set all RGB pins as outputs
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE, OUTPUT);
}

void loop() {
  // Cycle through rainbow colors

  setColor(255, 0, 0);      // Red
  delay(500);

  setColor(255, 127, 0);    // Orange
  delay(500);

  setColor(255, 255, 0);    // Yellow
  delay(500);

  setColor(0, 255, 0);      // Green
  delay(500);

  setColor(0, 0, 255);      // Blue
  delay(500);

  setColor(75, 0, 130);     // Indigo
  delay(500);

  setColor(148, 0, 211);    // Violet
  delay(500);
}

      

Notes: 

Some Heisters have inadvertly been supplied with ANODE RGB LEDs. These work in essentially the same way, but the longest pin of the LED needs to be connected to the 5V power input. If your RGB isn't working straight away, test this out.

Many thanks to @TheAceMagpie for identifying this, and sharing this helpful webpage for further information about anode and cathode RGB LEDs.