but when i tried to do the same thing using a class I am writing, it didn't work:
/*******************************
class to send and receive
messages using an xbee. the
send and recieve data types
are the ones i need for this
project. for general use, this
should be a template class.
*******************************/
class Zigbee {
public:
// constructor takes starting serial speed
Zigbee(int speed = 9600);
// send a char
void send(char message);
// receive an char
char receive();
};
// constructor takes starting serial speed
Zigbee::Zigbee(int speed) {
Serial.begin(speed);
}
// send char
void Zigbee::send(char message) {
Serial.println(message);
}
// wait until an char is received and return it
char Zigbee::receive() {
//while(Serial.available() < 1); // wait until there is data
char message[1];
Serial.readBytes(message, 1); // stores first char in message
return message[0];
}
Zigbee Xbee(9600);
void setup() {}
void loop() {
Xbee.send(42);
delay(100);
}
Is there something obvious I'm missing? I would really appreciate help.
thanks for the suggestions. i changed Zigbee::receive() and loop() but this still doesn't work:
/*******************************
class to send and receive
messages using an xbee. the
send and recieve data types
are the ones i need for this
project. for general use, this
should be a template class.
*******************************/
class Zigbee {
public:
// constructor takes starting serial speed
Zigbee(int speed = 9600);
// send a char
void send(char message);
// receive an char
char receive();
};
// constructor takes starting serial speed
Zigbee::Zigbee(int speed) {
Serial.begin(speed);
}
// send char
void Zigbee::send(char message) {
Serial.println(message);
}
// wait until an char is received and return it
char Zigbee::receive() {
//while(Serial.available() < 1); // wait until there is data
char* message;
Serial.readBytes(message, 1); // stores first char in message
return *message;
}
Zigbee Xbee(9600);
void setup() {}
void loop() {
Xbee.send(Xbee.receive());
}
problem solved. I turns out that it didn't work because I tried to start the Serial before arduino called init(), which is apparently where all those functions and classes are initialized. Thanks for trying to help me