Hi Guys!
Im into the last phase of my final year project and things are not looking good.
I have an ultrasonic sensor that is connected to an arduino uno and an xbee series 2 mounted onto the uno via a xbee shield. The xbee is configured as Router AT mode while the Coordinator is in API mode.
I am able to get response from the sensor in the coordinator but the values are wrong. Could someone point to me what i am doing wrong here.
Router (Ultrasonic Sensor Node)
#include "LCD4884.h"
#include <XBee.h>
// Xbee initialisation
XBee xbee = XBee();
uint8_t text[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// Address of receiving Xbee
XBeeAddress64 remoteAddress = XBeeAddress64(0x0013a200, 0x408b60eb);
ZBTxRequest zbTx = ZBTxRequest(remoteAddress, text, sizeof(text));
// Variables
const int sensorPin = 7;
int tonePin = 10;
int RedPin = 11;
int YellowPin = 12;
int GreenPin = 13;
int duration;
int distance;
int id = 0;
void setup() {
lcd.LCD_init();
lcd.LCD_clear();
Serial.begin(9600);
xbee.setSerial(Serial);
// Pinmodes
pinMode(sensorPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(RedPin, OUTPUT);
pinMode(YellowPin, OUTPUT);
pinMode(GreenPin, OUTPUT);
pinMode(tonePin, OUTPUT);
}
void loop()
{
long duration, cm;
pinMode(sensorPin, OUTPUT);
digitalWrite(sensorPin, LOW);
delayMicroseconds(2);
digitalWrite(sensorPin, HIGH);
delayMicroseconds(10);
digitalWrite(sensorPin, LOW);
//Compute distance
pinMode(sensorPin, INPUT);
duration = pulseIn(sensorPin, HIGH);
// convert the time into a distance
distance = duration/56.5;
Serial.println(distance);
delay(50);
// Transfer distance to payload
text[0] = distance >> 8 & 0xff;
text[1] = distance & 0xff;
xbee.send(zbTx);
//Print data on LCD 4884
lcd.LCD_clear();
if (distance >= 20 )
{
char tempString[12];
lcd.LCD_write_string(28, 2, "Water", MENU_NORMAL);
lcd.LCD_write_string(28, 3, "Level", MENU_NORMAL);
lcd.LCD_write_string(36, 4, "OK", MENU_NORMAL);
}
else {
char tempString[12];
sprintf(tempString, "=%3d cms", distance);
lcd.LCD_write_string(15, 1, "Distance", MENU_NORMAL);
lcd.LCD_write_string(15, 2, "Measuring", MENU_NORMAL);
lcd.LCD_write_string(15, 4, tempString, MENU_NORMAL);
delay (100);
}
//Turn ON LED and Buzzer
if (distance > 0 && distance <= 10) {
digitalWrite(RedPin, HIGH);
digitalWrite(YellowPin, LOW);
digitalWrite(GreenPin, LOW);
tone(tonePin, 200000, 200);
}
else if (distance >= 11 && distance <= 20) {
digitalWrite(RedPin, LOW);
digitalWrite(YellowPin, HIGH);
digitalWrite(GreenPin, LOW);
tone(tonePin, 5000, 200);
}
else if (distance >= 21) {
digitalWrite(RedPin, LOW);
digitalWrite(YellowPin, LOW);
digitalWrite(GreenPin, HIGH);
delay(1000);
noTone(tonePin);
}
}
Coordinator Code
#include <SPI.h>
#include <XBee.h>
// Xbee initialisation
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse zbRx = ZBRxResponse();
Rx64Response rx64 = Rx64Response();
uint8_t text[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //initialises the data array with 0s
void setup()
{
Serial.begin(9600);
xbee.begin(Serial);
}
void loop(){
// Read Xbee data
xbee.readPacket();
// Check packet
if (xbee.getResponse().isAvailable()) {
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
xbee.getResponse().getZBRxResponse(zbRx);
// read available message into text
for (int i = 0; i < zbRx.getDataLength(); i++) {
text [i] = zbRx.getData(i);
}
int distance1 = text[0] ;
//Serial.print(distance1);
int distance2 = text[1] ;
// Serial.println(distance2);
int distance3 = distance2 + (distance1 * 256);
//Serial.println(distance3);
}
}
I am really in need of help pls!
Regards
Eddie