my code.. continued.. too long.. sigh..
//==========================================================
void hextobyte(char *text,byte *bytes) {
unsigned int i, t, hn, ln;
for (t = 0,i = 0; i < atCmdValueSize; i+=2,++t) {
hn = text[i] > '9' ? text[i] - 'A' + 10 : text[i] - '0';
ln = text[i+1] > '9' ? text[i+1] - 'A' + 10 : text[i+1] - '0';
bytes[t] = (hn << 4 ) | ln;
}
return;
}
//=====================================================
//==========================================================
void xbSendPayload() {
xbee.send(zbTx);
// 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
Serial.println("TX Success!");
} else {
// the remote XBee did not receive our packet. is it powered on?
Serial.println("TX Delivery Failure, is it powered on?");
}
}
} else if (xbee.getResponse().isError()) {
Serial.print("Error reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
} else {
// local XBee did not provide a timely TX Status Response -- should not happen
Serial.println("TX Delivery Status TimeOut.");
}
}
//=====================================================
//==========================================================
void xbReceivePayload()
{
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
//Serial.println("Got something..");
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
//Serial.println("Got a ZB RX Packet..");
// now fill our zb rx class
//Serial.println("Filling rx...");
xbee.getResponse().getZBRxResponse(rx);
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
Serial.println("Sender get ACK.");
} else {
// we got it (obviously) but sender didn't get an ACK
Serial.println("Sender did not get ACK.");
}
// set dataLed PWM to value of the first byte in the data
} else if (xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE) {
xbee.getResponse().getModemStatusResponse(msr);
// the local XBee sends this response on certain events, like association/dissociation
if (msr.getStatus() == ASSOCIATED) {
// yay this is great. flash led
Serial.println("Modem Response : ASSOCIATED");
} else if (msr.getStatus() == DISASSOCIATED) {
// this is awful.. flash led to show our discontent
Serial.println("Modem Response : DISASSOCIATED");
} else {
// another status
Serial.println("Modem Response : UNKNOWN");
}
} else {
// not something we were expecting
Serial.println("Not ZB or MST response.");
}
} else if (xbee.getResponse().isError()) {
Serial.print("Error reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
}
}
//=====================================================
//==========================================================
void loop()
{
Serial.println("Preparing... You have 5 seconds to position..");
delay(5000);
Serial.println("Starting log...");
atRequest.setCommand(dbCommand);
atRequest.clearCommandValue();
while(1) {
if(nss.available()>0)
{
while(digitalRead(12)==HIGH);
duration[0]=pulseIn(12,HIGH,1000); //get the duration.. it is NOT working.... returns 0 values... when I get like a room far.. only it
//returned 100+- value... when I close in, it suddenly jumps to 0 again.
Serial.println(duration[0],DEC);
//xbReceivePayload();
atSendCommand();
delay(80);
nss.flush();
}
}
}