Arduino Moodlight library

Arduino library to control an RGB led with Hue, Saturation and Brigthness values.

Steps:

  • Download the library : Arduino Moodlight library
  • Unpack the library in the Arduino libraries folder (standard in documents, arduino).
  • Restart Arduino if it was running.
  • Open the moodlight file in File > Examples > MoodLight > moodlight.

You can see and use the original code in this example: Arduino Programming – HSB to RGB

You can find more information about using Arduino libraries on the Arduino Environment page.

Moodlight.pde :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Include the MoodLight library.
#include <MoodLight.h>

// inputs
int sensorPin = 0;

// outputs
int ledPinR = 9;  // pwm pin with red led
int ledPinG = 10; // pwm pin with green led
int ledPinB = 11; // pwm pin with blue led

// other variables
int sensorVal;   // store value from sensorPin

int hue;         // use value between 0 - 359
int saturation;  // use value between 0 - 255
int brightness;  // use value between 0 - 255

// create MoodLight object
MoodLight ml = MoodLight();

void setup() {  
 
   pinMode(ledPinR, OUTPUT);  
   pinMode(ledPinG, OUTPUT);  
   pinMode(ledPinB, OUTPUT);  
   
}

void loop() {
 
  // read the sensor (potmeter)
  sensorVal = analogRead(sensorPin);
 
  // map the value from 0-1023 to a value between 0 and 359
  hue        = map(sensorVal,0, 1023,0, 359);
  saturation = 255;
  brightness = 255;
 
  // set the Moodlight values  
  ml.setHSB(hue, saturation , brightness);

  // read the Red, Green and Blue values
  // and write them to the led pins
  analogWrite(ledPinR, ml.getRed());
  analogWrite(ledPinG, ml.getGreen());
  analogWrite(ledPinB, ml.getBlue());  
 
}