With this code you can control your RGB mood light simply via one rotary controller which you can fit in your wall next to the other light dimmers.
What does the code do now:
- You can turn the knob to change brightness or Hue
- Push the button to select between menu's
1=Brightness
2=Hue
3=Random
4=BlinkM preset
5=Power off - Get informed at which menu you are on, blink once, or twice.
- power a led to simulate a relais for powering an external driver
- Turn external power supply (for power leds) on and off
The future add-ons I have in mind are:
- Turn back to menu 1 of not touched the controller for x sec.
- Network control (ethernet shield)
- IR control
- Sound control
- Program a few presets
- A Nice Display to see all the information on.
Presets I had in mind are:
Random (Randomize Colours)
Sundown (change between red and orange/yellow)
IceAge (blue and white's)
Sound (Respond to sound/frequency)
As I use blinkm's I also want to use the blinkm address space to fancy it up.
Code PART 1
/* --------------------------------------------------------------------
Welcome to La Bougie v 1.7 2009-06-20
A controller for RGB Lightning using BlinkM
Connect a rotary controller with common gnd
A to digital pin 7
B to digital pin 8
switch to digital pin 5
menu 1 : Controlling Brightness
menu 2 : Controlling Hue
menu 3 : Going random, with controlled brightness
menu 4 : Run a pre-programmed BlinkM scene from memory
menu 5 : Turn the lights and external driver off via relais_pin
-------------------------------------------------------------------- */
#include "RotaryEncoder.h"
#include "Wire.h"
#include "BlinkM_funcs.h"
#include <Ethernet.h>
#define blinkm_addr 0x00
int my_menu = 0; // The first menu to start with (has to be 0 for now)
int my_hue = 128; // Default hue at startup
int my_hue_random= 100; // just to initialize, it will get random in the code
int my_brightness = 250; // Default brightness at startup
int my_power = false; // false will eventually disable an external driver for power-leds
int my_power_history = false;
int my_blinkm_program = 10; // Pre-programmed blinkm program between 0..18 (I think) datasheet is not accurate
int my_information = false;
int relais_pin = 6; // a reserved port for a relais that triggers the driver for the power-leds
int when_to_randomize_counter = 0; // counter needed to calculate when to random
int when_to_randomize = 100; // increase to change cycles between randomize in menu 3
int intro = false;
byte bri_val;
byte hue_val;
// Fadespeed that BlinkM uses to fade between colours (1=slowest, 255=instant, 0=invalid)
RotaryEncoder rotary(7, 8, 5);
// Connecting Rotary channels A en B to Digital pin 7 and 8.
// Digital pin 5 is for the switch
// Common to Ground, the RotaryEncoder does use internal pull-ups (read RotaryEncoder library)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 10, 0, 110 };
void setup()
{
BlinkM_begin();
// BlinkM_beginWithPower(); // to make blinkm use the power of Arduino
BlinkM_stopScript(blinkm_addr); // turn off BlinkM script
Serial.print("Blinkm default script is stopped\n");
rotary.minimum(0);
rotary.maximum(255);
rotary.position(100);
pinMode(relais_pin, OUTPUT); // Defining the relais port to use as an output port
randomSeed(analogRead(0)); // getting a randomized number from an analog port so we are sure it's random.
Serial.begin(9600);
Serial.print("Brightness initialized to: ");
Serial.print(my_brightness);
Serial.print("\n");
Serial.print("Hue initialized to: ");
Serial.print(my_hue);
Serial.print("\n");
Serial.print("Power is off\n");
Serial.print("Relais for power-leds is not active\n");
Serial.print("press the button to power up\n\n");
}
void loop()
{
if (my_power == false) {
BlinkM_stopScript(blinkm_addr); // turn off BlinkM script
BlinkM_setFadeSpeed(blinkm_addr, 5); // Slow down fade speed, so the lights will dim before power is cut
BlinkM_fadeToHSB( blinkm_addr, hue_val, 255, 0); // Start the fade out
if (my_power_history == true) {delay(5000); my_power_history = false;} // if the power before was true, the delay wil first dim the lights and next kill the power source
digitalWrite(relais_pin, LOW); // un-power a relais that triggers the power-led driver
}
if(rotary.pressed()) // If you press the (rotary) button, you are changing the menu.
// The following rules will apply only once after the button has been pushed until further notice
{
if (my_power == false) {intro = true; goto intro_start;}
Serial.print("\n\n Rotary Pressed\n\n");
if (my_menu == 5) // I only defined 5 menu's, you can add more by increasing this my_menu number)
my_menu = 0; // if you are at the last menu, go back to the initial menu number
else
increase_menu_number:
my_menu ++; // increase the menu number if it's not allready at max.
Serial.print("Menu nr: ");
Serial.print(my_menu);
Serial.print("\n");
delay(300); // wait a bit so the button is not repeated many times
if (my_menu == 1) {
my_information = true; // Informing the user we are at menu 1
rotary.position(my_brightness); // Giving the rotary the current brightness level back so we can controll it.
Serial.print("Now controlling brightness");
Serial.print("Current brightness level = ");
Serial.print(rotary.position());
Serial.print("\n\n");
}
else if (my_menu == 2) {
my_information = true; // Informing the user we are at menu 2
rotary.position(my_hue); // Giving the rotary the current Hue level back so we can controll it.
my_hue = rotary.position(); // This gives the hue the new value
Serial.print("Now controlling Hue");
Serial.print("Current Hue Colour = ");
Serial.print(rotary.position());
Serial.print("\n\n\n");
}
else if (my_menu == 3) {
my_information = true; // Informing the user we are at menu 3
rotary.position(my_brightness); // Giving the rotary the current brightness level back so we can controll it.
Serial.print("Now going random colour \n");
Serial.print("We can still controll brightness in random mode\n");
Serial.print("Current brightness level = ");
Serial.print(my_brightness);
Serial.print("\n\n");
}
else if (my_menu == 4)
{
// my_information = true; // Informing the user we are at menu 4
Serial.print("Running default program from BlinkM\n");
BlinkM_setFadeSpeed(blinkm_addr, 250); // For HUE testing purposes I want to change colours fast.
BlinkM_setTimeAdj(blinkm_addr, 0); // Set playspeed to normal
BlinkM_playScript(blinkm_addr, my_blinkm_program, 0, 0); // Play pre-programmed script from BlinkM looping forever
Wire.send('p');
}
else if (my_menu == 5)
{
Serial.print("Killing lights, power is now off\n");
Serial.print("Relais for power-leds is de-activated\n");
Serial.print("press the button to power up\n\n");
my_power = false;
// BlinkM_setFadeSpeed(blinkm_addr, 10);
}
}
// Here the code ends that apply to the press of the button. The next rules will continue to loop.
I'm not a coder expert, so If you have suggestions/improvements, they are all welcome as I'm learning on my way.
Foto's and video's will follow
In 2 to 3 month's my house is rebuild with ~ 15 meter rgb strips.
At the moment it's all on boards.