hobbyquaker / artnet

Send ArtDMX to an Art-Net node (DMX512, Stage Lighting) 💡🎬
MIT License
123 stars 23 forks source link

Wait Timers? #9

Closed TheFonix closed 7 years ago

TheFonix commented 7 years ago

Is there a way to make "waits" inside this art net system? I've tried many different ways but not had any luck, or is there some kind of fading system that i can use? I currently have an Amazon Echo "Get"ing PHP pages which trigger the execution of the node which then runs the Art net Script and that then gets sent to Open Lighting architecture which of course then changes the different lights according to the data sent to them.

The whole system works perfectly fine when im just sending one insistence of a lighting change, such as:

var options = {
    host: 'ola'
};
var artnet = require('artnet')(options);

artnet.set(1, null, null, 10, null, null, 10, function (err, res) {
    artnet.close();
});

But when i add more of them to attempt to run them as a sequence nothing happens it just chooses the first one. I've tried many different timer for NPM but none of them seem to work like i would like, my end goal is something along these lines ( Sorry about the poor example i cant think of any other way to do it)

var options = {
    host: 'ola'
};
var artnet = require('artnet')(options);

artnet.set(1, null, null, 10, null, null, 10, function (err, res) {
    artnet.close();
});
//10%
artnet.set(1, null, null, 50, null, null, 50, function (err, res) {
    artnet.close();
});
//30%
artnet.set(1, null, null, 100, null, null, 100, function (err, res) {
    artnet.close();
});
//55%
artnet.set(1, null, null, 150, null, null, 150, function (err, res) {
    artnet.close();
});
//70%
artnet.set(1, null, null, 200, null, null, 200, function (err, res) {
    artnet.close();
});
//85%
artnet.set(1, null, null, 250, null, null, 250, function (err, res) {
    artnet.close();
});
//95%
artnet.set(1, null, null, 255, null, null, 255, function (err, res) {
    artnet.close();
});
//100%

Thanks, Hope you can help!

hobbyquaker commented 7 years ago

You should not close the artnet connection and you need to use Javascript setTimeout or setInterval function. Here a short (untested) example:

var options = {
    host: 'ola'
};
var artnet = require('artnet')(options);

/**
 * Fade from 0 to 255 in given ms
 */
function fadeUp(ms) {
  var step = 5;
  var time = Math.floor(ms / (255 / step));
  var val = 0;
  var interval = setInterval(function () {
    artnet.set(1, null, null, val, null, null, val);
    val += step;
    if (val > 255) {
      clearInterval(interval);
      artnet.close();
    }
  }, time);
}

fadeUp(2000); 

this is just a quick example, untested, possibly full of bugs ;-) if you want faster fades you should increase step from 5 to a higher value because you should not send to Artnet in shorter Intervals than 25ms.

Another approach would be https://github.com/hobbyquaker/mqtt-dmx-sequencer. With this tool (that also uses https://github.com/hobbyquaker/artnet under the hood) you can define Scenes and combine Scenes to a Sequence. Triggering Scenes and starting/stopping Sequences can then be done via MQTT. You could use https://github.com/node-red to send the commands via MQTT triggered by your Echo. (see https://nathan.chantrell.net/20160328/amazon-echo-alexa-skills-kit-integration-with-node-red/)

HTH

TheFonix commented 7 years ago

Awesome! Thanks man, ill check all these options out, always helpful being pointed in the right direction! :+1: