ArtnetNeoPixelRGBW.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. This example will receive multiple universes via Artnet and control a strip of sk6812RGBW leds via
  3. Adafruit's NeoPixel library: https://github.com/adafruit/Adafruit_NeoPixel
  4. This example may be copied under the terms of the MIT license, see the LICENSE file for details
  5. */
  6. #include <Artnet.h>
  7. #include <Ethernet.h>
  8. #include <EthernetUdp.h>
  9. #include <SPI.h>
  10. #include <Adafruit_NeoPixel.h>
  11. // Neopixel settings
  12. const int numLeds = 120; // change for your setup
  13. const int channelsPerLed = 4;
  14. const int numberOfChannels = numLeds * channelsPerLed; // Total number of channels you want to receive (1 led = 4 channels)
  15. const byte dataPin = 2;
  16. Adafruit_NeoPixel leds = Adafruit_NeoPixel(numLeds, dataPin, NEO_GRBW + NEO_KHZ800);
  17. // Artnet settings
  18. Artnet artnet;
  19. const int startUniverse = 0; // CHANGE FOR YOUR SETUP most software this is 1, some software send out artnet first universe as 0.
  20. // Check if we got all universes
  21. const int maxUniverses = numberOfChannels / 512 + ((numberOfChannels % 512) ? 1 : 0);
  22. bool universesReceived[maxUniverses];
  23. bool sendFrame = 1;
  24. int previousDataLength = 0;
  25. // Change ip and mac address for your setup
  26. byte ip[] = {192, 168, 2, 2};
  27. byte mac[] = {0x04, 0xE9, 0xE5, 0x00, 0x69, 0xEC};
  28. void setup()
  29. {
  30. //Serial.begin(115200);
  31. artnet.begin(mac, ip);
  32. leds.begin();
  33. initTest();
  34. // this will be called for each packet received
  35. artnet.setArtDmxCallback(onDmxFrame);
  36. }
  37. void loop()
  38. {
  39. // we call the read function inside the loop
  40. artnet.read();
  41. }
  42. void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data, IPAddress remoteIP)
  43. {
  44. sendFrame = 1;
  45. // set brightness of the whole strip
  46. if (universe == 15)
  47. {
  48. leds.setBrightness(data[0]);
  49. leds.show();
  50. }
  51. // Store which universe has got in
  52. if ((universe - startUniverse) < maxUniverses)
  53. universesReceived[universe - startUniverse] = 1;
  54. for (int i = 0 ; i < maxUniverses ; i++)
  55. {
  56. if (universesReceived[i] == 0)
  57. {
  58. //Serial.println("Broke");
  59. sendFrame = 0;
  60. break;
  61. }
  62. }
  63. // read universe and put into the right part of the display buffer
  64. for (int i = 0; i < length / channelsPerLed; i++)
  65. {
  66. int led = i + (universe - startUniverse) * (previousDataLength / channelsPerLed);
  67. if (led < numLeds) {
  68. if (channelsPerLed == 4)
  69. leds.setPixelColor(led, data[i * channelsPerLed], data[i * channelsPerLed + 1], data[i * channelsPerLed + 2], data[i * channelsPerLed + 3]);
  70. if (channelsPerLed == 3)
  71. leds.setPixelColor(led, data[i * channelsPerLed], data[i * channelsPerLed + 1], data[i * channelsPerLed + 2]);
  72. }
  73. }
  74. previousDataLength = length;
  75. if (sendFrame)
  76. {
  77. leds.show();
  78. // Reset universeReceived to 0
  79. memset(universesReceived, 0, maxUniverses);
  80. }
  81. }
  82. void initTest()
  83. {
  84. for (int i = 0 ; i < numLeds ; i++)
  85. leds.setPixelColor(i, 127, 0, 0, 0);
  86. leds.show();
  87. delay(500);
  88. for (int i = 0 ; i < numLeds ; i++)
  89. leds.setPixelColor(i, 0, 127, 0, 0);
  90. leds.show();
  91. delay(500);
  92. for (int i = 0 ; i < numLeds ; i++)
  93. leds.setPixelColor(i, 0, 0, 127, 0);
  94. leds.show();
  95. delay(500);
  96. for (int i = 0 ; i < numLeds ; i++)
  97. leds.setPixelColor(i, 0, 0, 0, 127);
  98. leds.show();
  99. delay(500);
  100. for (int i = 0 ; i < numLeds ; i++)
  101. leds.setPixelColor(i, 0, 0, 0, 0);
  102. leds.show();
  103. }