mathertel / DMXSerial

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

DMXSerial.noDataSince() not behaving as expected. #76

Open bill-i-am-code opened 1 year ago

bill-i-am-code commented 1 year ago

I have been looking at this library to provide an interface between 2 channels of DMX and I2C. I have written the very simple sketch as below, based on the example.

//#include <Wire.h>

int dmxAddress[] = {112, 113  }; //DMX Address of the 2 channels to be monitored
byte dmxValues[2]; // Levels associated with the 2 channels
byte hiByte[2]; // used for reading DMX address from I2c
byte loByte[2]; // used for reading DMX address from I2c

const int Pin1 = 9;     // PWM output 1
const int Pin2 = 6;     // PWM output 2
const int FlagPin = 17; // Low when DMX is present.

void setup()
{
  DMXSerial.init(DMXReceiver);
//  Wire.begin(2);
//  Wire.onRequest(requestEvent);

//  Wire.onReceive(receiveEvent);

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(FlagPin, OUTPUT);

  // set some default values
  digitalWrite(FlagPin, 1);
  digitalWrite(LED_BUILTIN, HIGH);
}

void loop()
{
  // Calculate how long no data bucket was received
  unsigned long lastPacket = DMXSerial.noDataSince();
  if (lastPacket < 5000)
  {
    // read recent DMX values
    digitalWrite(FlagPin, 0);
    //digitalWrite(LED_BUILTIN, 1);
    dmxValues[0] = DMXSerial.read(dmxAddress[0]);
    dmxValues[1] = DMXSerial.read(dmxAddress[1]);
    analogWrite(Pin1, dmxValues[0]);
    analogWrite(Pin2, dmxValues[1]);
  }
  else
  {
    analogWrite(Pin1, 0);
    analogWrite(Pin2, 0);
    digitalWrite(LED_BUILTIN, 0);
    digitalWrite(FlagPin, 1);
  } // if
}
//void requestEvent()
//{
//  Wire.write(dmxValues[0]);
//  Wire.write(dmxValues[1]);
//}
//void receiveEvent(int howMany)
//{
//  while (Wire.available())
//  {
//    hiByte[0] = Wire.read();
//    loByte[0] = Wire.read();
//    hiByte[1] = Wire.read();
//    loByte[1] = Wire.read();
//    dmxAddress[0] = (hiByte[0] << 8) + loByte[0];
//    dmxAddress[1] = (hiByte[1] << 8) + loByte[1];
//  }
//}

I noticed that FlagPin will go high after some time even when valid DMX data seemed to be received. So I commented out the I2C part of the code. I have ran the above code on an Arduino Nano and an Arduino Nano Every. In both cases the else part of the if statement in loop() is called when I think valid data is being received. Please see attached video. LED_BUILTIN goes off after a few seconds, where the 2 LEDs keep varying in level showing DMX being received.

This is not the behaviour I was expecting, but I may have misunderstood the library. Any help received with thanks. IMG_8373.zip