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);
}
}
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.
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.