Serial Communication help

HI all

I have a setup with 2 arduino and 2 XBee modules, with both arduino running the code as shown below.

Basically, each arduino transmits its 'millis' 4 times a second, and spends the rest of its time listening for incoming serial messages (at the moment this is just the other arduino's 'millis' message).

For something so basic I am not very familiar with the serial operations. Is there a better way of receiving the serial messages in the case below, perhaps looking for a specific character or string length?

Many thanks for your help.

#include <SoftwareSerial.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define LED 13

SoftwareSerial mySerial(10, 11); // RX, TX

int timer1_counter;
ISR(TIMER1_COMPA_vect)
{
  unsigned long onTime = millis();
  mySerial.print("C1 on-time: ");
  mySerial.println(onTime);
}

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(LED, OUTPUT);  
  pinMode(rssiPin, INPUT);

  // initialize Timer1
  cli();          // disable global interrupts
  TCCR1A = 0;     // set entire TCCR1A register to 0
  TCCR1B = 0;     // same for TCCR1B
  
  // set compare match register to desired timer count:
  OCR1A = 3906; // = (16*10^6) / (1*1024) - 1 (must be <65536)
  
  // turn on CTC mode:
  TCCR1B |= (1 << WGM12);
  
  // Set CS10 and CS12 bits for 1024 prescaler:
  TCCR1B |= (1 << CS10);
  TCCR1B |= (1 << CS12);
  
  // enable timer compare interrupt:
  TIMSK1 |= (1 << OCIE1A);
  
  // enable global interrupts:
  sei();
}

void loop() // run over and over
{
  if (mySerial.available()>0)  
  {
    Serial.write(mySerial.read());
    digitalWrite(LED, HIGH);
    digitalWrite(LED,LOW);
  }
}

As a related issue, this setup works fine 95% of the time and the message is received as I would expect it to be.

Occasionally however the serial data becomes complete gibberish, such as this:

C1 on-time: 1751674
C1 on-time: 1751903
C1 on-time: 1752132
ÒË«?+««K?ºª??²?jRüz¹µÑ¥µ?é?ºª?ªÂÊjRü¨&HË«?+««K?ºª?Â?ºjRüC1 on-time: 1753046
on-time: 1753275
¤Ë«?+««K?ºª?ª??jRü·n-time: 1753732
z¹µÑ¥µ?é?ºª?ʲ?jRüÞ¹µÑ¥µ?é?ºª¢?ÂÂjRüz¹µÑ¥µ?é?ºª¢¢?ÂjRü@½¹µÑ¥µ?é?ºª¢²¢²jRü·n-time: 1754874
¹µÑ¥µ?é?ºªª???jRüôå«?+««K?ºªª???jRüë«?+««K?ºªªªªÊjRü[K]Z[YN??55789
W««K?ºª²??ºjRüÜZÑ¥µ?é?ºª²?¢²jRü]Z[YN??56474
¢¥µ?é?ºª²º??jRüµÑ¥µ?é?ºª²Ê??jRüWV«K?ºªº?²?jRüÑ¥µ?é?ºªº?ÂÂjRüµ?é?ºªº²?ºjRü««K?ºªºÂ¢ªjRü®ª[YN??58074
WV«K?ºªÂ???jRü?é?ºªÂª??jRüµ?é?ºªÂºªÊjRü'¤LSS''§CÂme: 1759216
ÊéÅºªÊ¢¢²jRü?é?ºªÊ²º¢jRüYN??59902
é?º²????jRü?º²??ªÊjRüRÓ?S§!á)Ó?§¦SHøHL?S&¦¦C¦?S?¦ÓSHø'¤L?SS?¦!á1761731
?SS§¦§CÂ??S&'§CÂ1762416
dÙÑÑjRüÙɺ?jRüfÅÁÉjRü²????jRü?559
?ºÂºjRü¢??²jRü245
¢¢º?jRü02
4930
ªÂjRümáájRþ)ÉSHø¢¢jRüSHø&¦¦CÂ`)ÿå5)þá?!áü!áHø??CCCC1C1CCC1C1 C1 oC1 C1 C1 on-C1 onC1 onC1 on-C1 on-C1 on-C1 on-tC1 on-tC1 on-tC1 on-tC1 on-tiC1 on-C1 on-tiC1 on-tiC1 on-timC1 on-timeC1 on-timeC1 on-timeC1 on-timeC1 on-timeC1 on-timC1 on-timC1 on-timeC1 on-time: C1 on-time: C1 on-time: C1 on-time:C1 on-time:C1 on-time: 1C1 on-time: C1 on-time: 1C1 on-time: 17C1 on-time: 17C1 on-time: 1C1 on-time: 177C1 on-time: 178C1 on-time: 17C1 on-time: 1780C1 on-time: 17C1 on-time: 1780C1 on-time: 178C1 on-time: 178C1 on-time: 1781C1 on-time: 17818C1 on-time: 17820C1 on-time: 17822C1 on-time: 17825C1 on-time: 178275C1 on-time: 1782985C1 on-time: 178321C1 on-time: 1783442
C1 on-time: 1783671C1 on-time: 1783900C1 on-time: 1784128
C1 on-time: 1784356
C1 on-time: 1784585
C1 on-time: 1784813
C1 on-time: 1785042
C1 on-time: 1785271
C1 on-time: 1785499
C1 on-time: 1785728
C1 on-time: 1785956

Is this an issue at the XBee side of things or something I am doing with the serial data within my arduino code? The problem gets worse as I up the interrupt frequency. Beyond about 5-6Hz the data is pure gibberish!

I'd be surprised if it is necessary to use interrupts for such a simple task.

In any case its much too difficult to understand code that uses interrupts without a comprehensive english explanation of what each part is supposed to do.

I have an application in which a "master" sends data to several "slaves" using 2.4GHz wireless and receives the slaves' responses. I don't use interrupts.

Without studying your code I wonder if the problem arises because some of the time the receiver isn't in sync with the bits in the serial byte - for example it may miss the start bit. I spent a lot of time with another application trying to deal with that reliably - I wanted my receiver to sleep and then it might wake in the middle of a byte and think it was the start of the byte.

...R

I'd be surprised if it is necessary to use interrupts for such a simple task.

Probably not, but this project will at some point in the future be far more complex and so this is an opportunity to gain familiarity with them now.

Thanks for the rest of your feedback, I'll have a look into the things you mentioned

so this is an opportunity to gain familiarity with them now.

Then you should start with learning what you can, and can not do, in an interrupt service routine. Anything that relies on interrupts, like sending serial data, is a no-no in an ISR.