/*
* RGB LED Color Mixing Station
* Control RGB LED colors using 3 potentiometers
* Created for Wokwi Simulator
*
* Features:
* - Real-time color mixing
* - Individual RGB channel control via potentiometers
* - Serial monitor output showing RGB values
* - Smooth color transitions
*/
// RGB LED Pin Definitions (PWM pins required)
const int RED_PIN = 9; // Red LED connected to PWM pin 9
const int GREEN_PIN = 10; // Green LED connected to PWM pin 10
const int BLUE_PIN = 11; // Blue LED connected to PWM pin 11
// Potentiometer Pin Definitions (Analog pins)
const int RED_POT = A0; // Potentiometer for Red channel
const int GREEN_POT = A1; // Potentiometer for Green channel
const int BLUE_POT = A2; // Potentiometer for Blue channel
// Variables to store color values (0-255)
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
// Variables to store potentiometer readings (0-1023)
int redPotValue = 0;
int greenPotValue = 0;
int bluePotValue = 0;
void setup() {
// Initialize serial communication
Serial.begin(9600);
Serial.println("========================================");
Serial.println(" RGB LED Color Mixing Station");
Serial.println("========================================");
Serial.println("Turn the potentiometers to mix colors!");
Serial.println();
// Set RGB LED pins as outputs
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
// Set potentiometer pins as inputs (optional, analog pins are input by default)
pinMode(RED_POT, INPUT);
pinMode(GREEN_POT, INPUT);
pinMode(BLUE_POT, INPUT);
// Start with a test color (white) to verify LED is working
setColor(255, 255, 255);
delay(1000);
// Then turn off
setColor(0, 0, 0);
// Display initial message
Serial.println("System Ready!");
Serial.println("Format: R:xxx G:xxx B:xxx | HEX:#XXXXXX");
Serial.println("----------------------------------------");
}
void loop() {
// Read potentiometer values (0-1023)
redPotValue = analogRead(RED_POT);
greenPotValue = analogRead(GREEN_POT);
bluePotValue = analogRead(BLUE_POT);
// Map potentiometer values to RGB range (0-255)
redValue = map(redPotValue, 0, 1023, 0, 255);
greenValue = map(greenPotValue, 0, 1023, 0, 255);
blueValue = map(bluePotValue, 0, 1023, 0, 255);
// Set the RGB LED color
setColor(redValue, greenValue, blueValue);
// Display color values in Serial Monitor
displayColorInfo();
// Small delay for stability
delay(100);
}
// Function to set RGB LED color
void setColor(int red, int green, int blue) {
// Write PWM values to LED pins
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}
// Function to display color information
void displayColorInfo() {
// Print RGB values
Serial.print("R:");
printPaddedValue(redValue);
Serial.print(" G:");
printPaddedValue(greenValue);
Serial.print(" B:");
printPaddedValue(blueValue);
// Print HEX color code
Serial.print(" | HEX:#");
printHex(redValue);
printHex(greenValue);
printHex(blueValue);
// Print color name (if identifiable)
Serial.print(" | ");
Serial.print(getColorName());
// Print color bar visualization
Serial.print(" | ");
printColorBar();
Serial.println();
}
// Function to print padded value (for alignment)
void printPaddedValue(int value) {
if (value < 100) Serial.print(" ");
if (value < 10) Serial.print(" ");
Serial.print(value);
}
// Function to print hexadecimal values
void printHex(int value) {
if (value < 16) Serial.print("0");
Serial.print(value, HEX);
}
// Function to get approximate color name
String getColorName() {
// All colors very low
if (redValue < 30 && greenValue < 30 && blueValue < 30) {
return "Black/Off ";
}
// All colors high
else if (redValue > 200 && greenValue > 200 && blueValue > 200) {
return "White ";
}
// Pure or dominant colors
else if (redValue > 200 && greenValue < 100 && blueValue < 100) {
return "Red ";
}
else if (greenValue > 200 && redValue < 100 && blueValue < 100) {
return "Green ";
}
else if (blueValue > 200 && redValue < 100 && greenValue < 100) {
return "Blue ";
}
// Secondary colors
else if (redValue > 150 && greenValue > 150 && blueValue < 100) {
return "Yellow ";
}
else if (redValue < 100 && greenValue > 150 && blueValue > 150) {
return "Cyan ";
}
else if (redValue > 150 && greenValue < 100 && blueValue > 150) {
return "Magenta ";
}
// Tertiary and other colors
else if (redValue > 150 && greenValue > 100 && blueValue < 80) {
return "Orange ";
}
else if (redValue > 150 && greenValue < 100 && blueValue > 100) {
return "Purple ";
}
else if (redValue < 100 && greenValue > 150 && blueValue > 100) {
return "Teal ";
}
else if (redValue > 100 && greenValue < 80 && blueValue > 100) {
return "Violet ";
}
else if (redValue > 200 && greenValue > 150 && blueValue > 150) {
return "Pink ";
}
else {
return "Mixed Color ";
}
}
// Function to print a visual color bar
void printColorBar() {
int barLength = (redValue + greenValue + blueValue) / 20; // 0-38 characters
Serial.print("[");
for (int i = 0; i < barLength; i++) {
Serial.print("=");
}
for (int i = barLength; i < 38; i++) {
Serial.print(" ");
}
Serial.print("]");
}
Comments
Post a Comment