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 :
// Include the MoodLight library.
#include
// 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());
}