Skip to content

Instantly share code, notes, and snippets.

@ollpu
Created January 17, 2015 20:17
Show Gist options
  • Select an option

  • Save ollpu/239cc50beb91bfe06189 to your computer and use it in GitHub Desktop.

Select an option

Save ollpu/239cc50beb91bfe06189 to your computer and use it in GitHub Desktop.
Processing > Arduino DMX Protocol
//DMX communication protocol for Processing ---> Arduino
//Made by Roope Salmi
void setup() {
Serial.begin(115200);
}
byte message[3];
byte pointer = 0;
void loop() {
if(Serial.available() > 0) {
if(Serial.peek() >= 250) {
pointer = 0;
parseMessage(message);
}
message[pointer] = Serial.read();
pointer++;
}
}
void parseMessage(byte* mesg) {
switch(mesg[0]) {
case 250:
execute(mesg[1]+1, mesg[2]);
break;
case 251:
execute(mesg[1]+1, mesg[2] + 250);
break;
case 252:
execute(mesg[1]+1 + 250, mesg[2]);
break;
case 253:
execute(mesg[1]+1 + 250, mesg[2] + 250);
break;
case 254:
execute(mesg[1]+1 + 262, mesg[2]);
break;
case 255:
execute(mesg[1]+1 + 262, mesg[2] + 250);
break;
}
}
void execute(int channel, int data) {
Serial.write(channel); //This line of code is unnecessary
//Now you can do whatever you like with this data
}
//DMX communication protocol for Processing ---> Arduino
//Made by Roope Salmi
import processing.serial.*;
Serial arduino;
void setup() {
println(Serial.list());
arduino = new Serial(this, Serial.list()[0], 115200);
delay(500);
arduino.write((byte)i);
}
void draw() {
for(int i = 1; i < 60; i++) {
int data = int(100);
sendDMXmessage(i, data);
println(arduino.read());
}
}
//DMX message to arduino:
//ByI|Name |Description
//1: Header (message type): >= 250 (in more detail below)
//2: Channel 0-249
//3: Data 0-249
//Don't worry, no data is lost by limiting the values to 249. The header also contains information about both of these values.
void sendDMXmessage(int chan, int val) {
byte[] message = new byte[3];
if(chan-1 < 250) {
if(val < 250) {
//Channel and value below 250
message[0] = (byte)250;
message[1] = (byte)(chan-1);
message[2] = (byte)val;
} else {
//Only channel below 250
message[0] = (byte)251;
message[1] = (byte)(chan-1);
message[2] = (byte)(val - 250);
}
} else {
if(chan-1 < 262) {
if(val < 250) {
//Channel below 256 and value below 250
message[0] = (byte)252;
message[1] = (byte)(chan-1 - 250);
message[2] = (byte)val;
} else {
//Channel below 256 and value above 250
message[0] = (byte)253;
message[1] = (byte)(chan-1 - 250);
message[2] = (byte)(val - 250);
}
} else {
if(val < 250) {
//Channel above 262 and value below 250
message[0] = (byte)254;
message[1] = (byte)(chan-1 - 262);
message[2] = (byte)val;
} else {
//Channel above 262 and value above 250
message[0] = (byte)255;
message[1] = (byte)(chan-1 - 262);
message[2] = (byte)(val - 250);
}
}
}
arduino.write(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment