I am trying to control some rgb lights from software that has the ability to send dmx via usb. I do not want to actually use any dmx lights/cords, just the messages from the software set to control one rgb light, so three channels. I am hoping to use a nano and some mosfets to drive the rgb strip. I am able set the color from the serial monitor but must adapt my code to read the dmx messages correctly. Here is my code:
char buffer[16]; //maximum expected length
int REDPin = 3 ; // RED pin of the LED to PWM pin 4
int GREENPin = 9; // GREEN pin of the LED to PWM pin 5
int BLUEPin =5;
int len = 0;
void setup()
{
pinMode(REDPin, OUTPUT);
pinMode(GREENPin, OUTPUT);
pinMode(BLUEPin, OUTPUT);
Serial.begin(115200);
}
void loop()
{
if (Serial.available() > 0)
{
int incomingByte = Serial.read();
buffer[len++] = incomingByte;
//
// check for overflow
//
if (len >= 16)
{
// overflow, resetting
len = 0;
}
//
// check for newline (end of message)
//
if (incomingByte == '\n')
{
int r, g, b;
int n = sscanf(buffer, "%d %d %d", &r, &g, &b);
if (n == 3)
{
analogWrite(REDPin, r);
analogWrite(GREENPin, g);
analogWrite(BLUEPin, b);
Serial.println(r);
Serial.println(g);
Serial.println(b);
// valid message received, use values here..
}
else
{
// parsing error, reject
}
len = 0; // reset buffer counter
}
}
}
I am not sure about the formatting or reading of dmx messages ( I was hoping they would just be 3 values 0-255). I used serial port monitor to check traffic my results are below. The first image is of successful messages to the lights via serial monitor. The second is the messages from the dmx software. You can see the successes have rgb values in the chars column, while the failures seem to have some other format. The three bytes in the data column makes me thing its reading the channel amount correctly, but I am not sure how to use these values to drive the pwm pins.
I could also use some clarity on the difference between data and data(char) in this context. I'm also not entirely clear on bytes vs char and DEC vs hex vs ASCII and conversion which may be the issue here?
Thanks :o