
The current code I'm using to convert serial commands to DMX signals.
Compiled to an Arduino with Grove DMX adapter.
// this sketch waits for serial inputs of the format "123,456x".
// where 123 is the channel, 456 is the value/brightness and "x" is the end command (initiating the DMX-write).
// "," can be replaced by any non numerical character.
#include <DmxSimple.h>
int channel, value, maxchannel = 24;
int myTargets[26]; //including an extra one to ease the channel allocation
int myColours[26];
void setup() {
// initialize serial:
Serial.begin(9600);
channel = 0;
value = 0;
// initialize and reset all dmx channels
maxchannel = 24;
myColours[0] = 0;
myTargets[0] = 0;
DmxSimple.usePin(3);
DmxSimple.maxChannel(maxchannel);
Serial.setTimeout(2);
for(int i = 1; i <= maxchannel; i++){
myColours[i] = 0;
myTargets[i] = 0;
DmxSimple.write(i,0);
Serial.print(i);
Serial.print(",");
Serial.print(myTargets[i]);
Serial.print(",");
Serial.println(myColours[i]);
}
}
void loop() {
// if there's any serial available, read it:
delay(2);
while (Serial.available() > 0) {
//Serial.println("serial reception");
// look for the next valid integer in the incoming serial stream:
int channel = Serial.parseInt();
int value = Serial.parseInt();
// look for the 'x'. That's the end of your sentence:
if (Serial.read() == 120) {
// constrain the values
channel = constrain(channel, 0, maxchannel);
myTargets[channel] = constrain(value, 0, 255);
}
}
// move all values 1 step closer to the target
for(int i = 1; i <= maxchannel; i++) {
if (myColours[i] != myTargets[i]) {
//Serial.print(i);
//Serial.print(",");
//Serial.print(myTargets[i]);
//Serial.print(",");
//Serial.println(myColours[i]);
if (myColours[i] < myTargets[i]) {
myColours[i] = myColours[i] + 1;
}
else {
myColours[i] = myColours[i] - 1;
}
myColours[i] = constrain(myColours[i], 0, 255);
DmxSimple.write(i,myColours[i]);
}
}
}