Ok…Think the sketch is ready.Had some trouble with smooth color changing,but this will do for the moment.
If someone has an id?e on how to do this better please comment.
I?m a newbie to programming so it?s a lot of copy and paste, but some are my own code.
Great thanx to hek for a joyful hobby.
First you need to get the library for the display “Ledcontrol.h”.
You find it here.
http://playground.arduino.cc/Main/LedControl#.UwpvhoU9RB5
When sketch is uploaded,you need to put some Luupcode in Veras “Startup Lua”.
You see Luupcode in the comments of the sketch.
/* Displaysketch showing Watts from Wattdevice in Vera
Using a 5-digit 7-segment display with a MAX7219 displaydriver.
- pin 8 is connected to the DATA IN on MAX7219 chip pin 1 (SI pin 1 on displaycard)
- pin 7 is connected to the CLK on MAX7219 chip pin 13 (SCK pin 3 on displaycard)
- pin 4 is connected to the LOAD(/CS) on MAX7219 chip pin 12 (SC# pin 4 on displaycard)
Including breathing and fading RGB-diod with Common Anod
Anod VCC 5V
Red pin 3 with 330ohm
Green pin 5 with 330ohm
Blue pin 6 with 330ohm
For breathing effect inspiration,thanx to Sean Voisen.
(http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/)
I?m that lazy web surfing Arduino hacker.
* LUUP code to put in Veras Startup Lua * (With a lot of help from hek)
---------------------------------------------------------------------------------------------------------------------------------------
function handleWattChange(lul_device, lul_service, lul_variable, lul_value_old, lul_value_new)
luup.call_action("urn:upnp-arduino-cc:serviceId:arduinonode1", "SendNodeCommand", {variableId="VAR_1", value=lul_value_new}, <arduinoDeviceID>)
local w = luup.variable_get("urn:micasaverde-com:serviceId:EnergyMetering1", "Watts", <wattDeviceID>)
luup.variable_set("urn:micasaverde-com:serviceId:EnergyMetering1", "Watts", w, <arduinoDeviceID>)
end
luup.variable_watch("handleWattChange", "urn:micasaverde-com:serviceId:EnergyMetering1", "Watts", <wattDeviceID>)
--------------------------------------------------------------------------------------------------------------------------------------
*/
#include "LedControl.h" // need the library
#include <RF24.h>
#include <Sensor.h>
#include <Relay.h>
#include <SPI.h>
#include <EEPROM.h>
#include <Time.h>
#include <math.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID 88
#define CHILD_ID 1 // Id of the sensor child
Relay gw(9,10);
int watt = 0;
int v = 0;
int firstrun = 0;
/* we always wait a bit between updates of the display */
int delaytime=1000; //1000 one second
LedControl lc=LedControl(8,7,4,1); // lc is our object
int redPin = 3;
int greenPin = 5;
int bluePin = 6;
int redVal = 255;
int greenVal = 255;
int blueVal = 255;
int oldRedVal = 255;
int oldGreenVal = 255;
int oldBlueVal = 255;
int newRedVal = 255;
int newGreenVal = 255;
int newBlueVal = 255;
int red = 255;
int green = 255;
int blue = 255;
float val = 0;
float rate = 1; //Breathingrate
int newmess = 0;
int fadingok = 0;
int breathing = 0;
void setup(){
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
// Register this device as power sensor
gw.sendSensorPresentation(CHILD_ID, S_POWER);
// Fetch value from gw for first run
watt = atoi(gw.getStatus(CHILD_ID, V_VAR1));
// the zero refers to the MAX7219 number, it is zero for 1 chip
lc.shutdown(0,false); // turn off power saving, enables display
lc.setIntensity(0,2); // sets brightness (0~15 possible values)
lc.clearDisplay(0); // clear screen
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// RGB write black
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
void loop() {
if (firstrun == 0){
firstrun=1;
//Serial.print("firstrun");
wattcalc();
}
//When fading is ready and breathing value is low,enter breathing mode
if(fadingok == 1 && val<1){
breathing =1;
}
// When new message breath until value is low
if((newmess == 1 && val>0) || fadingok == 1 ){
//Breathing math
val = (exp(sin(millis()/2000.0*PI*rate)) - 0.36787944)*108.0;
val = map(val, 0, 258, 0, 190);
int Invred = map(redVal, 0, 255, 255, 0);
int Invgreen = map(greenVal, 0, 255, 255, 0);
int Invblue = map(blueVal, 0, 255, 255, 0);
if(breathing == 1){
analogWrite(redPin, redVal+(val * Invred / 255));
analogWrite(greenPin, greenVal+(val * Invgreen / 255));
analogWrite(bluePin, blueVal+(val * Invblue / 255));
}
}
else if((oldRedVal != newRedVal) || (oldGreenVal != newGreenVal) || (oldBlueVal != newBlueVal) ){
//Serial.print("to Colorfade from loop");
breathing = 0;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
Colorfade();
}
else{
fadingok = 1;
}
if (gw.messageAvailable()) {
message_s message = gw.getMessage();
updateDisplay(message);
}
}
void updateDisplay(message_s message){
//Serial.print("watts from gw in updatedisplay void:");
//Serial.println(watt);
// Check that message really is for display
if (message.header.messageType=M_SET_VARIABLE &&
message.header.type == V_VAR1) {
newmess = 1;
fadingok = 0;
watt = atoi(message.data);
wattcalc();
}
}
void wattcalc(){
v = watt;
int ones;
int tens;
int hundreds;
int thousands;
int tenthousands;
//Calculate digits
ones=v%10;
v=v/10;
tens=v%10;
v=v/10;
hundreds=v%10;
v=v/10;
thousands=v%10;
v=v/10;
tenthousands=v;
//Print the number digit by digit
if(watt<=999) {
//Print a blank in the tenthousand and thousand column
lc.setChar(0,4,' ',false);
lc.setChar(0,3,' ',false);
lc.setDigit(0,2,(byte)hundreds,false);
lc.setDigit(0,1,(byte)tens,false);
lc.setDigit(0,0,(byte)ones,false);
}
if(watt<=9999) {
//Print a blank in the tenthousand column
lc.setChar(0,4,' ',false);
lc.setDigit(0,3,(byte)thousands,false);
lc.setDigit(0,2,(byte)hundreds,false);
lc.setDigit(0,1,(byte)tens,false);
lc.setDigit(0,0,(byte)ones,false);
}
else {
lc.setDigit(0,4,(byte)tenthousands,false);
lc.setDigit(0,3,(byte)thousands,false);
lc.setDigit(0,2,(byte)hundreds,false);
lc.setDigit(0,1,(byte)tens,false);
lc.setDigit(0,0,(byte)ones,false);
}
delay(delaytime);
// Adjust and scale colorlevels for youre RGB-diod
if(watt >= 0 && watt <=1000){
rate = 0,4;
setColor(255, 0, 80);
}
if(watt >= 1001 && watt <=1500){
rate = 0.5;
setColor(255, 0, 160);
}
if(watt >= 1501 && watt <=2000){
rate = 0.6;
setColor(255, 0, 255);
}
if(watt >= 2001 && watt <=2500){
rate = 0.7;
setColor(160, 40, 255);
}
if(watt >= 2501 && watt <=3000){
rate = 0.8;
setColor(0, 80, 255);
}
if(watt >= 3001 && watt <=3500){
rate = 0.9;
setColor(0, 120, 255);
}
if(watt >= 3501 && watt <=4000){
rate = 1.0;
setColor(0, 180, 255);
}
if(watt >= 4001 && watt <=4500){
rate = 1.1;
setColor(0, 200, 255);
}
if(watt >= 4501 && watt <=5000){
rate = 1.2;
setColor(0, 230, 255);
if(watt >= 5001 && watt <=6000){
rate = 1.3;
setColor(0, 255, 255);
}
if(watt >= 6001 && watt <=7000){
rate = 1,4;
setColor(0, 255, 220);
}
if(watt >= 7001 && watt <=20000){
rate = 1,5;
setColor(0, 255, 200);
}
}
void setColor(int red, int green, int blue){
newRedVal = red;
newGreenVal = green;
newBlueVal = blue;
}
void Colorfade(){
if(val > 1){
loop();
}
else{
Serial.print("val < 1");
fadingok = 0;
//Red fading
if(oldRedVal< newRedVal){
redVal += 1; //if old value is less than new value, increase
delay(2);
if(redVal>= newRedVal){
redVal = newRedVal; //until it gets to new value then put it equal to new value
oldRedVal = newRedVal; //set new value to old
}
}
else if(oldRedVal > newRedVal){
redVal--; //if old value is more than new value, decrease
delay(2);
if(redVal<= newRedVal){
redVal = newRedVal; //until it gets to new value then put it equal to new value
oldRedVal = newRedVal; //set new value to old
}
}
else{
redVal = newRedVal; // if new and old are the same then fading is over
oldRedVal = newRedVal; //set new value to old
}
// Green fading
if(oldGreenVal< newGreenVal){
greenVal++; //if old value is less than new value, increase
delay(2);
if(greenVal>= newGreenVal){
greenVal = newGreenVal; //until it gets to new value then put it equal to new value
oldGreenVal = newGreenVal; //set new value to old
}
}
else if(oldGreenVal > newGreenVal){
greenVal--; //if old value is more than new value, decrease
delay(2);
if(greenVal<= newGreenVal){
greenVal = newGreenVal; //until it gets to new value then put it equal to new value
oldGreenVal = newGreenVal; //set new value to old
}
}
else{
greenVal = newGreenVal; //if new and old are the same then fading is over
oldGreenVal = newGreenVal; //set new value to old
}
//Blue fading
if(oldBlueVal< newBlueVal){
blueVal++; //if old value is less than new value, increase
delay(2);
if(blueVal>= newBlueVal){
blueVal = newBlueVal; //until it gets to new value then put it equal to new value
oldBlueVal = newBlueVal; //set new value to old
}
}
else if(oldBlueVal > newBlueVal){
blueVal--; //if old value is more than new value, decrease
delay(2);
if(blueVal<= newBlueVal){
blueVal = newBlueVal; //until it gets to new value then put it equal to new value
oldBlueVal = newBlueVal; //set new value to old
}
}
else{
blueVal = newBlueVal; //if new and old are the same then fading is over
oldBlueVal = newBlueVal; //set new value to old
}
newmess = 0;
//Serial.print("delay");
delay(10);
}
}
I hope someone find this sketch useful,and please feel free to improve