DMX output giving errors

Hello,

First of all, this is my second arduino project. It is coded with a lot of help of ChatGPT, so it certainly isnt the cleanest code but it does the job...sort of.

this code is made for a DMX joystick followspot remote.
averything seems to work but the dmx output gives errors.
When connected to a tester, i get correct values but an error indication.
When connected to a moving head, i see data coming in but the head does not accept te data to move.

When I tried the example code from DMXSerial, same results.
So, the code doesn't seem to be the problem

I used a genuine arduino micro fed for testing by USB
For dmx I used Groves dmx shield but didn't connect the N/C pin.
VCC to 5V of the board
Ground to Ground
Signal to TX on the board.

Has anyone an idea what could be the problem?



#include <DMXSerial.h>



// Define operational mode 
bool debugMode = false;

// Define input pins
const int joystickX = A0;
const int joystickY = A1;
const int joystickZ = A2;
const int sensitivityPot = A3;
const int potA = A4;
const int potB = A5;
const int potC = A6;
const int potD = A7;
const int potE = A8;
const int buttonPins[8] = {12, 11, 10, 9, 7, 5, 3, 16};
const int shiftButtonPin = 13;


// Joystick and sensitivity settings
const int joystickCenter = 512;
const int deadzone = 50;
float sensitivityFactor = 2;

// DMX output channels (assuming 16-bit, split into coarse and fine)
const int dmxChannelXCoarse = 1;
const int dmxChannelXFine = 2;
const int dmxChannelYCoarse = 3;
const int dmxChannelYFine = 4;
const int dmxChannelZCoarse = 5;
const int dmxChannelZFine = 6;
const int dmxChannelA = 7;
const int dmxChannelB = 8;
const int dmxChannelC = 9;
const int dmxChannelD = 10;
const int dmxChannelE = 11;

// Storage for the joystick output values
unsigned int outputX = 32768;
unsigned int outputY = 32768;
unsigned int outputZ = 32768;

// Button state management
bool shiftActive = false;
unsigned long buttonPressTimes[8] = {0};
bool buttonStates[8] = {false};

// Structure for storing XYZ values
struct XYZValues {
  unsigned int x, y, z;
};

// Storage for XYZ values for buttons 1-16
XYZValues storedValues[16];




void setup() {
  Serial.begin(250000);
  DMXSerial.init(DMXController);

   // Set pin 2 as an output
  pinMode(2, OUTPUT);
  
  // Set pin 2 HIGH to enable transmission mode on the RS-485 transceiver
  digitalWrite(2, HIGH);

  // Initialize button input pins
  for (int i = 0; i < 8; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
  pinMode(shiftButtonPin, INPUT_PULLUP);

  // Initialize stored XYZ values to 128 (in 16-bit format, this is 128 << 8)
  for (int i = 0; i < 16; i++) {
    storedValues[i].x = 32768; // 128 << 8
    storedValues[i].y = 32768; // 128 << 8
    storedValues[i].z = 32768; // 128 << 8
  }

  if (debugMode) {
    Serial.println("Debug Mode: Active");
  } else {
    Serial.println("Operational Mode: Active");
  }
}

void updateOutputValue(unsigned int &outputValue, int joystickValue) {
  int distance = joystickValue - joystickCenter;
  if (abs(distance) < deadzone) {
    distance = 0;
  }

  int sensitivityReading = constrain(analogRead(sensitivityPot), 2, 1024);
  float adjustedSensitivity = sensitivityFactor * (sensitivityReading / 1024.0f);
  
  float change = adjustedSensitivity * distance * 2;
  long tempOutput = outputValue + change;

  if (tempOutput > 65535) {
    outputValue = 65535;
  } else if (tempOutput < 0) {
    outputValue = 0;
  } else {
    outputValue = tempOutput;
  }
}

void checkButtons() {
  shiftActive = !digitalRead(shiftButtonPin);

  for (int i = 0; i < 8; i++) {
    bool currentButtonState = !digitalRead(buttonPins[i]);
    if (currentButtonState && !buttonStates[i]) {
      buttonPressTimes[i] = millis();
    } else if (!currentButtonState && buttonStates[i]) {
      unsigned long pressDuration = millis() - buttonPressTimes[i];
      int index = shiftActive ? i + 8 : i;

      if (pressDuration >= 2000) {
        storedValues[index] = {outputX, outputY, outputZ};
        if (debugMode) Serial.println("Stored: Button " + String(index + 1));
      } else if (pressDuration <= 1000) {
        outputX = storedValues[index].x;
        outputY = storedValues[index].y;
        outputZ = storedValues[index].z;
        if (debugMode) Serial.println("Recalled: Button " + String(index + 1));
      }
    }
    buttonStates[i] = currentButtonState;
  }
}

void loop() {
  updateOutputValue(outputX, analogRead(joystickX));
  updateOutputValue(outputY, analogRead(joystickY));
  updateOutputValue(outputZ, analogRead(joystickZ));

  checkButtons();

    // Read and process additional potentiometers
  int valueA = analogRead(potA) / 4; // Mapping 0-1023 to 0-255
  int valueB = analogRead(potB) / 4;
  int valueC = analogRead(potC) / 4;
  int valueD = analogRead(potD) / 4;
  int valueE = analogRead(potE) / 4;


  if (!debugMode) {
    DMXSerial.write(dmxChannelXCoarse, (outputX >> 8) & 0xFF);
    DMXSerial.write(dmxChannelXFine, outputX & 0xFF);
   // DMXSerial.write(dmxChannelYCoarse, (outputY >> 8) & 0xFF);
    //DMXSerial.write(dmxChannelYFine, outputY & 0xFF);
    //DMXSerial.write(dmxChannelZCoarse, (outputZ >> 8) & 0xFF);
    //DMXSerial.write(dmxChannelZFine, outputZ & 0xFF);
   // DMXSerial.write(dmxChannelA, valueA);
   // DMXSerial.write(dmxChannelB, valueB);
   // DMXSerial.write(dmxChannelC, valueC);
    //DMXSerial.write(dmxChannelD, valueD);
    //DMXSerial.write(dmxChannelE, valueE);

  } else {
    
      //Serial.print(" X Coarse: "); Serial.print((outputX >> 8) & 0xFF );
      //Serial.print(", Fine: "); Serial.print(outputX & 0xFF );
      //Serial.print(" Y Coarse: "); Serial.print((outputY >> 8) & 0xFF );
      //Serial.print(", Fine: "); Serial.print(outputY & 0xFF );
      //Serial.print(" Z Coarse: "); Serial.print((outputZ >> 8) & 0xFF );
      //Serial.print(", Fine: "); Serial.println(outputZ & 0xFF );
      //Serial.print("A: "); Serial.print(valueA);
      //Serial.print("B: "); Serial.print(valueB);
      //Serial.print("C: "); Serial.print(valueC);
      //Serial.print("D: "); Serial.print(valueD);
      //Serial.print("E: "); Serial.println(valueE);
     
      
    }
  

  delay(10); // Short delay for loop stability and responsiveness
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.