Pages: [1]   Go Down
Author Topic: Not displaying correct values  (Read 915 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

   I am trying to send information from arduino to the xbee explorer using Xbee series-1.I am using the following code

/**
 * Copyright (c) 2009 Andrew Rapp. All rights reserved.
 *
 * This file is part of XBee-Arduino.
 *
 * XBee-Arduino is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XBee-Arduino is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XBee-Arduino.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include <XBee.h>
#include <SoftwareSerial.h>

#include <I2C.h>
#include <MMA8453_n0m1.h>
MMA8453_n0m1 accel;
// Define SoftwareSerial TX/RX pins
// Connect Arduino pin 9 to TX of usb-serial device
uint8_t ssRX = 9;
// Connect Arduino pin 10 to RX of usb-serial device
uint8_t ssTX = 10;
// Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
SoftwareSerial nss(ssRX, ssTX);

/*
This example is for Series 1 XBee
Sends a TX16 or TX64 request with the value of analogRead(pin5) and checks the status response for success
Note: In my testing it took about 15 seconds for the XBee to start reporting success, so I've added a startup delay
*/

XBee xbee = XBee();

unsigned long start = millis();

// allocate two bytes for to hold a 10-bit analog reading
uint8_t payload[] = { 0, 0 };

// with Series 1 you can use either 16-bit or 64-bit addressing

// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(0x1234, payload, sizeof(payload));

// 64-bit addressing: This is the SH + SL address of remote XBee
//XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x406c3887);
// unless you have MY on the receiving radio set to FFFF, this will be received as a RX16 packet
//Tx64Request tx = Tx64Request(addr64, payload, sizeof(payload));

TxStatusResponse txStatus = TxStatusResponse();

int pin5 = 0;

int statusLed = 11;
int errorLed = 12;

void flashLed(int pin, int times, int wait) {
   
    for (int i = 0; i < times; i++) {
      digitalWrite(pin, HIGH);
      delay(wait);
      digitalWrite(pin, LOW);
     
      if (i + 1 < times) {
        delay(wait);
      }
    }
}

void setup() {
  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);
 
  xbee.begin(9600);
  nss.begin(9600);
  nss.println("starting up yo!");
   accel.setI2CAddr(0x1D); //change your device address if necessary, default is 0x1C
  accel.dataMode(true, 2); //enable highRes 10bit, 2g range [2g,4g,8g]
}

void loop() {
   
    accel.update();
 
   // start transmitting after a startup delay.  Note: this will rollover to 0 eventually so not best way to handle
    if (millis() - start > 15000) {
      // break down 10-bit reading into two bytes and place in payload
      pin5 = accel.x();
      payload[0] = pin5 >> 8 & 0xff;
      payload[1] = pin5 & 0xff;
      Tx16Request tx = Tx16Request(0x1234, payload, sizeof(payload));
      xbee.send(tx);

      // flash TX indicator
     // flashLed(statusLed, 1, 100);
    }
 
    // after sending a tx request, we expect a status response
    // wait up to 5 seconds for the status response
    if (xbee.readPacket(5000)) {
        // got a response!

        // should be a znet tx status               
       if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) {
          xbee.getResponse().getZBTxStatusResponse(txStatus);
          
          // get the delivery status, the fifth byte
           if (txStatus.getStatus() == SUCCESS) {
              // success.  time to celebrate
              flashLed(statusLed, 5, 50);
           } else {
               // the remote XBee did not receive our packet. is it powered on?
              flashLed(errorLed, 3, 500);
           }
        }     
    } else if (xbee.getResponse().isError()) {
      nss.print("Error reading packet.  Error code: "); 
      //nss.println(xbee.getResponse().getErrorCode());
      // or flash error led
    } else {
      // local XBee did not provide a timely TX Status Response.  Radio is not configured properly or connected
     flashLed(errorLed, 2, 50);
    }
   
    delay(1000);
}


     The serial monitor is displaying some trash. Please tell me whats wrong with my program.

Thnaku very much
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question

Code tags please.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry i dont understand what do u mean by "Code tags please".
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can u plese tell me how to modify the code to display the correct value in the serial monitor

Thanku very much
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry i dont understand what do u mean by "Code tags please".

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The serial monitor is displaying some trash. Please tell me whats wrong with my program.
Probably nothing is wrong with the program. Most likely, you don't have the Serial Monitor baud rate set to match the Arduino's serial baud rate.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The serial monitor is displaying some trash. Please tell me whats wrong with my program.

I don't see where you did Serial.begin().

Plus see reply #4.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

   I am trying to send information from arduino to the xbee explorer using Xbee series-1.I am using the following code

 
Code:

/**
 * Copyright (c) 2009 Andrew Rapp. All rights reserved.
 *
 * This file is part of XBee-Arduino.
 *
 * XBee-Arduino is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XBee-Arduino is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XBee-Arduino.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include <XBee.h>
#include <SoftwareSerial.h>

#include <I2C.h>
#include <MMA8453_n0m1.h>
MMA8453_n0m1 accel;
// Define SoftwareSerial TX/RX pins
// Connect Arduino pin 9 to TX of usb-serial device
uint8_t ssRX = 9;
// Connect Arduino pin 10 to RX of usb-serial device
uint8_t ssTX = 10;
// Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
SoftwareSerial nss(ssRX, ssTX);

/*
This example is for Series 1 XBee
Sends a TX16 or TX64 request with the value of analogRead(pin5) and checks the status response for success
Note: In my testing it took about 15 seconds for the XBee to start reporting success, so I've added a startup delay
*/

XBee xbee = XBee();

unsigned long start = millis();

// allocate two bytes for to hold a 10-bit analog reading
uint8_t payload[] = { 0, 0 };

// with Series 1 you can use either 16-bit or 64-bit addressing

// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(0x1234, payload, sizeof(payload));

// 64-bit addressing: This is the SH + SL address of remote XBee
//XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x406c3887);
// unless you have MY on the receiving radio set to FFFF, this will be received as a RX16 packet
//Tx64Request tx = Tx64Request(addr64, payload, sizeof(payload));

TxStatusResponse txStatus = TxStatusResponse();

int pin5 = 0;

int statusLed = 11;
int errorLed = 12;

void flashLed(int pin, int times, int wait) {
   
    for (int i = 0; i < times; i++) {
      digitalWrite(pin, HIGH);
      delay(wait);
      digitalWrite(pin, LOW);
     
      if (i + 1 < times) {
        delay(wait);
      }
    }
}

void setup() {
  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);
 
  xbee.begin(9600);
  nss.begin(9600);
  nss.println("starting up yo!");
   accel.setI2CAddr(0x1D); //change your device address if necessary, default is 0x1C
  accel.dataMode(true, 2); //enable highRes 10bit, 2g range [2g,4g,8g]
}

void loop() {
   
    accel.update();
 
   // start transmitting after a startup delay.  Note: this will rollover to 0 eventually so not best way to handle
    if (millis() - start > 15000) {
      // break down 10-bit reading into two bytes and place in payload
      pin5 = accel.x();
      payload[0] = pin5 >> 8 & 0xff;
      payload[1] = pin5 & 0xff;
      Tx16Request tx = Tx16Request(0x1234, payload, sizeof(payload));
      xbee.send(tx);

      // flash TX indicator
     // flashLed(statusLed, 1, 100);
    }
 
    // after sending a tx request, we expect a status response
    // wait up to 5 seconds for the status response
    if (xbee.readPacket(5000)) {
        // got a response!

        // should be a znet tx status           
    if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) {
       xbee.getResponse().getZBTxStatusResponse(txStatus);
   
       // get the delivery status, the fifth byte
           if (txStatus.getStatus() == SUCCESS) {
            // success.  time to celebrate
            flashLed(statusLed, 5, 50);
           } else {
            // the remote XBee did not receive our packet. is it powered on?
            flashLed(errorLed, 3, 500);
           }
        }     
    } else if (xbee.getResponse().isError()) {
      nss.print("Error reading packet.  Error code: "); 
      //nss.println(xbee.getResponse().getErrorCode());
      // or flash error led
    } else {
      // local XBee did not provide a timely TX Status Response.  Radio is not configured properly or connected
     flashLed(errorLed, 2, 50);
    }
   
    delay(1000);
}


     The serial monitor is displaying some trash. Please tell me whats wrong with my program.

Thnaku very much
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,Even though after including  Serial.begin() also it is showing some trash in the serial monitor and the baud rate is same for serial monitor also.
Can u please tell me the configuration settings of the two xbees using XCTU.

Thanku very much
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4807
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// Connect Arduino pin 9 to TX of usb-serial device
uint8_t ssRX = 9;
// Connect Arduino pin 10 to RX of usb-serial device
uint8_t ssTX = 10;
// Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
SoftwareSerial nss(ssRX, ssTX);

What Arduino are you using? My UNO does (hardware) usb-serial on pins 2 and 3 only.
But I could go softserial on 9 and 10 to a serial-USB adapter, I suppose.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using Arduino Uno
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can u please tell me the configuration settings of the two xbees using XCTU.
Can you learn to spell. You is not u.

You need to have set PAN ID, DL, and MY. What values did you set/do you have?

What baud rate are the XBees operating at?

Quote
What Arduino are you using? My UNO does (hardware) usb-serial on pins 2 and 3 only.
The ones labeled 0 and 1 on the board?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@OP:  You can still go back to the start of this thread and add those code tags -  it isn't too late.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry i dont understand what do u mean by "Code tags please".

Did you read the thread I linked about how to post a programming question? If not, and if you are going to ignore us, I'll have to lock this thread. Sorry.
Logged


Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4807
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What Arduino are you using? My UNO does (hardware) usb-serial on pins 2 and 3 only.
The ones labeled 0 and 1 on the board?

Hence my confusion over INT0 and INT1 being tied up with Serial when they're not.
I swear I go round this monkey ride a few more times and I'll either never forget or never get it right!

Yeah... Arduino pins 0 and 1, 328P pins 2 and 3, but definitely not pins 9 and 10. I'm just wondering since the code I've seen doesn't use the usual path to Serial Monitor if the screen garbage has anything to do with the wiring to get data to it. Something must go there or there wouldn't be garbage would there?



Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: