Unit 3: Multi Light LEDs
Code for Sketch One:
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);
}
void loop() {
// Continuous Green state
setColor(0, 255, 0); // Green
delay(10000); // Stay green for 10 seconds
// Flashing Green (Paused) state
for (int i = 0; i < 5; i++) {
setColor(0, 255, 0); // Green
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
delay(500); // On for 0.5 seconds
setColor(0, 0, 0); // Off
delay(500); // Off for 0.5 seconds
}
// Continuous Red (Off) state
setColor(255, 0, 0); // Red
delay(10000); // Stay red for 10 seconds
}
Code for Sketch Two:
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 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);
}
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);
}
delay(500);
}