|
| 1 | +// SPDX-FileCopyrightText: 2024 John Park for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +// Use a DMX controller to drive NeoPixel strips |
| 6 | +// Arduino Uno or Metro 328 + Conceptinetics DMX Shield |
| 7 | +// Recieves incoming DMX messages from controller, translates to NeoPixel |
| 8 | + |
| 9 | + |
| 10 | +#include <Conceptinetics.h> |
| 11 | +#include <Adafruit_NeoPixel.h> |
| 12 | + |
| 13 | +// User adjust these for the number of strips, pins, pixels per strip, and color formats: |
| 14 | +#define NUM_STRIPS 3 |
| 15 | +const int pin_for_strip[] = {A0, A1, A2}; |
| 16 | +const int leds_per_strip[] = {30, 20, 30}; |
| 17 | +const neoPixelType format_per_strip[] = {NEO_GRB + NEO_KHZ800, NEO_BGR + NEO_KHZ800, NEO_GRB + NEO_KHZ800 }; |
| 18 | + |
| 19 | +#define CH_PER_STRIP 16 // only 4 used per strip, but 16 is nicer on UI of controllers that have 16 channel/page |
| 20 | +#define DMX_CHANNELS (CH_PER_STRIP * NUM_STRIPS) |
| 21 | + |
| 22 | +const int ledPin = 13; |
| 23 | + |
| 24 | +Adafruit_NeoPixel *strips[NUM_STRIPS]; |
| 25 | + |
| 26 | +DMX_Slave dmx_slave ( DMX_CHANNELS ); // Configure a DMX receiving controller |
| 27 | +uint16_t channelValues[DMX_CHANNELS]; // Array to store DMX values |
| 28 | + |
| 29 | + |
| 30 | +void setup() { |
| 31 | + // Set led pin as output pin |
| 32 | + pinMode ( ledPin, OUTPUT ); |
| 33 | + digitalWrite(ledPin, HIGH); |
| 34 | + |
| 35 | + // set up strips |
| 36 | + for(int i=0; i< NUM_STRIPS; i++) { |
| 37 | + int pin = pin_for_strip[i]; |
| 38 | + int num_leds = leds_per_strip[i]; |
| 39 | + int format = format_per_strip[i]; |
| 40 | + Adafruit_NeoPixel *strip = new Adafruit_NeoPixel(num_leds, pin, format); |
| 41 | + strips[i] = strip; |
| 42 | + strips[i]->begin(); |
| 43 | + } |
| 44 | + // light up all the strips RGB to test |
| 45 | + for(int i=0; i< NUM_STRIPS; i++) { |
| 46 | + strips[i]->fill(0xff0000); |
| 47 | + strips[i]->show(); |
| 48 | + delay(1000); |
| 49 | + strips[i]->fill(0x00ff00); |
| 50 | + strips[i]->show(); |
| 51 | + delay(1000); |
| 52 | + strips[i]->fill(0x0000ff); |
| 53 | + strips[i]->show(); |
| 54 | + } |
| 55 | + delay(1000); |
| 56 | + |
| 57 | + // Enable DMX receiving interface and start receiving DMX data |
| 58 | + dmx_slave.enable (); |
| 59 | + dmx_slave.setStartAddress (1); |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +void loop() |
| 64 | +{ |
| 65 | + // Fetch all DMX channel values into the array |
| 66 | + for (int i = 0; i < DMX_CHANNELS; i++) { |
| 67 | + channelValues[i] = dmx_slave.getChannelValue(i + 1); // Get values starting from channel 1 |
| 68 | + } |
| 69 | + |
| 70 | + for(int i=0; i<NUM_STRIPS; i++){ |
| 71 | + //remap some channels for specific value ranges |
| 72 | + uint16_t strip_hue1 = map(channelValues[0 + (i * CH_PER_STRIP)], 0, 255, 0, 65535); |
| 73 | + uint16_t strip_hue2 = map(channelValues[4 + (i * CH_PER_STRIP)], 0, 255, 0, 65535); |
| 74 | + uint16_t strip_pix1 = map(channelValues[3 + (i * CH_PER_STRIP)], 0, 255, 0, leds_per_strip[i] - 1); |
| 75 | + uint16_t strip_pix2 = map(channelValues[7 + (i * CH_PER_STRIP)], 0, 255, 0, leds_per_strip[i] - 1); |
| 76 | + |
| 77 | + strips[i]->fill(0x000000); |
| 78 | + strips[i]->setPixelColor(strip_pix1, strips[i]->ColorHSV(strip_hue1, channelValues[1+(i*CH_PER_STRIP)], channelValues[2+(i*CH_PER_STRIP)])); //first pixel |
| 79 | + strips[i]->setPixelColor(strip_pix2, strips[i]->ColorHSV(strip_hue2, channelValues[5+(i*CH_PER_STRIP)], channelValues[6+(i*CH_PER_STRIP)])); //last pixel |
| 80 | + // all the pixels in between |
| 81 | + for (int j = strip_pix1; j <= strip_pix2; j++) { |
| 82 | + float fraction = float(j - strip_pix1) / float(strip_pix2 - strip_pix1); // Calculate the fraction of the interpolation (0 to 1) |
| 83 | + // Interpolate HSV components (Hue, Saturation, Value) |
| 84 | + uint16_t interpolated_hue = int(lerp(strip_hue1, strip_hue2, fraction)) % 65536; // Wrap around Hue |
| 85 | + uint16_t interpolated_saturation = lerp(channelValues[1+(i*CH_PER_STRIP)], channelValues[5+(i*CH_PER_STRIP)], fraction); |
| 86 | + uint16_t interpolated_value = lerp(channelValues[2+(i*CH_PER_STRIP)], channelValues[6+(i*CH_PER_STRIP)], fraction); |
| 87 | + |
| 88 | + // Set the interpolated color to the pixel |
| 89 | + strips[i]->setPixelColor(j, strips[i]->ColorHSV(interpolated_hue, interpolated_saturation, interpolated_value)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + for(int i=0; i<NUM_STRIPS; i++){ |
| 94 | + strips[i]->show(); |
| 95 | + } |
| 96 | + |
| 97 | + delay(100); |
| 98 | +} |
| 99 | + |
| 100 | +// Linear interpolation function |
| 101 | +float lerp(float start, float end, float t) { |
| 102 | + return start + (end - start) * t; |
| 103 | +} |
0 commit comments