0

This question already has an answer here:

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 one 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:

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:

#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;
          }
    }
}

marked as duplicate by sachleen Mar 30 '14 at 6:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • also, maybe you should talk to @user734 and work on your posts together – Madivad Mar 30 '14 at 0:50
1

The problem is the same as last time (XBee+Arduino communication problem)

You are sending the data via:

Serial.println(val1);

which outputs an ASCII representation of the number.

But in your reading, you are reading in a byte value. Actually, I just realised you are reading in a char which is a signed (-128 to 127) value:

char inByte = portOne.read();

first, change that to:

byte inByte = portOne.read();

Now, you have three options:

  1. If (as you say) the numbers you expect are around 200 and will always be below 256 (and positive) then you could send a byte value instead. That way, when you read the byte in, you get the same value. You would do this using Serial.write(val1); instead of print.

  2. If you need to send larger than a byte value, eg int, then send as two bytes, HIGH and LOW. Then on the receiving end, take in two bytes when reading that data.

  3. Send as plain text but read in as plain text. Since you have delimiters, keep reading in the serial until you have all the data you need and then break it down. (You could adapt the code I posted in the answer above to do that).

edit: a couple of other points with your code:

  • You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

  • analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

  • You're never setting the pinMode of the outputs in the receiver.

  • You're sending val1, val2, and val3 out via Serial.println which will send your value out (as ASCII), followed by a newline, so the bytes sent from that one line will be 32 30 30 0D 0A. You have to interpret that at the other end.

  • You're also using software serial on the hardware serial port (you're assigning digital pins 0 and 1. These are the hardware serial pins). BUT you're also initialising it via hardware serial. I'm guessing that it really wouldn't matter (if the pins are aligned), but you can't expect to receive data via software serial and send data via hardware serial if they're all using the same pins.

Not the answer you're looking for? Browse other questions tagged or ask your own question.