Now that the robot can respond to commands programmatically, the next step is to get the robot to listen for and respond to wireless commands. RF communication will be accomplished using the same library (
VirtualWire) and components as my previous post, entitled
Arduino Weatherstation – RF Communication.
I'll split this part into two posts. The first will go over how to make the transmitter. The second will detail how to get the robot to respond to the transmitter's commands.
The Transmitter
Two components are needed, a 433 MHz transmitter and an Arduino. Connect the components as follows:
DATA --> Arduino pin 12
VCC --> Arduino 5V
GND --> Arduino GND
The transmitter operates as follows: First, the user opens the Serial monitor in the Arduino IDE and enters one of five commands:
- 0 - Stop
- 1 - Move forward
- 2 - Move backward
- 3 - Turn clockwise
- 4 - Turn counterclockwise
Second, this command will be transmitted to the robot, which will respond to the command.
Show Code
// Transmitter using the VirtualWire library
// Author: Zac DeMeo
#include <VirtualWire.h>
const byte TXBURST = 1;
const int TXRATE = 2000;
const int SERIALBAUD = 9600;
String input;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(SERIALBAUD);
Serial.println("Commands:");
Serial.println("0 - Stop");
Serial.println("1 - Move forward");
Serial.println("2 - Move backward");
Serial.println("3 - Turn clockwise");
Serial.println("4 - Turn counterclockwise");
Serial.println("");
Serial.println("Enter command:");
vw_setup(TXRATE);
}
void loop() {
}
void serialEvent() {
while(Serial.available()) {
char inputChar = Serial.read();
if (inputChar == '\n') {
transmit();
break;
}
input += inputChar;
}
}
void transmit() {
char buf[20];
buf[0] = '';
// Copy Serial command into string
input.toCharArray(buf, 20);
input = "";
// Send command TXBURST times
for (int i = 0; i < TXBURST; i++) {
digitalWrite(LED_BUILTIN, true); // Turn on onboard LED
vw_send((uint8_t *)buf, strlen(buf));
vw_wait_tx();
}
Serial.println("Sent " + String(buf)); // Print command that was sent
digitalWrite(LED_BUILTIN, false); // Turn off onboard LED
delay(40);
}
How It Works
Open the Arduino Serial Monitor. Make sure that the drop-down menu next to the baud rate is set to "Newline." Enter a command,
0-4. The
serialEvent event will fire, initiating the
serialEvent() function. This function reads whatever was typed into the Serial Monitor into a String, one character at a time. When the line break character is reached, the contents of the String are transmitted. Refer to the
transmitter.pde example that comes with VirtualWire for how to setup and transmit data. This sketch is simply a modification of that example. Note that VirtualWire requires a C-string, so
toCharArray must be called on the Serial data to transmit.
The
TXBURST variable will repeat the transmission however many number of times the user desires. This might be useful for for long-distance communication, where checksum validation might fail on the robot.