help with code plea...
 
Notifications
Clear all

help with code please

27 Posts
8 Users
0 Reactions
9,308 Views
Posts: 13
Topic starter
(@mennis22)
Active Member
Joined: 12 years ago
[#35076]

hey guys,

sorry for possibly sounding dumb. I'm only 19 and taught this to myself so I'm by no means a professional and cannot find a solution to a problem I've had for years. My problem is that I am using Vixen with Arduino, and it runs for a while, but eventually the music and lights get off sync. I need to go to my computer when this happens, shut down the show, restart the Arduino, and then restart the show for it to work again. After much research, I've determined that I need a header and footer in my code to keep itself in check. My show uses 60 channels. I have looked at others' coding and cannot understand it or even adapt it to work for me. Any help with helping me figure out how to successfully use a header/footer in my sketch and keeping my show running would be greatly appreciated. Attached is my code. Thanks!


int Chan0 = 0;  
int Chan1 = 1;  
int Chan2 = 2;  
int Chan3 = 3;  
int Chan4 = 4;  
int Chan5 = 5;  
int Chan6 = 6;  
int Chan7 = 7;  
int Chan8 = 8;  
int Chan9 = 9; 
int Chan10 = 10;  
int Chan11 = 11;  
int Chan12 = 12;  
int Chan13 = 13;  
int Chan14 = 14;  
int Chan15 = 15;  
int Chan16 = 16;  
int Chan17 = 17; 
int Chan18 = 18;  
int Chan19 = 19;  
int Chan20 = 20;  
int Chan21 = 21;  
int Chan22 = 22;  
int Chan23 = 23;  
int Chan24 = 24;  
int Chan25 = 25; 
int Chan26 = 26;  
int Chan27 = 27;  
int Chan28 = 28;  
int Chan29 = 29;  
int Chan30 = 30;  
int Chan31 = 31;  
int Chan32 = 32;  
int Chan33 = 33; 
int Chan34 = 34;  
int Chan35 = 35;  
int Chan36 = 36;  
int Chan37 = 37;  
int Chan38 = 38;  
int Chan39 = 39;  
int Chan40 = 40;  
int Chan41 = 41;
int Chan42 = 42;  
int Chan43 = 43;  
int Chan44 = 44;  
int Chan45 = 45;  
int Chan46 = 46;  
int Chan47 = 47;  
int Chan48 = 48;  
int Chan49 = 49; 
int Chan50 = 50;  
int Chan51 = 51;  
int Chan52 = 52;  
int Chan53 = 53;  
int Chan54 = 54;  
int Chan55 = 55;  
int Chan56 = 56;  
int Chan57 = 57; 
int Chan58 = 58;  
int Chan59 = 59;  
int Chan60 = 60;  
int Chan61 = 61;  
int Chan62 = 62;  
int Chan63 = 63;  


#include <Wire.h>
#include <Centipede.h>;

Centipede CS;



int idx = 0;     
int incomingByte[60];   

void setup()
{
  Wire.begin();
  CS.initialize();
  Serial.begin(9600);     

  CS.pinMode(Chan0, OUTPUT);   
  CS.pinMode(Chan1, OUTPUT);
  CS.pinMode(Chan2, OUTPUT);
  CS.pinMode(Chan3, OUTPUT);
  CS.pinMode(Chan4, OUTPUT);
  CS.pinMode(Chan5, OUTPUT);   
  CS.pinMode(Chan6, OUTPUT);
  CS.pinMode(Chan7, OUTPUT);
  CS.pinMode(Chan8, OUTPUT);
  CS.pinMode(Chan9, OUTPUT);
  CS.pinMode(Chan10, OUTPUT);   
  CS.pinMode(Chan11, OUTPUT);
  CS.pinMode(Chan12, OUTPUT);
  CS.pinMode(Chan13, OUTPUT);
  CS.pinMode(Chan14, OUTPUT);   
  CS.pinMode(Chan15, OUTPUT);
  CS.pinMode(Chan16, OUTPUT);
  CS.pinMode(Chan17, OUTPUT);
  CS.pinMode(Chan18, OUTPUT);
  CS.pinMode(Chan19, OUTPUT);
  CS.pinMode(Chan20, OUTPUT);   
  CS.pinMode(Chan21, OUTPUT);
  CS.pinMode(Chan22, OUTPUT);
  CS.pinMode(Chan23, OUTPUT);
  CS.pinMode(Chan24, OUTPUT);   
  CS.pinMode(Chan25, OUTPUT);
  CS.pinMode(Chan26, OUTPUT);
  CS.pinMode(Chan27, OUTPUT);
  CS.pinMode(Chan28, OUTPUT);
  CS.pinMode(Chan29, OUTPUT);
  CS.pinMode(Chan30, OUTPUT);   
  CS.pinMode(Chan31, OUTPUT);
  CS.pinMode(Chan32, OUTPUT);
  CS.pinMode(Chan33, OUTPUT);
  CS.pinMode(Chan34, OUTPUT);   
  CS.pinMode(Chan35, OUTPUT);
  CS.pinMode(Chan36, OUTPUT);
  CS.pinMode(Chan37, OUTPUT);
  CS.pinMode(Chan38, OUTPUT);
  CS.pinMode(Chan39, OUTPUT);
  CS.pinMode(Chan40, OUTPUT);   
  CS.pinMode(Chan41, OUTPUT);
  CS.pinMode(Chan42, OUTPUT);
  CS.pinMode(Chan43, OUTPUT);
  CS.pinMode(Chan44, OUTPUT);   
  CS.pinMode(Chan45, OUTPUT);
  CS.pinMode(Chan46, OUTPUT);
  CS.pinMode(Chan47, OUTPUT);
  CS.pinMode(Chan48, OUTPUT);
  CS.pinMode(Chan49, OUTPUT);
  CS.pinMode(Chan50, OUTPUT);   
  CS.pinMode(Chan51, OUTPUT);
  CS.pinMode(Chan52, OUTPUT);
  CS.pinMode(Chan53, OUTPUT);
  CS.pinMode(Chan54, OUTPUT);   
  CS.pinMode(Chan55, OUTPUT);
  CS.pinMode(Chan56, OUTPUT);
  CS.pinMode(Chan57, OUTPUT);
  CS.pinMode(Chan58, OUTPUT);
  CS.pinMode(Chan59, OUTPUT);

  
}

void loop()
{  

   if (Serial.available() >= 60) {
    for (int idx=0; idx<61; idx++) {
    
      incomingByte[idx] = Serial.read();
    }
    
    CS.digitalWrite(Chan0, incomingByte[0]);   
    CS.digitalWrite(Chan1, incomingByte[1]);   
    CS.digitalWrite(Chan2, incomingByte[2]);   
    CS.digitalWrite(Chan3, incomingByte[3]);   
    CS.digitalWrite(Chan4, incomingByte[4]);   
    CS.digitalWrite(Chan5, incomingByte[5]);   
    CS.digitalWrite(Chan6, incomingByte[6]);   
    CS.digitalWrite(Chan7, incomingByte[7]);   
    CS.digitalWrite(Chan8, incomingByte[8]);   
    CS.digitalWrite(Chan9, incomingByte[9]); 
    CS.digitalWrite(Chan10, incomingByte[10]);   
    CS.digitalWrite(Chan11, incomingByte[11]);   
    CS.digitalWrite(Chan12, incomingByte[12]);   
    CS.digitalWrite(Chan13, incomingByte[13]);
    CS.digitalWrite(Chan14, incomingByte[14]);   
    CS.digitalWrite(Chan15, incomingByte[15]);   
    CS.digitalWrite(Chan16, incomingByte[16]);   
    CS.digitalWrite(Chan17, incomingByte[17]);
    CS.digitalWrite(Chan18, incomingByte[18]);   
    CS.digitalWrite(Chan19, incomingByte[19]);
    CS.digitalWrite(Chan20, incomingByte[20]);   
    CS.digitalWrite(Chan21, incomingByte[21]);   
    CS.digitalWrite(Chan22, incomingByte[22]);   
    CS.digitalWrite(Chan23, incomingByte[23]);
    CS.digitalWrite(Chan24, incomingByte[24]);   
    CS.digitalWrite(Chan25, incomingByte[25]);   
    CS.digitalWrite(Chan26, incomingByte[26]);   
    CS.digitalWrite(Chan27, incomingByte[27]);
    CS.digitalWrite(Chan28, incomingByte[28]);   
    CS.digitalWrite(Chan29, incomingByte[29]);
    CS.digitalWrite(Chan30, incomingByte[30]);   
    CS.digitalWrite(Chan31, incomingByte[31]);   
    CS.digitalWrite(Chan32, incomingByte[32]);   
    CS.digitalWrite(Chan33, incomingByte[33]);
    CS.digitalWrite(Chan34, incomingByte[34]);   
    CS.digitalWrite(Chan35, incomingByte[35]);   
    CS.digitalWrite(Chan36, incomingByte[36]);   
    CS.digitalWrite(Chan37, incomingByte[37]);
    CS.digitalWrite(Chan38, incomingByte[38]);   
    CS.digitalWrite(Chan39, incomingByte[39]);
    CS.digitalWrite(Chan40, incomingByte[40]);   
    CS.digitalWrite(Chan41, incomingByte[41]);   
    CS.digitalWrite(Chan42, incomingByte[42]);   
    CS.digitalWrite(Chan43, incomingByte[43]);
    CS.digitalWrite(Chan44, incomingByte[44]);   
    CS.digitalWrite(Chan45, incomingByte[45]);   
    CS.digitalWrite(Chan46, incomingByte[46]);   
    CS.digitalWrite(Chan47, incomingByte[47]);
    CS.digitalWrite(Chan48, incomingByte[48]);   
    CS.digitalWrite(Chan49, incomingByte[49]);
    CS.digitalWrite(Chan50, incomingByte[50]);   
    CS.digitalWrite(Chan51, incomingByte[51]);   
    CS.digitalWrite(Chan52, incomingByte[52]);   
    CS.digitalWrite(Chan53, incomingByte[53]);
    CS.digitalWrite(Chan54, incomingByte[54]);   
    CS.digitalWrite(Chan55, incomingByte[55]);   
    CS.digitalWrite(Chan56, incomingByte[56]);   
    CS.digitalWrite(Chan57, incomingByte[57]);
    CS.digitalWrite(Chan58, incomingByte[58]);   
    CS.digitalWrite(Chan59, incomingByte[59]);
   }
}


also, I just realized now that I should use things like loops to be more efficient...


26 Replies
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
mennis22 wrote:... My problem is that I am using Vixen with Arduino, and it runs for a while, but eventually the music and lights get off sync.

Well, assuming that Vixen actually sends 60 channels;
1) you are reading 61 (i.e. 0 to 60).
2) It takes 62.5ms to send 60 channels worth, so whatever your sequence is asking it should not try to sequence any faster, or increase the baud rate.
3) Another issue is that I don't know if the Arduino can process 60 channels worth so it would need to be verified that it's not losing sync because of buffer overrun.


Reply
Posts: 3650
(@jhinkle)
Famed Member
Joined: 15 years ago

As LightUp has stated -- your for loop is not indexed properly.

It should be:

for (int idx=0; idx<60; idx++)

You are actualy corrupting memory that is positioned just after your array "incomingByte" -- because it is 60 in size and you are writing 61 locations.

A few other comments ....

You are probably getting "byte" size data from Vixen yet your array incomingByte is defined as int --- so I suspect it is 16 bits in size.

That is wasting 60 bytes of ram.

Also -- you define:

int Chan0 = 0;
int Chan1 = 1;
.....
int Chan62 = 62;
int Chan63 = 63;

That is 126 bytes of ram that is wasted and slowing your loop execution time down .... I'll explain.

The instruction that use Chan0 has to use a memory fetch instruction to fulfill the "c" instruction. THAT requires more code space and cpu cycles.

If you simply replaced them with an "immediate" value -- code will shrink and it will execute faster.

May I suggest you look into the use of an "enum" to accomplish --- example follows

enum
{
Chan0 = 0,
Chan1,
Chan2,
..... keep going until,
Chan59,
Chan60
};

You compiler will replace the label Chanxx with its value --- thereby using an immediate instruction -- reducing code size and making it execute faster.

Your baud rate in 9600 --- that mean 9600 bits per second.

So -- one bit takes .0001041 seconds.

Most serial communication protocols take a start and a stop bit --- making each byte that Vixen sends requires 10 bits --- or .001041 seconds per byte.

Since your packet is 60 bytes in length --- just the communication time to send the 60 bytes is 62.46 millisecs.

You might want to time an execution loop for the 60 bytes to make sure you can actually process that in time.

I suggest you look at a faster baud rate --- especially if your Vixen's time tic is around 50 millisecs.

Hope that helps.

Joe


Reply
Posts: 3650
(@jhinkle)
Famed Member
Joined: 15 years ago

To answer your "Header" question ... THAT requires a change to the Vixen driver --- can you do that yourself or has someone already done that?

What you really want is a number of bytes (at least two) whose values will NEVER appear in the data stream.

Look at the Renard data stream ...

Lets just say that somehow you can guarantee that the values 0xa5 0x5a will NEVER appear in your Vixen data steam in THAT order AND you can make those two values be transmitted at the start of every packet transmitted by Vixen.

Your loop processing has to change --- because would would then POLL for those two value in the serial steam ... upon finding them --- you would know the next 60 bytes were Vixen data.

As you can see --- this requirees a change to the Vixen driver and to the way you process you incoming data.

Joe


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
JHinkle wrote:...What you really want is a number of bytes (at least two) whose values will NEVER appear in the data stream.....

I was taught to never say NEVER. 🙂

I sometimes wonder in all the datastreams (without header/footer) that Vixen spews out how long it would take to form a recognizable 2 or 3 letter word like NO, YES, TRY, WHY, 5a, a5, ... as headers?

Since Vixen sends a byte for light values, it is statistically possible to have any value from 0 to 255. Therefore we don't have an algorithm to guarantee that two or more certain bytes will never occur consecutively. From probability theory, the more characters used for header/footer the less likely the occurrence of a match in the datastream, and a match is something our light displays can easily tolerate on a few occasion during the night.

I'm not aware of anyone here who actually programs a Vixen sequence to prevent certain consecutive bytes from occurring in their sequence.

Edit: The code in the post #1 uses digitalWrite. Hence almost any header, barring 0 and 255, would work.


Reply
Posts: 3650
(@jhinkle)
Famed Member
Joined: 15 years ago

The User does not program to "not" have a specific value -- the output driver does.

Instead of two bytes being used as the "start of packet" identifier ... lets use one.

You have a choice of going to the TOP of the 255 window or the bottom (0) -- depending on how your controller uses the values to determine intensity.

Let's assume that 255 means full bright.

I would recommend designing at the top of the illumination window --- you then get auto "off" for your lights until the controller sees the "start of packet" identifier.

You would modify your Vixen driver to watch for a User illumination value of 253 (as an example -- 0xfd). When the driver saw a value of User Illumination intensity value of 253 (0xfd) - it would modify it and actually send 254 (0xfe) instead -- thereby removing the possibility of the value 253 ever showing as an illumination value.

Then all the driver has to do to send 60 bytes to a "253" smart controller would be to first send the value of 253 (our "start of packet" identifier) and then 60 bytes of user illumination data with the 253 replacement process as defined above.

You then modify your controller to POLL for the 253 value in the Vixen stream -- when it sees a 253 -- the controller knows the next 60 bytes is illumination data. Auto "off" is a side benefit of the process.

The User does not program, or hope that statistically, the number (253 in this case) does not appear in the data stream --- it's the output driver that performs that task --- which is why I asked if Vixen already had a similar driver or the young man had the capability to make/modify a Vixen driver to accomplish what he asked for -- "Adding a Header".

Joe


Reply
Posts: 3650
(@jhinkle)
Famed Member
Joined: 15 years ago

The process above removes a single value from the illumination data stream and uses it as a "start of packet" identifier.

You loose one illumination value -- but it is simple and easy.

You can implement a more complex process -- as Renard requires.

Renard uses values 0x7f, 0x7e, and 0x7d as stream identifiers.

The stream driver (Vixen) watches for these values and substitutes a pair of values instead of a single value. The Renard controller also watches for the "control pair" and replaces them with a single value thereby returning the data stream to the exact User value.

The process is more complicated and results in a variable length message packet --- but if you can not sacrifice a data value -- as defined above -- then an approach similar to what the Renard controller implements is a good choice.

I have provided a snippet of HLS code that implements the Renard protocol ... It shows the Renard "start of packet" header and data recovery pairs used by a Renard.

Please use for educational purposes ... Renard also requires padding --- so that requirement is also shown.

I hope this helps.

Joe


    // fill output buffer

    OutPutIdx = 0;
    RenardPad = 0;

    XU[i].OutPutData[OutPutIdx] = 0x7e;    // renard header
    OutPutIdx++;
    RenardPad++;

    XU[i].OutPutData[OutPutIdx] = 0x80;
    OutPutIdx++;
    RenardPad++;


    for(k = 0; k < XU[i].h_NumChannelsToWork; k++)
    {
            vv = (word)(.5 + XU[i].ChanArray[k]->h_Output[j] * (float)bFF / 100.0);

            switch (vv)
            {
            case 0x7f:
                XU[i].OutPutData[OutPutIdx] = 0x7f;
                OutPutIdx++;
                RenardPad++;
                XU[i].OutPutData[OutPutIdx] = 0x31;
                break;

            case 0x7e:
                XU[i].OutPutData[OutPutIdx] = 0x7f;
                OutPutIdx++;
                RenardPad++;
                XU[i].OutPutData[OutPutIdx] = 0x30;
                break;

            case 0x7d:
                XU[i].OutPutData[OutPutIdx] = 0x7f;
                OutPutIdx++;
                RenardPad++;
                XU[i].OutPutData[OutPutIdx] = 0x2f;
                break;

            default:
                XU[i].OutPutData[OutPutIdx] = (byte)vv;
                break;
            }

            OutPutIdx++;
            RenardPad++;

            if(RenardPad >= 100)
            {
                RenardPad = 0;
                XU[i].OutPutData[OutPutIdx] = 0x7d;
                OutPutIdx++;
            }
    }


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
JHinkle wrote:...The User does not program, or hope that statistically, the number (253 in this case) does not appear in the data stream --- it's the output driver that performs that task ---Joe

Oh,... I get it now. I didn't realise what the Renard plugin was doing. I hadn't seriously looked at the Renard coding as I never used a Renard. I should read that Renard WIKI.


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
JHinkle wrote:The process above removes a single value from the illumination data stream and uses it as a "start of packet" identifier.

You loose one illumination value -- but it is simple and easy.

You can implement a more complex process -- as Renard requires.

Renard uses values 0x7f, 0x7e, and 0x7d as stream identifiers.

So this may be a case where the "benefit out weighs" the cost of extra processing time.


Reply
Posts: 13
Topic starter
(@mennis22)
Active Member
Joined: 12 years ago

hey guys, thanks so much for the help! I haven't had much time to look into it yet (as you know, this time of the year is crazy for everyone), but I hope to in the next few days and get it to work. I didn't realize making a header was quite that complicated, and I also didn't even notice those simple embarrassing little mistakes you guys pointed out in my code :blush2: I will hopefully be working on it soon and letting you guys know the results.


Reply
Page 1 / 3
Share: