SparkFun Forums 

Where electronics enthusiasts find answers.

All things pertaining to wireless and RF links
By Renee
#169887
I am sending three analog signals through the xbees to be written to analog pins on the receiving side so they can be read in Simulink. I connected both of the arduino megas to my laptop and uploaded the sketches. I have a shield and a series xbee connected to each arduino. I pulled up the serial monitor for each sketch. I connected a 1 volt DC power supply to all three analog pins. On the transmitting serial monitor I got numbers close to 200 which I believe is correct but on the receiving side I was not getting anything. I believe the problem is with the arduino. After I uploaded the code on the transmitting side the TX led was green but the RX led on the receiving side was not lit up at all. The power LED on the transmitting arduino is red but it is green on the receiving side. The 'L' led on the transmitting side is red on the transmitting side is read and yellow on the receiving side. On both of the xbee shield the power led's are red and the DIO5 led blinks green. On the receiving xbee shield the DOUT LED is red and the DI LED is green. If more information is needed let me know.

Here is my configuration for the xbees: (series 1. firmware version: 10ed)
Transmitting Xbee:
PAN ID: 1234
MY:10
DL:11

Receiving Xbee:
PAN ID: 1234
MY: 11
DL: 10

Transmitting arduino program:
Code: Select all
int val1 = 0;
int val2 = 0;
int val3 = 0;

int analogPin1 = 0; //these are you analog in pins
int analogPin2 = 1;
int analogPin3 = 2;

void setup()  {
  Serial.begin(9600);  //start serial
}



void loop()                               // run over and over again
{
        Serial.print("<");                // This character signifies the begining of our data cycle
        
        val1 = analogRead(analogPin1);    // read the input pin
        Serial.println(val1);             // output the signal
        
        Serial.print("\t");               // Signifies end of 1st piece of data
        
        val2 = analogRead(analogPin2);    // read the input pin
        Serial.println(val2);             // output the signal
        
        Serial.print("\t");               // Signifies end of 2nd piece of data        
        
        val3 = analogRead(analogPin3);    // read the input pin
        
        Serial.println(val3);             // output the signal
        
        Serial.print(">");                // This character signifies the end of our data cycle
        
        delay(100); 
}
Receiving arduino program:
Code: Select all
#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 0, RX = digital pin 1
SoftwareSerial portOne(0,1);

int Apin1 = 0; // These variables hold the current incoming value
int Apin2 = 0;
int Apin3 = 0;

int AnalogOut1 = 0;  //Set our analog pins
int AnalogOut2 = 1;
int AnalogOut3 = 2;

int Digits = 0; // Helps watch for comma

int Mode = 0; // This designates which section of reading we are doing - begin = 0, Apin1 = 1, Apin2 = 2, APin3 = 3, end = 4

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // Start each software serial port
  portOne.begin(9600);
}

void loop()
{
  portOne.listen();
  while (portOne.available() > 0) {
  // read the incoming byte:
    char inByte = portOne.read();

    if (isdigit(inByte) && Mode == 1) {
    int temp = inByte - '0';
    Apin1 = (Apin1 * 10) + temp;
    Serial.print('\n'); // This is just some debugging stuff, if you plug in your programmer this will output the data to a serial monitor
    Serial.print(temp);
    Serial.print('\n');
    Serial.print(Apin1);
    Serial.print('\n');
    }

    if (isdigit(inByte) && Mode == 2) {
    int temp = inByte - '0';
    Apin2 = (Apin2 * 10) + temp;
    Serial.print(temp); // This is just some debugging stuff, if you plug in your programmer this will output the data to a serial monitor
    Serial.print('\n');
    Serial.print(Apin2);
    Serial.print('\n');
    }

    if (isdigit(inByte) && Mode == 3) {
      int temp = inByte - '0';
    Apin3 = (Apin3 * 10) + temp;
    Serial.print(temp); // This is just some debugging stuff, if you plug in your programmer this will output the data to a serial monitor
    Serial.print('\n');
    Serial.print(Apin3);
    Serial.print('\n');
    }

    if (inByte == ',') {
    Mode++;
    }

    if (inByte == '<') {
    Mode = 1;
    }

    if (inByte == '>') {
    Mode = 4;

    if (Apin1 > 0 && Apin2 > 0 && Apin3 > 0) {
    analogWrite(AnalogOut1, Apin1);
    analogWrite(AnalogOut2, Apin2);
    analogWrite(AnalogOut3, Apin3);
    }

    Serial.print('\n'); // This is just some debugging stuff, if you plug in your programmer this will output the data to a serial monitor
    Serial.print(Apin1);
    Serial.print('\n');
    Serial.print(Apin2);
    Serial.print('\n');
    Serial.print(Apin3);
    Serial.print('\n');


    Apin1 = 0; // These variables hold the current incoming value
    Apin2 = 0;
    Apin3 = 0;

          }
    }
}
By Valen
#169975
You have 2 Arduino Mega's with each 3 hardware serial ports. Yet you have to use the software serial library to simulate another one on extra pins via software? That does not seem neccesary. Why did you do this?

Since you are having trouble with the transmission of data over the serial connection I suggest you first eliminate the XBee link in your setup while you are testing. (AND the software-serial library) It's an added level of complexity you can do without. Connect the TX from one Mega to the RX of the other, and vice versa. And also GND ofcourse or else the voltages are just floating in the air. Then focus on getting your code to work correctly (get the data from one mega to the other). If it works as you want it, then include the XBee link and make it wireless.

Also, I have no idea how this Simulink code plays into this. I understand that it can generate Arduino sketches. But how or what format Simulink expects is something you have to find out for yourself.

I also suggest some other kind of serial monitor, as the Arduino serial monitor does not show all bytes it receives. But only the ones that it can show an ASCII character for. Some values might come along and will not be shown, or just look like a space. Get yourself a seperate terminal emulator program. Personally I like this one. It can simultaneously show the ascii characters received, aswel as the Hex and binary value of it.

https://sites.google.com/site/terminalbpp/

[EDIT]As for led colors, I don't own one. So I cannot confirm. But the official schematic on the Arduino forums only shows a normal led, without color specification. If two Megas show different colors then it might simply be fabrication reasons for it. Atleast, I asume if they are from the same vendors. If you want to know if it really passes data or just stays in one level, then I suggest you hook it up to an oscilloscope or logic analyser and look at the waveforms.
Last edited by Valen on Thu Apr 03, 2014 9:59 am, edited 1 time in total.
By Valen
#169976
Also looking into your receive code:
Code: Select all
...
if (isdigit(inByte) && Mode == 1) {
...
Be careful with concatenating these logic statements. There is a priority to these operators! First the == is evaluated, then the &&. I presume this was intended, but to remove any doubt and avoid possible compiler incompliance I would use round brackets to force the priority.
Code: Select all
...
if (isdigit(inByte) &&  (Mode == 1) ) {
...
[EDIT]Your receive-code also never sends to the (hardware) serial port and monitor what it did receive. It only reformats the incomming bytes to a integer value and sends debug data, whenever the incomming by is a decimal digit in ASCII. If it receives something entirely else then you'll never know what it is. I suggest you also forward the bytes that are received to the serial (monitor) port. That way you get a better idea if there is some data corruption along the way.