mdraber
Published © GPL3+

How to select colors for Arduino led projects. RGB vs HSV

This tutorial will show you two different approaches for selecting colors for your Arduino LED projects

IntermediateFull instructions provided1,507
How to select colors for Arduino led projects. RGB vs HSV

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×4
I2C OLED display
×1
8x8 WS2812 LED matrix
×1

Story

Read more

Schematics

Conenctivity for LED coror selection device

Code

Setting all 64 led of the matrix to a color selected using RGB color model

Arduino
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <FastLED.h>

int Red;
int Green;
int Blue;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1// Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define NUM_LEDS 64 
#define DATA_PIN 13
CRGB leds[NUM_LEDS];


void setup() { 

  FastLED.addLeds<WS2812,DATA_PIN,GRB>(leds,NUM_LEDS);
  FastLED.setBrightness(5);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  
  display.display();
  delay(2000);
  display.clearDisplay();
  display.display();
  
  display.fillRect(0,0,63,16,SSD1306_WHITE);
  display.fillRect(65,0,63,16,SSD1306_WHITE);
  display.setTextSize(2);
  display.setTextColor(SSD1306_BLACK); 
  display.setCursor(15,1); 
  display.println("RGB"); 
  display.setCursor(80,1); 
  display.println("HSV");
  display.fillRect(0,15,63,16,SSD1306_WHITE);
  display.fillRect(0,32,63,16,SSD1306_WHITE);
  display.fillRect(0,49,63,16,SSD1306_WHITE);
  display.setCursor(5,17);
  display.println("R:");
  display.setCursor(5,33);
  display.println("G:");
  display.setCursor(5,50);
  display.println("B:");
  
  
  display.display();
  
}

void loop() { 
  //reading the composite color values from the 3 potentiometers
  Red= map(analogRead(A2),0,1023,0,275  );
  Green= map(analogRead(A1),0,1023,0,275  );
  Blue= map(analogRead(A0),0,1023,0,275  );
  //displaying read values on OLED display
  display.fillRect(0,15,63,16,SSD1306_WHITE);
  display.fillRect(0,32,63,16,SSD1306_WHITE);
  display.fillRect(0,49,63,16,SSD1306_WHITE);
  display.setCursor(5,17);
  display.print("R:");
  display.print(Red);
  display.setCursor(5,33);
  display.print("G:");
  display.print(Green);
  display.setCursor(5,50);
  display.print("B:");
  display.print(Blue);
  display.display();
  // setting all 64 led to the color made of selected R G B values
  for (int i=0;i<64;i++) {
      leds[i]=CRGB(Red,Green,Blue);
  }
  // redisplaing all leds 
  FastLED.show();
}

Setting one half of the matrix to the RGB color and the other half to HSV color

Arduino
RGB color is selected with 3 potentiometers
HSV color is selected with 1 potentiometer
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <FastLED.h>

int Red;
int Green;
int Blue;
int C_HSV;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1// Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


uint16_t Colors [27]{0x7FFF00,0x6495ED,0xDC143,0x00008B,0x8B008B,
                     0xFF8C00,0x00CED1,0x9400D3,0xFF1493,0x228B22,
                     0xFFD700,0xADFF2F,0x7CFC00,0x87CEFA,0x00FF00,
                     0xFF00FF,0x0000CD,0xFFA500,0x9ACD32,0xFFE4C4,
                     0x00FFFF,0x008B8B,0xE9967A,0xFFFACD,0xADD8E6,
                     0xFAFAD2,0xFFEFD5};


#define NUM_LEDS 64 
#define DATA_PIN 13
CRGB leds[NUM_LEDS];


void setup() { 

  FastLED.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
  FastLED.setBrightness(5);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  
  display.display();
  delay(2000);
  display.clearDisplay();
  display.display();
  
  display.fillRect(0,0,63,16,SSD1306_WHITE);
  display.fillRect(65,0,63,16,SSD1306_WHITE);
  display.setTextSize(2);
  display.setTextColor(SSD1306_BLACK); 
  display.setCursor(15,1); 
  display.println("RGB"); 
  display.setCursor(80,1); 
  display.println("HSV");
  display.fillRect(0,15,63,16,SSD1306_WHITE);
  display.fillRect(0,32,63,16,SSD1306_WHITE);
  display.fillRect(0,49,63,16,SSD1306_WHITE);
  display.setCursor(5,17);
  display.println("R:");
  display.setCursor(5,33);
  display.println("G:");
  display.setCursor(5,50);
  display.println("B:");
  display.fillRect(80,15,63,16,SSD1306_WHITE);
  display.setCursor(95,17); 
  display.println("Hue:");
  
  
  display.display();
  
}

void loop() { 
// reading RGB composite color values from 3 potentiometers  
  Red= map(analogRead(A2),0,1023,0,260  );
  Green= map(analogRead(A1),0,1023,0,260  );
  Blue= map(analogRead(A0),0,1023,0,260  );
// reading HSV color value from the potentiometer
  C_HSV= map(analogRead(A3),0,1023,0,260  );
// Displaying selected RGB and HSV color values on oled display
  display.fillRect(0,15,63,16,SSD1306_WHITE);
  display.fillRect(0,32,63,16,SSD1306_WHITE);
  display.fillRect(0,49,63,16,SSD1306_WHITE);
  display.setCursor(5,17);
  display.print("R:");
  display.print(Red);
  display.setCursor(5,33);
  display.print("G:");
  display.print(Green);
  display.setCursor(5,50);
  display.print("B:");
  display.print(Blue);
  display.fillRect(65,15,63,16,SSD1306_WHITE);
  display.setCursor(67,17); 
  display.print("H:");
  display.print(C_HSV);
  display.display();
  // Setting LEDs in first 4 columns to selected RGB color
  // Setting leds in columns 5-8 to selected HSV color
  for (int i=0;i<8;i++) {
      //leds[i]=CRGB(Red,Green,Blue);
      leds[8*i+5]=CRGB(Red,Green,Blue);
      leds[8*i+6]=CRGB(Red,Green,Blue);
      leds[8*i+7]=CRGB(Red,Green,Blue);
      leds[8*i]=CHSV( C_HSV, 255, 255);
      leds[8*i+1]=CHSV( C_HSV, 255, 255);
      leds[8*i+2]=CHSV( C_HSV, 255, 255);
  }
// Redisplaying all leds
  FastLED.show();
   

}

Credits

mdraber
50 projects • 74 followers

Comments