Hello all,
So I just got my two RFM12B transceivers from sparkfun wired up and sending data back and forth. I took the packetbuf example and modified it to send a number based on what key I send to the transmitter arduino, very basic. Now I am trying to work in motor control and I have hit a wall.
I am using the motor shield made by Lady Ada, I have the library included with no error. But when I try to instantiate the AF_DCMotor class the arduino no longer receives data over the radio. It still spits out my eeprom config, but no output from the radio. I have no idea where to even begin looking for a fix because this seems so basic. Anyway here is my transmitter and receiver code:
Receiver
#include <Ports.h>
#include <RF12.h>
#include <AFMotor.h>
class PacketBuffer : public Print {
public:
PacketBuffer () : fill (0) {}
const byte* buffer() { return buf; }
byte length() { return fill; }
void reset() { fill = 0; }
virtual void write(uint8_t ch)
{ if (fill < sizeof buf) buf[fill++] = ch; }
private:
byte fill, buf[RF12_MAXDATA];
};
AF_DCMotor motor(2, MOTOR12_64KHZ);
byte myId; // remember my own node ID
byte needToSend; // set when we want to send
word counter; // incremented each second
MilliTimer sendTimer; // used to send once a second
PacketBuffer payload; // temp buffer to send out
static void checkForCmd (char c) {
switch (c) {
default:
Serial.print(c);
break;
case '2': // forward
Serial.print("forward");
break;
case '3': // backward
Serial.print("backward");
break;
case '5': // left
Serial.print("left");
break;
case '7': // right
Serial.print("right");
break;
}
}
void setup () {
Serial.begin(57600);
Serial.print("\n[rfStrings]");
myId = rf12_config();
}
void loop () {
if (rf12_recvDone() && rf12_crc == 0) {
// a packet has been received
Serial.print("GOT ");
for (byte i = 0; i < rf12_len; ++i)
checkForCmd(rf12_data[i]);
Serial.println();
}
}
Transmitter
#include <Ports.h>
#include <RF12.h>
// Utility class to fill a buffer with string data
class PacketBuffer : public Print {
public:
PacketBuffer () : fill (0) {}
const byte* buffer() { return buf; }
byte length() { return fill; }
void reset() { fill = 0; }
virtual void write(uint8_t ch)
{ if (fill < sizeof buf) buf[fill++] = ch; }
private:
byte fill, buf[RF12_MAXDATA];
};
byte myId; // remember my own node ID
byte needToSend; // set when we want to send
word counter = 0; // incremented each second
MilliTimer sendTimer; // used to send once a second
PacketBuffer payload; // temp buffer to send out
static void handleInput (char c) {
switch (c) {
default:
break;
case 'w': // forward
counter = 2;
break;
case 's': // backward
counter = 3;
break;
case 'a': // left
counter = 4;
break;
case 'd': // right
counter = 5;
break;
}
}
void setup () {
Serial.begin(57600);
Serial.print("\n[rfStrings]");
myId = rf12_config();
}
void loop () {
// we intend to send once a second
if (sendTimer.poll(1000)) {
needToSend = 1;
Serial.print(" SEND ");
Serial.println(counter);
}
// can only send when the RF12 driver allows us to
handleInput(Serial.read());
if (needToSend && rf12_canSend()) {
needToSend = 0;
// fill the packet buffer with text to send
payload.print(counter);
// send out the packet
rf12_sendStart(0, payload.buffer(), payload.length());
payload.reset();
}
}