I changed the switch statement into an "if" and the if(XBeeRX.available()) to while(XBeeRX.available())
For some reason nothing shows up in the COM port which is connected to the receiver. I introduced a delay between the transmissions in the transmitter side. These are my codes :
TRANSMITTER
#include <SoftwareSerial.h>
#include <i2cmaster.h>
SoftwareSerial XBeeTX(2, 3); // RX, TX
byte dev = 0x5A<<1;
char data_start = '*';
void setup()
{
Serial.begin(9600);
XBeeTX.begin(9600);
i2c_init(); //Initialise the i2c bus
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups
}
void loop()
{
byte data_low;
byte data_high;
byte pec = 0;
i2c_start_wait(dev+I2C_WRITE);
i2c_write(0x07);
// read
i2c_rep_start(dev+I2C_READ);
data_low = i2c_readAck(); //Read 1 byte and then send ack
data_high = i2c_readAck(); //Read 1 byte and then send ack
pec = i2c_readNak();
i2c_stop();
Serial.println(data_start, HEX);
XBeeTX.print(data_start, HEX);
delay(40);
Serial.println(data_high, HEX);
XBeeTX.print(data_high, HEX);
delay(40);
Serial.println(data_low, HEX);
XBeeTX.print(data_low, HEX);
delay(40);
delay(1000);
}
RECEIVER
#include <SoftwareSerial.h>
#include <ctype.h>
SoftwareSerial XBeeRX(2, 3); // RX, TX
char data_start = 0x2A;
boolean newReading;
byte readPosition;
byte data_low;
byte data_high;
char data_in;
void setup()
{
Serial.begin(9600);
XBeeRX.begin(9600);
}
void loop()
{
while (XBeeRX.available())
{
data_in = XBeeRX.read();
if(readPosition==0 && data_in==data_start)
{
readPosition++;
}
if(readPosition==1)
{
data_high = HexCharToValue(data_in)<<4;
readPosition++;
}
if(readPosition==2)
{
data_high = HexCharToValue(data_in)+data_high;
readPosition++;
}
if(readPosition==3)
{
data_low = HexCharToValue(data_in)<<4;
readPosition++;
}
if(readPosition==4)
{
data_low = HexCharToValue(data_in)+data_low;
readPosition = 0;
newReading = true;
}
}
if (newReading == true)
{
Serial.println(data_start, HEX);
Serial.println(data_high, HEX);
Serial.println(data_low, HEX);
double tempFactor = 0.02;
double tempData = 0x0000;
int frac;
tempData = (float)(((data_high & 0x007F) << 8) + data_low);
tempData = (tempData * tempFactor)-0.01;
float celcius = tempData - 273.15;
Serial.print("Celcius: ");
Serial.println(celcius);
newReading = false;
}
}
byte HexCharToValue (char x)
{
byte value;
int upper;
byte first_result;
if(isxdigit(x))
{
upper = toupper(x);
first_result = upper - 0x30;
if (first_result < 0x0A)
{
value = first_result;
}
else
{
value = first_result - 0x10;
value = value + 10;
}
}
else
{
value = 0;
}
return value;
}
Could anyone have any idea why nothing seems to show up in the receiver COM port?