mathertel / DMXSerial

An Arduino library for sending and receiving DMX packets.
BSD 3-Clause "New" or "Revised" License
336 stars 79 forks source link

DMX sending stops after call DMXSerial.init(DMXController,Direction); twice #40

Open maik-dmxc opened 4 years ago

maik-dmxc commented 4 years ago

hi,

i've made a LED TUBE sketch, and found that if i call DMXSerial.init(DMXController,Direction); twice wenn mode is already in this mode DMXController

the Output stops! after calling DMXSerial.init(DMXController,Direction); again the third time, output starts again. everytime i call DMXSerial.init(DMXController,Direction); it alternates.

this is done in

void CASEmode() {
  switch (MODE) {
    case 0:
      strcpy_P( Mode, PSTR( "72ch"));
      strcpy_P( Function, PSTR( "Adress"));
      Channels = 216;
      pixels.setBrightness(255);
      DMXSerial.init(DMXProbe, Direction);
      break;
    case 1:
      strcpy_P( Mode, PSTR( "3ch"));
      strcpy_P( Function, PSTR( "Adress"));
      Channels = 3;
      pixels.setBrightness(255);
      DMXSerial.init(DMXProbe, Direction);
      break;
    case 2:
      strcpy_P( Mode, PSTR( "4ch"));
      strcpy_P( Function, PSTR( "Adress"));
      Channels = 4;
      DMXSerial.init(DMXProbe, Direction);
      pixels.setBrightness(0);
      break;
    case 3:
      strcpy_P( Mode, PSTR( "Rain"));
      strcpy_P( Function, PSTR( "Speed"));
      Channels = 0;
      pixels.setBrightness(255);
      DMXSerial.init(DMXController, Direction);
      break;
    case 4:
      strcpy_P( Mode, PSTR( "Cycl"));
      strcpy_P( Function, PSTR( "Speed"));
      Channels = 0;
      pixels.setBrightness(255);
      DMXSerial.init(DMXController, Direction);
      break;
    case 5:
      strcpy_P( Mode, PSTR( "chase"));
      strcpy_P( Function, PSTR( "Speed"));
      Channels = 0;
      pixels.setBrightness(255);
      DMXSerial.init(DMXController, Direction);
      break;
    case 6:
      strcpy_P( Mode, PSTR( "static"));
      strcpy_P( Function, PSTR( "Color"));
      Channels = 256; //256
      pixels.setBrightness(255);
      DMXSerial.init(DMXController, Direction);
      break;
    case 7:
      strcpy_P( Mode, PSTR( "white"));
      strcpy_P( Function, PSTR( "Value"));
      Channels = 256; //257
      DMXSerial.init(DMXController, Direction);
      break;
    case 8:
      strcpy_P( Mode, PSTR( "black"));
      strcpy_P( Function, PSTR( "Value"));
      Channels = 256; //257
      DMXSerial.init(DMXController, Direction);
      break;
  }
}

after each button press to change the values in each mode off the LEDTUBE

this only hapends if i call the mode DMXController calling DMXProbe nothing runs bad.

here is my whole code

#include <DMXSerial.h>
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <EEPROM.h>

#include <avr/pgmspace.h>

#include <Adafruit_NeoPixel.h>

#define PIN            2
#define UP             6
#define DOWN           8
#define MENU           7
#define Direction      9

#define NUMPIXELS      72
#define DMXLENGTH (NUMPIXELS*3)
int DMXSTART = 1;
int DMXMAX = 512;

int MODE = 0;
int SPEED = 0;
int BRIGHT = 0;
int COLOR = 0;
int adresse1 = 0;
int adresse2 = 2;
int adresse3 = 4;
int adresse4 = 6;
int adresse5 = 8;

#define sda 18
#define scl 19
char Mode[40];
char Function[40];
char Name[40] = "LED Tube 4";
int Channels;
int Contrast = 255;

unsigned long lastTime = 0;  // the last time a button was pressed
unsigned long Delay = 5000;    // the delay time after dimm Display
unsigned long lastDebounceTimeUP = 0;  // the last time the UP button was not pressed
unsigned long lastDebounceTimeDOWN = 0;  // the last time the DOWN button was not pressed
unsigned long debounceDelay = 2000;    // the delay increment/decrement speedup time

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ scl, /* data=*/ sda);

void setup () {
  pinMode(UP, INPUT_PULLUP);
  pinMode(DOWN, INPUT_PULLUP);
  pinMode(MENU, INPUT_PULLUP);
  //  pinMode(Direction, OUTPUT);
  strcpy_P( Mode, PSTR( "StartUP"));

  DMXSTART = (eepromReadInt(adresse1));
  if (DMXSTART < 0 ) {
    DMXSTART = 0;
  }
  MODE = (eepromReadInt(adresse2));
  if (MODE < 0 ) {
    MODE = 0;
  }
  SPEED = (eepromReadInt(adresse3));
  if (SPEED < 0 ) {
    SPEED = 0;
  }
  BRIGHT = (eepromReadInt(adresse4));
  if (BRIGHT < 0 ) {
    BRIGHT = 0;
  }
  COLOR = (eepromReadInt(adresse5));
  if (COLOR < 0 ) {
    COLOR = 0;
  }
  u8g2.begin();
  CASEmode();
  draw();

  pixels.begin(); // This initializes the NeoPixel library.
  pixels.show(); // Initialize all pixels to 'off'

}

void CASEmode() {
  switch (MODE) {
    case 0:
      strcpy_P( Mode, PSTR( "72ch"));
      strcpy_P( Function, PSTR( "Adress"));
      Channels = 216;
      pixels.setBrightness(255);
      DMXSerial.init(DMXProbe, Direction);
      break;
    case 1:
      strcpy_P( Mode, PSTR( "3ch"));
      strcpy_P( Function, PSTR( "Adress"));
      Channels = 3;
      pixels.setBrightness(255);
      DMXSerial.init(DMXProbe, Direction);
      break;
    case 2:
      strcpy_P( Mode, PSTR( "4ch"));
      strcpy_P( Function, PSTR( "Adress"));
      Channels = 4;
      DMXSerial.init(DMXProbe, Direction);
      pixels.setBrightness(0);
      break;
    case 3:
      strcpy_P( Mode, PSTR( "Rain"));
      strcpy_P( Function, PSTR( "Speed"));
      Channels = 0;
      pixels.setBrightness(255);
      DMXSerial.init(DMXController, Direction);
      break;
    case 4:
      strcpy_P( Mode, PSTR( "Cycl"));
      strcpy_P( Function, PSTR( "Speed"));
      Channels = 0;
      pixels.setBrightness(255);
      DMXSerial.init(DMXController, Direction);
      break;
    case 5:
      strcpy_P( Mode, PSTR( "chase"));
      strcpy_P( Function, PSTR( "Speed"));
      Channels = 0;
      pixels.setBrightness(255);
      DMXSerial.init(DMXController, Direction);
      break;
    case 6:
      strcpy_P( Mode, PSTR( "static"));
      strcpy_P( Function, PSTR( "Color"));
      Channels = 256; //256
      pixels.setBrightness(255);
      DMXSerial.init(DMXController, Direction);
      break;
    case 7:
      strcpy_P( Mode, PSTR( "white"));
      strcpy_P( Function, PSTR( "Value"));
      Channels = 256; //257
      DMXSerial.init(DMXController, Direction);
      break;
    case 8:
      strcpy_P( Mode, PSTR( "black"));
      strcpy_P( Function, PSTR( "Value"));
      Channels = 256; //257
      DMXSerial.init(DMXController, Direction);
      break;
  }
}

void draw() {
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(0, 20, Name);
    u8g2.drawStr(0, 40, Function);
    u8g2.setCursor(75, 40);
    switch (MODE) {
      case 0: u8g2.print(DMXSTART); break;
      case 1: u8g2.print(DMXSTART); break;
      case 2: u8g2.print(DMXSTART); break;
      case 3: u8g2.print(SPEED); break;
      case 4: u8g2.print(SPEED); break;
      case 5: u8g2.print(SPEED); break;
      case 6: u8g2.print(COLOR); break;
      case 7: u8g2.print(BRIGHT); break;
      case 8: u8g2.print(BRIGHT); break;
    }
    u8g2.drawStr(0, 60, "Mode:");
    u8g2.drawStr(65, 60, Mode);
  } while ( u8g2.nextPage() );
  //delay(1000);
}

void eepromWriteInt(int adr, uint16_t wert) {

  byte low, high;

  low = wert & 0xFF;
  high = (wert >> 8) & 0xFF;
  EEPROM.write(adr, low);
  EEPROM.write(adr + 1, high);
  return;
}

int eepromReadInt(int adr) {

  byte low, high;

  low = EEPROM.read(adr);
  high = EEPROM.read(adr + 1);
  return low + ((high << 8) & 0xFF00);
}

void setChannelRGB(int channel, int WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    /// red to green
    DMXSerial.write(channel * 3 + 1, 255 - WheelPos * 3);
    DMXSerial.write(channel * 3 + 2, 0);
    DMXSerial.write(channel * 3 + 3, WheelPos * 3);
  } else if
  (WheelPos < 170) {
    WheelPos -= 85;
    /// green to blue
    DMXSerial.write(channel * 3 + 1, 0);
    DMXSerial.write(channel * 3 + 2, WheelPos * 3);
    DMXSerial.write(channel * 3 + 3, 255 - WheelPos * 3);

  } else {
    WheelPos -= 170;
    /// red to green
    DMXSerial.write(channel * 3 + 1, WheelPos * 3);       //red
    DMXSerial.write(channel * 3 + 2, 255 - WheelPos * 3); //green
    DMXSerial.write(channel * 3 + 3, 0);                  //blue
  }

} // setChannelRGB()

void rainbow() {
  uint16_t i;
  int j;
  for (j = 0; j < 256; j++) {
    button();
    for (i = 0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, Wheel((i + j) & 255));
      setChannelRGB (i, (i + j & 255));
    }
    pixels.show();
    if (MODE != 3) break;
    delay(SPEED);
  }
}

void chase() {
  uint16_t i;
  int j;
  for (j = 0; j < 256; j++) {
    button();
    for (i = 0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, Wheel(j));
      setChannelRGB (i, j);

    }
    pixels.show();
    if (MODE != 5) break;
    delay(SPEED);
  }
}

void rainbowCycle() {
  uint16_t i;
  int j;
  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    button();
    for (i = 0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, Wheel(((i * 256 / NUMPIXELS) + j) & 255));
      setChannelRGB (i, ((i * 256 / NUMPIXELS) + j) & 255);
    }
    pixels.show();
    if (MODE != 4) break;
    delay(SPEED);
  }
}

void staticColor() {
  uint16_t i;
  for (i = 0; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, Wheel(COLOR));
    setChannelRGB (i, COLOR);
    if (MODE != 6) break;
  }
  pixels.show();
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

void button() {
  int ButtonUP = digitalRead(UP);
  int ButtonDOWN = digitalRead(DOWN);
  int ButtonMENU = digitalRead(MENU);
  //-----------------------------------------------------------------------Button UP--------------------------------------------------------------------------------------

  if (ButtonUP == LOW) {
    lastTime = millis();
    Contrast = 255;
    u8g2.setContrast(Contrast);
    switch (MODE) {
      case 0:
        if ((millis() - lastDebounceTimeUP) > debounceDelay) {

          DMXSTART = DMXSTART + 10;
          DMXMAX = 512 - Channels;
          if (DMXSTART > DMXMAX) {
            DMXSTART = 1;
          }
          draw();

        } else {
          DMXSTART++;
          DMXMAX = 512 - Channels;
          if (DMXSTART > DMXMAX) {
            DMXSTART = 1;
          }
          draw();
        }
        DMXSerial.maxChannel(DMXSTART + Channels);
        eepromWriteInt(adresse1, DMXSTART);
        delay(100);
        break;
      case 1:
        if ((millis() - lastDebounceTimeUP) > debounceDelay) {

          DMXSTART = DMXSTART + 10;
          DMXMAX = 512 - Channels;
          if (DMXSTART > DMXMAX) {
            DMXSTART = 1;
          }
          draw();

        } else {
          DMXSTART++;
          DMXMAX = 512 - Channels;
          if (DMXSTART > DMXMAX) {
            DMXSTART = 1;
          }
          draw();
        }
        DMXSerial.maxChannel(DMXSTART + Channels);
        eepromWriteInt(adresse1, DMXSTART);
        delay(100);
        break;
      case 2:
        if ((millis() - lastDebounceTimeUP) > debounceDelay) {

          DMXSTART = DMXSTART + 10;
          DMXMAX = 512 - Channels;
          if (DMXSTART > DMXMAX) {
            DMXSTART = 1;
          }
          draw();

        } else {
          DMXSTART++;
          DMXMAX = 512 - Channels;
          if (DMXSTART > DMXMAX) {
            DMXSTART = 1;
          }
          draw();
        }
        DMXSerial.maxChannel(DMXSTART + Channels);
        eepromWriteInt(adresse1, DMXSTART);
        delay(100);
        break;
      case 3:
        if ((millis() - lastDebounceTimeUP) > debounceDelay) {

          SPEED = SPEED + 10;
          if (SPEED > 256) {
            SPEED = 1;
          }
          draw();

        } else {
          SPEED++;

          if (SPEED > 256) {
            SPEED = 1;
          }
          draw();
        }

        eepromWriteInt(adresse3, SPEED);
        delay(100);
        break;
      case 4:
        if ((millis() - lastDebounceTimeUP) > debounceDelay) {

          SPEED = SPEED + 10;
          if (SPEED > 256) {
            SPEED = 1;
          }
          draw();

        } else {
          SPEED++;

          if (SPEED > 256) {
            SPEED = 1;
          }
          draw();
        }

        eepromWriteInt(adresse3, SPEED);
        delay(100);
        break;
      case 5:
        if ((millis() - lastDebounceTimeUP) > debounceDelay) {

          SPEED = SPEED + 10;
          if (SPEED > 256) {
            SPEED = 1;
          }
          draw();

        } else {
          SPEED++;

          if (SPEED > 256) {
            SPEED = 1;
          }
          draw();
        }

        eepromWriteInt(adresse3, SPEED);
        delay(100);
        break;
      case 6:
        if ((millis() - lastDebounceTimeUP) > debounceDelay) {

          COLOR = COLOR + 10;
          if (COLOR > 255) {
            COLOR = 1;
          }
          draw();

        } else {
          COLOR++;

          if (COLOR > 255) {
            COLOR = 1;
          }
          draw();
        }

        eepromWriteInt(adresse5, COLOR);
        delay(100);
        break;
      case 7:
        if ((millis() - lastDebounceTimeUP) > debounceDelay) {

          BRIGHT = BRIGHT + 10;
          if (BRIGHT > 255) {
            BRIGHT = 0;
          }
          draw();

        } else {
          BRIGHT++;

          if (BRIGHT > 255) {
            BRIGHT = 0;
          }
          draw();
        }

        eepromWriteInt(adresse4, BRIGHT);
        delay(100);
        break;
      case 8:
        if ((millis() - lastDebounceTimeUP) > debounceDelay) {

          BRIGHT = BRIGHT + 10;
          if (BRIGHT > 255) {
            BRIGHT = 0;
          }
          draw();

        } else {
          BRIGHT++;

          if (BRIGHT > 255) {
            BRIGHT = 0;
          }
          draw();
        }

        eepromWriteInt(adresse4, BRIGHT);
        delay(100);
        break;
    }
  } else {
    lastDebounceTimeUP = millis();
  }
  //-----------------------------------------------------------------------Button DOWN--------------------------------------------------------------------------------------

  if (ButtonDOWN == LOW) {
    lastTime = millis();

    Contrast = 255;
    u8g2.setContrast(Contrast);
    switch (MODE) {
      case 0:
        if ((millis() - lastDebounceTimeDOWN) > debounceDelay) {
          DMXSTART = DMXSTART - 10;
          if (DMXSTART < 1) {
            DMXSTART = DMXMAX;
          }
          draw();
        } else {
          DMXSTART--;
          if (DMXSTART < 1) {
            DMXSTART = DMXMAX;
          }

          draw();
        }

        DMXSerial.maxChannel(DMXSTART + Channels);
        eepromWriteInt(adresse1, DMXSTART);

        delay(100);
        break;
      case 1:
        if ((millis() - lastDebounceTimeDOWN) > debounceDelay) {
          DMXSTART = DMXSTART - 10;
          if (DMXSTART < 1) {
            DMXSTART = DMXMAX;
          }
          draw();
        } else {
          DMXSTART--;
          if (DMXSTART < 1) {
            DMXSTART = DMXMAX;
          }

          draw();
        }

        DMXSerial.maxChannel(DMXSTART + Channels);
        eepromWriteInt(adresse1, DMXSTART);

        delay(100);
        break;
      case 2:
        if ((millis() - lastDebounceTimeDOWN) > debounceDelay) {
          DMXSTART = DMXSTART - 10;
          if (DMXSTART < 1) {
            DMXSTART = DMXMAX;
          }
          draw();
        } else {
          DMXSTART--;
          if (DMXSTART < 1) {
            DMXSTART = DMXMAX;
          }

          draw();
        }

        DMXSerial.maxChannel(DMXSTART + Channels);
        eepromWriteInt(adresse1, DMXSTART);

        delay(100);
        break;
      case 3:
        if ((millis() - lastDebounceTimeDOWN) > debounceDelay) {
          SPEED = SPEED - 10;
          if (SPEED < 1) {
            SPEED = 256;
          }
          draw();
        } else {
          SPEED--;
          if (SPEED < 1) {
            SPEED = 256;
          }

          draw();
        }

        eepromWriteInt(adresse3, SPEED);

        delay(100);
        break;
      case 4:
        if ((millis() - lastDebounceTimeDOWN) > debounceDelay) {
          SPEED = SPEED - 10;
          if (SPEED < 1) {
            SPEED = 256;
          }
          draw();
        } else {
          SPEED--;
          if (SPEED < 1) {
            SPEED = 256;
          }

          draw();
        }

        eepromWriteInt(adresse3, SPEED);

        delay(100);
        break;
      case 5:
        if ((millis() - lastDebounceTimeDOWN) > debounceDelay) {
          SPEED = SPEED - 10;
          if (SPEED < 1) {
            SPEED = 256;
          }
          draw();
        } else {
          SPEED--;
          if (SPEED < 1) {
            SPEED = 256;
          }

          draw();
        }

        eepromWriteInt(adresse3, SPEED);

        delay(100);
        break;
      case 6:
        if ((millis() - lastDebounceTimeDOWN) > debounceDelay) {
          COLOR = COLOR - 10;
          if (COLOR < 1) {
            COLOR = 255;
          }
          draw();
        } else {
          COLOR--;
          if (COLOR < 1) {
            COLOR = 255;
          }

          draw();
        }

        eepromWriteInt(adresse5, COLOR);

        delay(100);
        break;
      case 7:
        if ((millis() - lastDebounceTimeDOWN) > debounceDelay) {
          BRIGHT = BRIGHT - 10;
          if (BRIGHT < 0) {
            BRIGHT = 255;
          }
          draw();
        } else {
          BRIGHT--;
          if (BRIGHT < 0) {
            BRIGHT = 255;
          }

          draw();
        }

        eepromWriteInt(adresse4, BRIGHT);

        delay(100);
        break;
      case 8:
        if ((millis() - lastDebounceTimeDOWN) > debounceDelay) {
          BRIGHT = BRIGHT - 10;
          if (BRIGHT < 0) {
            BRIGHT = 255;
          }
          draw();
        } else {
          BRIGHT--;
          if (BRIGHT < 0) {
            BRIGHT = 255;
          }

          draw();
        }

        eepromWriteInt(adresse4, BRIGHT);

        delay(100);
        break;
    }
  } else {
    lastDebounceTimeDOWN = millis();
  }
  //-----------------------------------------------------------------------Button MODE--------------------------------------------------------------------------------------

  if (ButtonMENU == LOW) {
    lastTime = millis();
    Contrast = 255;
    u8g2.setContrast(Contrast);
    MODE++;
    if (MODE > 8) {
      MODE = 0;
    }

    eepromWriteInt(adresse2, MODE);
    CASEmode();

    draw();

    delay(100);
  } else {}

}

void DMX72CH() {

  if (DMXSerial.receive()) {
    for (int i = 0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, pixels.Color(DMXSerial.read((i * 3) + DMXSTART), DMXSerial.read((i * 3) + DMXSTART + 1), DMXSerial.read((i * 3) + DMXSTART + 2)));
    }

    pixels.show();
  }

}

void DMX3CH() {
  if (DMXSerial.receive()) {
    for (int i = 0; i < NUMPIXELS; i++) {

      pixels.setPixelColor(i, pixels.Color(DMXSerial.read(DMXSTART), DMXSerial.read(DMXSTART + 1), DMXSerial.read(DMXSTART + 2)));
    }
    pixels.show();
  }
}

void DMX4CH() {
  if (DMXSerial.receive()) {
    for (int i = 0; i < NUMPIXELS; i++) {

      pixels.setPixelColor(i, pixels.Color(DMXSerial.read(DMXSTART), DMXSerial.read(DMXSTART + 1), DMXSerial.read(DMXSTART + 2)));
    }

    pixels.setBrightness(DMXSerial.read(DMXSTART + 3));
    pixels.show();
  }
}

void loop() {

  button();

  if      (MODE == 0) {
    DMX72CH();
  } else {
    if (MODE == 1) {
      DMX3CH();
    } else {
      if (MODE == 2) {

        DMX4CH();
      } else {
        if (MODE == 3) {

          rainbow();
        } else {
          if (MODE == 4) {
            rainbowCycle();
          } else {
            if (MODE == 5) {
              chase();
            } else {
              if (MODE == 6) {
                staticColor();
              } else {
                if (MODE == 7) {
                  pixels.setBrightness(BRIGHT);
                  for (int i = 0; i < NUMPIXELS; i++) {
                    pixels.setPixelColor(i, pixels.Color(255, 255, 255));
                    DMXSerial.write(i * 3 + 1, BRIGHT);
                    DMXSerial.write(i * 3 + 2, BRIGHT);
                    DMXSerial.write(i * 3 + 3, BRIGHT);
                  }
                  pixels.show();
                } else {
                  if (MODE == 8) {
                    pixels.setBrightness(BRIGHT);
                    for (int i = 0; i < NUMPIXELS; i++) {
                      pixels.setPixelColor(i, pixels.Color(0, 0, 0));
                      DMXSerial.write(i * 3 + 1, 0);
                      DMXSerial.write(i * 3 + 2, 0);
                      DMXSerial.write(i * 3 + 3, 0);
                    }
                    pixels.show();
                  } else {

                  }
                }
              }
            }
          }
        }
      }
    }
  }
  if ((millis() - lastTime ) > Delay && Contrast != 1) {
    Contrast = 1;
    u8g2.setContrast(Contrast);
    draw();
  }
}

regards maik

mathertel commented 4 years ago

Pleas try again with latest library,