Increments, Arrays & LEDS

Thanks for the info UKHeliBob,

That worked great.

I had a further issue with the LED's flickering like mad. But fixed it by adding a delay before the pixels.show(). This sorted the flickering issue out.

Thanks for all the info and help everyone!

I suspect that's a sticking plaster.

I'm no DMX expert (in fact I've never used it). But looking at the library then this code...

 unsigned long lastPacket = DMXSerial.noDataSince();
  if (lastPacket < 5000) {

...doesn't seem the best way to decide if new DMX data has arrived.

It will be constantly updating the leds for 5 seconds every time new data arrives. If new data arrives more frequently than every 5 seconds it will simply continually update the LEDs ad infinitum as fast as it can. That's what is causing the flicker and why you need the delay: to slow down the update rate.

My guess at a more correct way to only update ONCE when new data arrives...

if (DMXSerial.dataUpdated())
{
   DMXSerial.resetUpdated();
   // read recent DMX values 
   Ch1Value = DMXSerial.read(1);
   Ch2Value = DMXSerial.read(2);
   Ch3Value = DMXSerial.read(3);
   // etc....
}

@eddc: Perhaps you'd like to comment, having more DMX experience than me (i.e. none!) :slight_smile:

@pcbbc: That works far better than my delay. Are you sure you don't know anything about DMX?? :slight_smile:

One further thing.

I am trying to get the LED's to turn off when the DMX cable is disconnected.

I was originally doing an else statement after the lastpacket > 5000 bit.

Now this does not want to play ball.

Any ideas?

eddc:
@pcbbc: That works far better than my delay. Are you sure you don't know anything about DMX?? :slight_smile:

I know what it is and what it is for, but I’ve never used it or done anything with it.
I just read the library API description and tried to work out how you tell it has received some NEW data.
That’s just experience using libraries, not any direct DMX experience.

I was originally doing an else statement after the lastpacket > 5000 bit.
Now this does not want to play ball.

I’m not sure why.

Where did you put the clear? As an “else” to the “if” that I posted???
That won’t work. My “if” continuously checks if new data has arrived since last reset, not if data has arrived in the last 5 seconds (like your original code did). Therefore the vast majority of times it checks no new data has arrived, so the display will be cleared.

I would assume you can combine both methods and add a second if, something like...

if (DMXSerial.noDataSince() >= 5000)
{
  // Clear display here...
}

Perhaps that’s what you did try, but somehow I doubt it.

Please: Always show your new code, otherwise we’re just guessing (based on what you said you tried) about what you actually tried.

If the above doesn’t work you can create your own timer with millis() to check if 5 seconds have passed since the last dataUpdated event.

#include <DMXSerial.h>
#include <Adafruit_NeoPixel.h>

#define NUM_LEDS 20
#define DATA_PIN 12
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);



int DmxAddress=1;// SET DMX ADDRESS HERE // If DMX Address Is Set To 0 Then In Manual Mode. DMX Does Not Then Work (MANUAL MODE)

int redValue1 = 0; // value to write to the red LED
int greenValue1 = 0; // value to write to the green LED
int blueValue1 = 0; // value to write to the blue LED

int redValue2 = 0; // value to write to the red LED
int greenValue2 = 0; // value to write to the green LED
int blueValue2 = 0; // value to write to the blue LED

int redValue3 = 0; // value to write to the red LED
int greenValue3 = 0; // value to write to the green LED
int blueValue3 = 0; // value to write to the blue LED

int Ch1Value = 0;
int Ch2Value = 0;
int Ch3Value = 0;

int Ch4Value = 0;
int Ch5Value = 0;
int Ch6Value = 0;

int Ch7Value = 0;
int Ch8Value = 0;
int Ch9Value = 0;

void setup () {

  pixels.begin();
  pixels.show(); // Initialize all pixels to 'off'
  
  DMXSerial.init(DMXReceiver);
 
}


void loop() {

  if(DmxAddress!=0) {
  unsigned long lastPacket = DMXSerial.noDataSince();
  

    if (DMXSerial.dataUpdated())
{
   DMXSerial.resetUpdated();
    // read recent DMX values and set pwm levels  
    Ch1Value = DMXSerial.read(DmxAddress);
    Ch2Value = DMXSerial.read(DmxAddress+1); // Get DMX Address From int value and add one to go to next DMX address
    Ch3Value = DMXSerial.read(DmxAddress+2);
    
    Ch4Value = DMXSerial.read(DmxAddress+3);
    Ch5Value = DMXSerial.read(DmxAddress+4);
    Ch6Value = DMXSerial.read(DmxAddress+5);

    Ch7Value = DMXSerial.read(DmxAddress+6);
    Ch8Value = DMXSerial.read(DmxAddress+7);
    Ch9Value = DMXSerial.read(DmxAddress+8);
    


  redValue1 = map(Ch1Value, 0, 1023, 0, 255);
  greenValue1 = map(Ch2Value, 0, 1023, 0, 255);
  blueValue1 = map(Ch3Value, 0, 1023, 0, 255);;

  redValue2 = map(Ch4Value, 0, 1023, 0, 255);
  greenValue2 = map(Ch5Value, 0, 1023, 0, 255);
  blueValue2 = map(Ch6Value, 0, 1023, 0, 255);;

  redValue3 = map(Ch7Value, 0, 1023, 0, 255);
  greenValue3 = map(Ch8Value, 0, 1023, 0, 255);
  blueValue3 = map(Ch9Value, 0, 1023, 0, 255);;
 // }
  for (int group1 = 0; group1 < NUM_LEDS; group1 += 3)
  {
    pixels.setPixelColor(group1, pixels.Color(redValue1, greenValue1, blueValue1));
//delay(10);  
  }
  for (int group2 = 1; group2 < NUM_LEDS; group2 += 3)
  {
    pixels.setPixelColor(group2, pixels.Color(redValue2, greenValue2, blueValue2));
//delay(10);  
  }
  for (int group3 = 2; group3 < NUM_LEDS; group3 += 3)
  {
    pixels.setPixelColor(group3, pixels.Color(redValue3, greenValue3, blueValue3));
   // delay(10);  
  }
//delay(5);  
pixels.show();
  } 
  if (DMXSerial.noDataSince() >= 5000)
{
    {
    for( int i = 0; i<NUM_LEDS; i++){
    pixels.setPixelColor(i, LOW);
   } 
    pixels.show();
}

    
  }
  }


// End.

This is the current code

With the new snippet to see if DMX has been received recently, it not makes the LED's flicker again.

eddc:

#include <DMXSerial.h>

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define NUM_LEDS 20
#define DATA_PIN 12
#define BRIGHTNESS 100
LiquidCrystal_I2C lcd(0x27,16,2);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, NEO_GRBW + NEO_KHZ800);

//int clothtype = 0; // RGB cloth = 0 ---- White cloth = 1 ---- RGBW cloth = 2

int DmxAddress=1;// SET DMX ADDRESS HERE // If DMX Address Is Set To 0 Then In Manual Mode. DMX Does Not Then Work (MANUAL MODE)

int redValue1 = 0; // value to write to the red LED
int greenValue1 = 0; // value to write to the green LED
int blueValue1 = 0; // value to write to the blue LED
int whiteValue1 = 0; // value to write to the white LED

int redValue2 = 0; // value to write to the red LED
int greenValue2 = 0; // value to write to the green LED
int blueValue2 = 0; // value to write to the blue LED
int whiteValue2 = 0; // value to write to the white LED

int redValue3 = 0; // value to write to the red LED
int greenValue3 = 0; // value to write to the green LED
int blueValue3 = 0; // value to write to the blue LED
int whiteValue3 = 0; // value to write to the white LED

int Ch1Value = 0; // DMX Channel
int Ch2Value = 0; // DMX Channel
int Ch3Value = 0; // DMX Channel
int Ch4Value = 0; // DMX Channel

int Ch5Value = 0; // DMX Channel
int Ch6Value = 0; // DMX Channel
int Ch7Value = 0; // DMX Channel
int Ch8Value = 0; // DMX Channel

int Ch9Value = 0; // DMX Channel
int Ch10Value = 0; // DMX Channel
int Ch11Value = 0; // DMX Channel
int Ch12Value = 0; // DMX Channel

void setup () {
  pixels.setBrightness(BRIGHTNESS);
  pixels.begin();
  pixels.show(); // Initialize all pixels to 'off'
 
  DMXSerial.init(DMXReceiver);

lcd.begin();                      // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1,0);
  lcd.print("Test");
  lcd.setCursor(4,1);
  lcd.print("Screen");
 
}

void loop() {

if(DmxAddress!=0) {
  unsigned long lastPacket = DMXSerial.noDataSince();

if (DMXSerial.dataUpdated())
{
  DMXSerial.resetUpdated();
    // read recent DMX values and set pwm levels 
    Ch1Value = DMXSerial.read(DmxAddress);
    Ch2Value = DMXSerial.read(DmxAddress+1); // Get DMX Address From int value and add one to go to next DMX address
    Ch3Value = DMXSerial.read(DmxAddress+2);
    Ch4Value = DMXSerial.read(DmxAddress+3);
   
    Ch5Value = DMXSerial.read(DmxAddress+4);
    Ch6Value = DMXSerial.read(DmxAddress+5);
    Ch7Value = DMXSerial.read(DmxAddress+6);
    Ch8Value = DMXSerial.read(DmxAddress+7);
   
    Ch9Value = DMXSerial.read(DmxAddress+8);
    Ch10Value = DMXSerial.read(DmxAddress+9);
    Ch11Value = DMXSerial.read(DmxAddress+10);
    Ch12Value = DMXSerial.read(DmxAddress+11);

// if (clothtype == 0)
// {
  redValue1 = map(Ch1Value, 0, 1023, 0, 255);
  greenValue1 = map(Ch2Value, 0, 1023, 0, 255);
  blueValue1 = map(Ch3Value, 0, 1023, 0, 255);;
  whiteValue1 = map(Ch4Value, 0, 1023, 0, 255);
 
 
  redValue2 = map(Ch5Value, 0, 1023, 0, 255);
  greenValue2 = map(Ch6Value, 0, 1023, 0, 255);;
  blueValue2 = map(Ch7Value, 0, 1023, 0, 255);
  whiteValue2 = map(Ch8Value, 0, 1023, 0, 255);

redValue3 = map(Ch9Value, 0, 1023, 0, 255);;
  greenValue3 = map(Ch10Value, 0, 1023, 0, 255);;
  blueValue3 = map(Ch11Value, 0, 1023, 0, 255);;
  whiteValue3 = map(Ch12Value, 0, 1023, 0, 255);;

// }
  for (int group1 = 0; group1 < NUM_LEDS; group1 += 3)
  {
    pixels.setPixelColor(group1, pixels.Color(redValue1, greenValue1, blueValue1, whiteValue1));
//delay(10); 
  }
  for (int group2 = 1; group2 < NUM_LEDS; group2 += 3)
  {
    pixels.setPixelColor(group2, pixels.Color(redValue2, greenValue2, blueValue2, whiteValue2));
//delay(10); 
  }
  for (int group3 = 2; group3 < NUM_LEDS; group3 += 3)
  {
    pixels.setPixelColor(group3, pixels.Color(redValue3, greenValue3, blueValue3, whiteValue3));
  // delay(10); 
  }
//delay(5); 
pixels.show();
  }
  //if (DMXSerial.noDataSince() >= 5000)
//{
  //  {
  // for( int i = 0; i<NUM_LEDS; i++){
  // pixels.setPixelColor(i, LOW);
  // }
  //  pixels.show();
//}

//else
    // Turn off LED's, when no data was received since 5 seconds or more.
  // {
  //  for( int i = 0; i<NUM_LEDS; i++){
  //  pixels.setPixelColor(i, LOW);
  // }
    //  pixels.show();
    //}
   
  }
  }

// End.




This is the current code

With the new snippet to see if DMX has been received recently, it not makes the LED's flicker again.

I have been playing with this. Now I have added White function of the SK6812 to this project I have run into another issue. When I use a DMX controller to turn on the white led's it makes all the other leds (RGB) flash.

Any ideas on this.

Maybe it is just me but I don’t see the white in your code!

pmagowan:
Maybe it is just me but I don’t see the white in your code!

My apologies, i have amended my previous post