Ok.. before the obvious question of "there has been many topics on that, just do a search"... I have to say I did, and my whole day was spent debugging why it is not working, but the other people using the same method is working....
So basically.... I'm now calling for help.. desperate.
1.What I want to do. -Get RSSI values from XBee for Localization.
2. Why use PWM, rather than the ATDB method? - I want more resolution... ATDB command returns range of 256 values only.
3. What have I tried - pulseIn fucntion... and it is causing me much headache as to WHY hasn't it work.
The pulsein function gets 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.
I thought it's because of pulseIn can't detect the pulses?
From Xbee datasheet,
PWM operates at 12MHz, period is 200us...
This is another baffling stuff to me.. Period according to frequency... 1/12M=0.08333us, Period according to statemet is 200us.. @@
//==========================================================
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();
}
}
}
Why use PWM, rather than the ATDB method? - I want more resolution... ATDB command returns range of 256 values only.
Breaking a half-assed guess as to RSSI into 256 values is still going to have huge amounts of discrepancy in the distances you try to discern from those values.
Like printing a float to 12 decimal places, you can not make an inaccurate value more accurate by reading it a different way.
Why use PWM, rather than the ATDB method? - I want more resolution... ATDB command returns range of 256 values only.
Breaking a half-assed guess as to RSSI into 256 values is still going to have huge amounts of discrepancy in the distances you try to discern from those values.
Like printing a float to 12 decimal places, you can not make an inaccurate value more accurate by reading it a different way.
So... what you mean is... even if PWM values may have a higher resolution.. it still won't give a higher accuracy?
I need the resolution so that I can discern smaller changes in distances. Right now, if I'm using the DB command, it's sensitivity is 10CM per dB unit. When it's close. When it's further away, it' resolution increases...
I have to have a higher sensitivity/resolution distance per unit change.... so that is why I am making the PWM RSSI work, since it says in the datasheet, it is able to give a resolution of 2400 counts...