mathertel / DMXSerial

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

flips out after 3 channels #79

Open tonyxforce opened 1 year ago

tonyxforce commented 1 year ago

I have 2 servos and 4 LEDs attached to pins 3, 5, 6, 9, 10, 11 and the 2 servos work fine with the first 2 channels, and the first led works perfectly, but when i try to bring up the brightness of the second led (channel 4) the servos start moving randomly in random directions and the led doesn't light up, the third led just doesn't light up at all, the fourth led(channel 6) lights up perfectly.

code:

#include <Servo.h>
#include <DMXSerial.h>

Servo pan;
Servo tilt;

const int panPin = 3;
const int tiltPin = 5;
const int RedPin =    6;  // PWM output pin for Red Light.
const int GreenPin =  9;  // PWM output pin for Green Light.
const int BluePin =   10;  // PWM output pin for Blue Light.
const int WhitePin = 11;

int panPos = 0;
int tiltPos = 0;

int lastPanPos = 0;
int lastTiltPos = 0;

const int startChannel = 0 * 6 + 1;

#define RedDefaultLevel   128
#define GreenDefaultLevel 128
#define BlueDefaultLevel  128
#define WhiteDefaultLevel 128

void setup() {
  DMXSerial.init(DMXReceiver);

  // set some default values
  DMXSerial.write(1, 0);
  DMXSerial.write(2, 0);
  DMXSerial.write(3, 80);
  DMXSerial.write(4, 0);
  DMXSerial.write(5, 0);
  DMXSerial.write(6, 0);

  pan.attach(panPin);
  tilt.attach(tiltPin);
  pinMode(RedPin,   OUTPUT); 
  pinMode(GreenPin, OUTPUT);
  pinMode(BluePin,  OUTPUT);
  pinMode(WhitePin, OUTPUT);

}

void loop() {

  if (lastPanPos != panPos) {
    lastPanPos = panPos;
    pan.write(panPos);
  };

  if (lastTiltPos != tiltPos) {
    lastTiltPos = tiltPos;
    tilt.write(tiltPos);
  };

  unsigned long lastPacket = DMXSerial.noDataSince();

  if (lastPacket < 5000) {
    // read recent DMX values and set pwm levels
    pan.write(            DMXSerial.read(startChannel + 0));
    tilt.write(           DMXSerial.read(startChannel + 1));
    analogWrite(RedPin,   DMXSerial.read(startChannel + 2));
    analogWrite(GreenPin, DMXSerial.read(startChannel + 3));
    analogWrite(BluePin,  DMXSerial.read(startChannel + 4));
    analogWrite(WhitePin, DMXSerial.read(startChannel + 5));

  } else {
    // Show pure red color, when no data was received since 5 seconds or more.
    panPos = 0;
    tiltPos = 0;
    analogWrite(RedPin,   RedDefaultLevel);
    analogWrite(GreenPin, GreenDefaultLevel);
    analogWrite(BluePin,  BlueDefaultLevel);
    analogWrite(WhitePin, WhiteDefaultLevel);

  }
}

edit: formatting