Hi everyone,
I am receiving dmx with the DMXSerial.h library and a max 485 dmx shield on one Arduino and passing it thru via I2C to another Arduino, but every now and then my code misinterprets the signal.
For example: my lighting desk is only sending Channel 10 at Value 255, but sometimes it fails to read that signal propperly and thinks I am sending a Value of 255 on Channel 7, 8 or 9.
Please let me know if you know what I am doing wrong!
Arduino that receives the DMX:
#include <Wire.h>
#include <DMXSerial.h>
bool Setup;
byte delayTime = 1;
int FaderChannel;
byte Universe32[32];
byte FullUniverse[512];
void setup() {
// put your setup code here, to run once:
Wire.begin(8); // join i2c bus with address #8
// Serial.begin(9600);
Wire.setClock(250000);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
DMXSerial.init(DMXReceiver); // initialize DMX receiver in manual mode
}
void receiveEvent() { // Checks if Setup Status
while (Wire.available()) {
Setup = Wire.read();
// Serial.println(Setup);
}
}
void requestEvent() { // Sends requested Data depending on Setup
if (Setup == 1) { // Returns 1 Byte for Fader Channel
for (int i = 0; i <= 511; i++) {
if (FullUniverse[i] != DMXSerial.read(i + 1)) { // Checks if different than last time
FaderChannel = i + 1;
}
FullUniverse[i] = DMXSerial.read(i + 1);
}
Wire.write(FaderChannel);
}
else { // Returns 32 Bytes for Universe32
for (byte i = 0; i <= 31; i++) {
Wire.write(DMXSerial.read(i + 1));
// Serial.println(Universe32[i]);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
// for (byte i = 0; i <= 31; i++) {
// Universe32[i] = DMXSerial.read(i + 1);
// delay(1);
}
}
Arduino that receives the dmx data via I2C and Serial.prints the received data:
#include <Wire.h>
byte S9S = 9;// Setup Switch
byte S10S = 10; // Setup Switch
byte S11S = 11; // Rel Abs Switch
byte S12S = 12;// Rel Abs Switch
bool S9state = 0;
bool S10state = 0;
bool S11state = 0;
bool S12state = 0;
bool Setup = 0;
byte delayTime = 1;
int FaderChannel;
byte requBytes;
byte Universe32[32];
void setup() {
// put your setup code here, to run once:
Wire.begin(9); // join i2c bus with address #9
Serial.begin(250000); // Unit 2 need 57600 not 2000000
Wire.setClock(250000);
pinMode(S9S, INPUT_PULLUP);
pinMode(S10S, INPUT_PULLUP);
pinMode(S11S, INPUT_PULLUP);
pinMode(S12S, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
S9state = digitalRead(S9S);
S10state = digitalRead(S10S);
S11state = digitalRead(S11S);
S12state = digitalRead(S12S);
if (S9state == LOW) { // Setup Active
Setup = 1;
requBytes = 1;
}
else { // Setup Inactive
Setup = 0;
requBytes = 32;
}
Wire.beginTransmission(8); // Sends Status to Receiver
Wire.write(Setup);
Wire.endTransmission();
delay(delayTime);
// Serial.println(Setup);
Wire.requestFrom(8, requBytes, true); // Requests 1 or 32 Bytes
while (Wire.available()) {
if (Setup == 1) { // Requests 1 Byte for FaderChannel
FaderChannel = Wire.read();
if (FaderChannel != 0) {
Serial.println(FaderChannel);
}
}
else { // Requests 32 Bytes for Universe32
for (byte i = 0; i <= 31; i++) {
Universe32[i] = Wire.read();
if (Universe32[i] != 0) {
Serial.print(i+1);
Serial.print(": ");
Serial.println(Universe32[i]);
// delay(20);
}
}
}
}
}
Thanks for your help.
All the best,
Leon
