serial communication problem

hello all! I have an arduino uno, and have been using it to sync Christmas lights to music using serial communication. I built a box with transistors and relays and many other components. it worked great for the first three nights, but on the fourth night I encountered a problem.

The problem was that the arduino was not outputing the correct outputs, and the lights got off of the music. The show was running fine for a few hours, but then all of a sudden it just got off. I am not great at programming, so im hoping someone may just find a stupid error in my coding.

I was just confused since it was working fine for hours, and then all of a sudden stopped working correctly.

here’s my code:

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 i = 0;     // Loop counter
int incomingByte[14];   // store the 14 values from the serial port


void setup()
{
  Serial.begin(9600);        // set up Serial at 9600 bps

  pinMode(Chan0, OUTPUT);   // pins as output
  pinMode(Chan1, OUTPUT);
  pinMode(Chan2, OUTPUT);
  pinMode(Chan3, OUTPUT);
  pinMode(Chan4, OUTPUT);
  pinMode(Chan5, OUTPUT);   
  pinMode(Chan6, OUTPUT);
  pinMode(Chan7, OUTPUT);
  pinMode(Chan8, OUTPUT);
  pinMode(Chan9, OUTPUT);
  pinMode(Chan10, OUTPUT);   
  pinMode(Chan11, OUTPUT);
  pinMode(Chan12, OUTPUT);
  pinMode(Chan13, OUTPUT);
  
}

void loop()
{  // 14 channels coming to Arduino
   if (Serial.available() >= 14) {   
    for (int i=0; i<15; i++) {
       incomingByte[i] = Serial.read();
    }
    
    digitalWrite(Chan0, incomingByte[0]);   
    digitalWrite(Chan1, incomingByte[1]);   
    digitalWrite(Chan2, incomingByte[2]);   
    digitalWrite(Chan3, incomingByte[3]);   
    digitalWrite(Chan4, incomingByte[4]);   
    digitalWrite(Chan5, incomingByte[5]);   
    digitalWrite(Chan6, incomingByte[6]);   
    digitalWrite(Chan7, incomingByte[7]);   
    digitalWrite(Chan8, incomingByte[8]);   
    digitalWrite(Chan9, incomingByte[9]); 
    digitalWrite(Chan10, incomingByte[10]);   
    digitalWrite(Chan11, incomingByte[11]);   
    digitalWrite(Chan12, incomingByte[12]);   
    digitalWrite(Chan13, incomingByte[13]);   
      
   }
}

I am using the software Vixen, and the output is generic serial. I just have no idea why it worked for hours, then stopped. Every night when i restart it, it will get off again so i stop the show and restart it.

any help would be greatly appreciated :slight_smile:

Two things…

  1. The maximum array index is 13 not 14. The array is too small…

int incomingByte[14]; // store the 14 values from the serial port

for (int i=0; i<15; i++) {
incomingByte = Serial.read();

  • }[/font]*
    2. Your protocol has no synchronization. If any data is lost… well, you know the result.
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;

I don't see the point of this. Each one is its name. (Chan4 is 4 and so on).

    digitalWrite(Chan0, incomingByte[0]);   
    digitalWrite(Chan1, incomingByte[1]);   
    digitalWrite(Chan2, incomingByte[2]);   
    digitalWrite(Chan3, incomingByte[3]);   
    digitalWrite(Chan4, incomingByte[4]);   
    digitalWrite(Chan5, incomingByte[5]);   
    digitalWrite(Chan6, incomingByte[6]);   
    digitalWrite(Chan7, incomingByte[7]);   
    digitalWrite(Chan8, incomingByte[8]);   
    digitalWrite(Chan9, incomingByte[9]); 
    digitalWrite(Chan10, incomingByte[10]);   
    digitalWrite(Chan11, incomingByte[11]);   
    digitalWrite(Chan12, incomingByte[12]);   
    digitalWrite(Chan13, incomingByte[13]);

Better read up on the "for" loop. You could save a lot of coding here.

[quote author=Coding Badly link=topic=138729.msg1042017#msg1042017 date=1356236141] 2. Your protocol has no synchronization. If any data is lost... well, you know the result. [/quote]

Disaster?

[quote author=Nick Gammon link=topic=138729.msg1042066#msg1042066 date=1356243307]Disaster?[/quote]

For a Christmas display aficionado, you bet!

The symptom would be similar or identical to what @mennis22 described: lights and sound no longer synchronized.

No one else sees the problem with using pins 0 and 1 as output pins while reading serial data?

PaulS: No one else sees the problem with using pins 0 and 1 as output pins while reading serial data?

That, plus the lack of any method to detect and recover from errors on the serial stream.

(shameless self-promotion)

SerialCommands: easy command-based sketches

http://arduino.cc/forum/index.php/topic,133148.0.html

Thanks for the help everyone!! I believe it was in part of using pins 0 and 1. I don’t have them hooked up to anything but they were still in the code.

I’m sorry if this seemed like a dumb question. I’m only 17 and taught this all to myself so sorry for sounding stupid.

Hope everyone has a great Christmas!

mennis22:
I’m sorry if this seemed like a dumb question. I’m only 17 and taught this all to myself so sorry for sounding stupid.

The only way it would be a dumb question is if you were born with the knowledge of how to program an Arduino. I assume that is not the case which makes this nothing more than a learning experience.

Hope everyone has a great Christmas!

Back at ya!