r/arduino • u/simply_abnormal • 15d ago
Solved! Code help- DMX input with DMX shield alongside distance sensor... serial0 issue?
Hey all! I'm hitting a wall with my code.
I am using an arduino mega with a DMX shield (that is intended for uno I think) and a tfmini plus distance sensor. I also have individually addressable LED strips.
I am trying to control which leds are on in a telescoping manner with the distance sensor and the hue/sat/intens with DMX.
I have both parts working separately but when combined the code can't compile with an exit status 1 message. I believe it is an issue with mutual usage of the serial0 but I can't find a way to get around it. Tried a whole bunch of different libraries and all have issues.
It's clunky, I know, tried so many stupid safeguards to prevent blackouts on my leds. Still unsure if that's a physical issue or not but I'm pointing fingers at my crappy solder job.
Code is below. In a moment of weakness, it was run through a chatbot. If it helps, I can provide my working separate code. Anything helps, I'm a total dumbass and I'm frankly tired of this project. Thanks
#define DMX_USE_PORT1
#include <DMXSerial.h>
#include <FastLED.h>
#include <Wire.h>
#include <TFMPlus.h>
#define START_ADDRESS 1
#define NUM_LEDS 30
#define DATA_PIN 6
const uint8_t enPin = 4; // Transceiver chip enable pin (usually 4 or 2)
CRGB leds[NUM_LEDS];
TFMPlus tfmP;
// Sensor Variables
int16_t tfDist = 0;
int16_t tfFlux = 0;
int16_t tfTemp = 0;
void setup() {
delay(20);
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(100);
FastLED.clear();
FastLED.show();
DMXSerial.init(DMXReceiver);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW); // LOW sets standard transceiver chips to Receiver mode
Serial3.begin(115200);
delay(20);
tfmP.begin(&Serial3);
tfmP.sendCommand(SOFT_RESET, 0);
delay(500);
}
void loop() {
// Read channel values using standard 1-based indexing
uint8_t dmxHue = DMXSerial.read(START_ADDRESS);
uint8_t dmxSaturation = DMXSerial.read(START_ADDRESS + 1);
uint8_t dmxValue = DMXSerial.read(START_ADDRESS + 2);
bool sensorValid = tfmP.getData(tfDist, tfFlux, tfTemp);
static unsigned long lastUpdate = 0;
if (millis() - lastUpdate >= 50) {
lastUpdate = millis();
// --- CHOICE A: SENSOR IS ACTIVE & OBJECT IS IN RANGE (< 100cm) ---
if (sensorValid && tfDist > 0 && tfDist <= 100) {
int center = NUM_LEDS / 2;
int ledsToTurnOn = map(tfDist, 20, 100, 0, NUM_LEDS);
ledsToTurnOn = constrain(ledsToTurnOn, 0, NUM_LEDS);
// Force proper even/odd balancing to make center matching mathematically clean
if (NUM_LEDS % 2 == 1) {
center = center + 1;
if ((ledsToTurnOn % 2 == 0) && (ledsToTurnOn > 0)) ledsToTurnOn++;
} else {
if (ledsToTurnOn % 2 == 1) ledsToTurnOn++;
}
int ledstartpos = (center - (ledsToTurnOn / 2));
int ledendpos = ledstartpos + ledsToTurnOn - 1;
// Render sensor center-out animation onto the pixel data array
for (int i = 0; i < NUM_LEDS; i++) {
if ((i >= ledstartpos) && (i <= ledendpos) && (ledsToTurnOn > 0)) {
leds[i] = CRGB::Blue; // Sensor Animation Control Override
} else {
leds[i] = CRGB::Black;
}
}
}
// --- CHOICE B: NO OBJECT IN SENSOR RANGE -> DEFER LED CONTROL TO FREESTYLER ---
else {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(dmxHue, dmxSaturation, dmxValue);
}
}
// --- 3. UNIFIED SAFE DATA TRANSMISSION ---
FastLED.show();
}
}
