#include <Arduino.h>
#include <Servo.h> // Included as permitted, though not utilized in this example
// Define pin connections
const int led1Pin = 22; // Single-color LED control pin
// RGB LED control pins (using PWM pins for brightness control)
const int rgbPinR = 10;
const int rgbPinG = 9;
const int rgbPinB = 8;
// Timing intervals (in milliseconds)
const unsigned long interval = 2000; // 2 seconds interval
// Variables for non-blocking timing
unsigned long previousStatusTime = 0; // for led1 blinking
unsigned long previousColorTime = 0; // for RGB color change
bool ledState = false; // Current state of led1 (off initially)
const int numColors = 7; // Total number of colors in the sequence
int colorIndex = 0; // Start with the first color (Red)
// Structure to hold a color (RGB values)
struct Color {
int r;
int g;
int b;
};
// Predefined sequence of colors
Color colors[numColors] = {
{255, 0, 0}, // Red
{0, 255, 0}, // Green
{0, 0, 255}, // Blue
{255, 255, 0}, // Yellow
{0, 255, 255}, // Cyan
{255, 0, 255}, // Magenta
{255, 255, 255} // White
};
void setup() {
// Initialize the single-color LED pin and set it off initially
pinMode(led1Pin, OUTPUT);
digitalWrite(led1Pin, LOW);
// Initialize the PWM pins for the RGB LEDs
pinMode(rgbPinR, OUTPUT);
pinMode(rgbPinG, OUTPUT);
pinMode(rgbPinB, OUTPUT);
// Set initial RGB LED color (Red, the first in the sequence)
analogWrite(rgbPinR, colors[colorIndex].r);
analogWrite(rgbPinG, colors[colorIndex].g);
analogWrite(rgbPinB, colors[colorIndex].b);
}
void loop() {
unsigned long currentMillis = millis();
// Toggle the single-color LED every 2 seconds
if (currentMillis - previousStatusTime >= interval) {
previousStatusTime = currentMillis;
ledState = !ledState;
digitalWrite(led1Pin, ledState ? HIGH : LOW);
}
// Update the color of the RGB LEDs every 2 seconds
if (currentMillis - previousColorTime >= interval) {
previousColorTime = currentMillis;
// Proceed to the next color in the sequence (wrap around at the end)
colorIndex = (colorIndex + 1) % numColors;
analogWrite(rgbPinR, colors[colorIndex].r);
analogWrite(rgbPinG, colors[colorIndex].g);
analogWrite(rgbPinB, colors[colorIndex].b);
}
}