Hello,
I have 2 arduino's, 0018 version. The transmitting arduino is running some test code that is sending commands out through a Hope rf transceiver. The receiving arduino is getting the commands through a second Hope rf transceiver.
Can't remember were I found this receive end code. It works great decoding the commands directly from the serial monitor, Does not work at all when using the wireless transceivers.
Can someone tell me why this will not work over wireless? Thanks
"Transmitter"
*/
#define LineEnd '#'
void setup() {
Serial.begin(9600);
}
void loop() {
// read the analog input into a variable:
// print the result:
Serial.println("BR40#");
// wait 10 milliseconds for the analog-to-digital converter
// to settle after the last reading:
delay(3000);
Serial.println("BF40#");
delay(3000);
Serial.println("S140#");
delay(3000);
Serial.println("S199#");
delay(3000);
"Receiver"
#include <Servo.h>
#define PwmPinMotorA 3 // this has been moved from pin 11 on the original Ardumoto
#define PwmPinMotorB 11
#define DirectionPinMotorA 12
#define DirectionPinMotorB 13
#define SerialSpeed 9600
#define BufferLength 16
#define LineEnd '#'
#define PinServo1 7
#define PinServo2 8
#define PinServo3 9
#define DefaultServoPosition 90
Servo servo1;
Servo servo2;
Servo servo3;
char inputBuffer[BufferLength];
void setup()
{
// motor and servo pins must be outputs
pinMode(PwmPinMotorA, OUTPUT);
pinMode(PwmPinMotorB, OUTPUT);
pinMode(DirectionPinMotorA, OUTPUT);
pinMode(DirectionPinMotorB, OUTPUT);
pinMode(PinServo1, OUTPUT);
pinMode(PinServo2, OUTPUT);
pinMode(PinServo3, OUTPUT);
// attach servos and set servo-specific timing details
servo1.attach(PinServo1, 600, 2100); // Tower Pro SG90 servo
servo2.attach(PinServo2, 700, 2300); // Sanwa SRM-102 servo
servo3.attach(PinServo3, 600, 2100); // Tower Pro SG90 servo
// default servo positions
servo1.write(DefaultServoPosition);
servo2.write(DefaultServoPosition);
servo3.write(DefaultServoPosition);
Serial.begin(SerialSpeed);
}
// process a command string
void HandleCommand(char* input, int length)
{
Serial.println(input);
if (length < 2) { // not a valid command
return;
}
int value = 0;
// calculate number following command
if (length > 2) {
value = atoi(&input[2]);
}
int* command = (int*)input;
// check commands
// note that the two bytes are swapped, ie 'RA' means command AR
switch(*command) {
case 'FA':
// motor A forwards
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorA, HIGH);
break;
case 'RA':
// motor A reverse
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorA, LOW);
break;
case 'FB':
// motor B forwards
analogWrite(PwmPinMotorB, value);
digitalWrite(DirectionPinMotorB, LOW);
break;
case 'RB':
// motor B reverse
analogWrite(PwmPinMotorB, value);
digitalWrite(DirectionPinMotorB, HIGH);
break;
case '1S':
// servo1
servo1.write(value);
break;
case '2S':
// servo2
servo2.write(value);
break;
case '3S':
// servo 3
servo3.write(value);
break;
default:
Serial.println("unknown command");
break;
}
}
void loop()
{
// get a command string form the serial port
int inputLength = 0;
do {
while (!Serial.available()); // wait for input
inputBuffer[inputLength] = Serial.read(); // read it in
} while (inputBuffer[inputLength] != LineEnd && ++inputLength < BufferLength);
inputBuffer[inputLength] = 0; // add null terminator
HandleCommand(inputBuffer, inputLength);
}