/*
==== Task 2 - RGB Colour Cycle Using a Function (Alternative Version) ====
Author: Roberto Palozzo
===========================================================================
*/
int pins[3] = {14, 18, 17}; // red, green, blue pins grouped in an array
// Turns on a single LED for "onTime" ms, then turns it off
void showColor(int pin, int onTime) {
digitalWrite(pin, HIGH);
delay(onTime);
digitalWrite(pin, LOW);
}
// Turns on all LEDs in the array together (white light)
void showWhite(int onTime) {
for (int i = 0; i < 3; i++) {
digitalWrite(pins[i], HIGH); // turn all LEDs on
}
delay(onTime);
for (int i = 0; i < 3; i++) {
digitalWrite(pins[i], LOW); // turn all LEDs off
}
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 3; i++) {
pinMode(pins[i], OUTPUT); // set each pin as output using the array
}
}
void loop() {
for (int i = 0; i < 3; i++) {
showColor(pins[i], 500); // cycles through red, green, blue automatically
}
showWhite(500);
}