/**
* 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 .
*/
#include
/*
This example is for Series 2 XBee
Sends a ZB TX request with the value of analogRead(pin5) and checks the status response for success
*/
// analog pin to read
int read_pin_a = 5;
int read_pin_b = 3;
// pin for led
int write_pin = 9;
int loop_delay = 50;
// create the XBee object
XBee xbee = XBee();
uint8_t payload[] = { 0, 0, 0 };
// send to coordinator
XBeeAddress64 addr64 = XBeeAddress64(0x0, 0x0);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
int value_a = 0;
int value_b = 0;
int out_value_a = 255;
int out_value_b = 255;
int statusLed = 13;
int errorLed = 13;
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);
analogWrite(out_value_a, write_pin);
xbee.begin(9600);
}
void loop()
{
// break down 10-bit reading into two bytes and place in payload
value_a = analogRead(read_pin_a);
value_b = analogRead(read_pin_b);
// mirror led value locally
out_value_a = map(value_a, 0, 1023, 1, 255);
out_value_b = map(value_b, 0, 1023, 1, 255);
analogWrite(write_pin, out_value_a);
payload[0] = 0; // designates start of packet
payload[1] = out_value_a; // first sensor value
payload[2] = out_value_b; // second sensor value
xbee.send(zbTx);
// flash TX indicator
//flashLed(statusLed, 1, 100);
// after sending a tx request, we expect a status response
// wait up to half second for the status response
if (xbee.readPacket(500)) {
// got a response!
// should be a znet tx status
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
xbee.getResponse().getZBTxStatusResponse(txStatus);
// get the delivery status, the fifth byte
if (txStatus.getDeliveryStatus() == 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 {
// local XBee did not provide a timely TX Status Response -- should not happen
//flashLed(errorLed, 2, 50);
}
delay(loop_delay);
}