Here is a better explanation. I am basing everything off of the information here: Arduino Playground - Protokoll
I'm trying to read the first 9 bytes of the stream with this code. I'm only going to use bytes 2-9 because the first byte of every DMX stream is a start byte and it contains a value of 0.
while(Serial.available() > 0)
{
byteRead = Serial.read();
if(byteRead >= 0)
{
numBytesRead++;
if(numBytesRead <= 9)
{
interestingBytes[numBytesRead] = byteRead;
}
}
}
The code is saying that while serial data is being received, record the data to byteRead. If byteRead is greater then or equal to 0 then increase numBytesRead by one. If the numBytesRead is less then 9 then record the serial data from byteRead to interestingBytes[1-9]. Each interestingByte should contain a value for a channel.
I'm not sure on how to handle the remaining 500 or so bytes in the stream. My thought is just to ignore them but I'm not sure if that works.
The next section of code is designed to detect the break between the stream. It's a bunch of bits with the value of 0 for approximately 88 milliseconds.
/* detecting break between packets */
if (bitRead(byteRead,1)) // if a one is detected, we're not in a break, reset zerocounter.
{
zerocounter = 0;
}
if(bitRead(byteRead,0)) //if the bit is a 0 then we are in a break
{
zerocounter++; // increment zerocounter
}
if (zerocounter >= 22) // if 22 0's are received in a row (88uS break)
{
numBytesRead = 0; //Set number of bytes read back to 0 so we can read the first 9 bytes of the new packet
}
This code is reading each bit to see if it is a 0 or a 1. Each time a 1 is received it resets zerocounter but if a 0 is received it increments zerocounter by 1. When we receive 22 0's in a row then there is a break and we reset numBytesRead back to 0 to start reading the first 9 bytes from the stream again.
The final section is attempting to read the value of the byte. In the DMX program I can set the value of a channel which corresponds to a byte to any number between 0 and 255. I'm not sure if you can simply check to see if a variable is equal to a number like I am doing.
/* Reading Byte data to control pins */
if(interestingBytes[2] == '255')
{
digitalWrite(relay2, HIGH); //if byte2 value = 255 set pin 2 to high
}
else if(interestingBytes[2] == '0') //if byte2 value = 0 set pin 2 to low
{
digitalWrite(relay2, LOW);
}
When I have the DMX program running I set channel 1 to a value of 255 which should correspond to the second byte of each packet/stream since byte 1 is the start byte. However, when I change the value nothing happens.
I hope that is a better explanation and any help would be great.