RobotControl Control your Robot

Control your Robot

This example shows you how to command your Robot using a remote control. 
You have need of a infrared sensor and a Sony compatible remote controller. 
Now connect the IR sensor to the robot, execute the detailed instructions shown down and control your Robot.

Hardware Required

  • Arduino Robot
  • SD-card
  • Infrared sensor
  • Cable to connect the sensor
  • Remote control

Instruction

  • Connect the IR sensor to TKD2 connector on the top board, using the cable.
  • Fix the sensor on the board.
  • Upload this sketch.
  • Unplug USB.
  • Turn on power.
  • After that the sketch is uploaded, use the remote control to send it commands.
  • Press the buttons that you have choosed and move the robot.
  • You can see received signals and commands on the LCD.

Command your Arduino Robot via Remote Control

Code

The sketch uses differents libraries that you have need to install on your IDE as the IR-Remote library by Ken Shirriff. 
If you want to customize your commands: 
Press the buttons on remote control and write down the values showing up on LCD. 
Locate these lines in the example code:

  1. define IR_CODE_FORWARD 0x2C9B
  2. define IR_CODE_BACKWARDS 0x6C9B
  3. define IR_CODE_TURN_LEFT 0xD4B8F
  4. define IR_CODE_TURN_RIGHT 0x34B8F

Change the values here to the ones you saw on the screen.

The complete code and its detailed description are shown down.

 
 
/* 08 Remote Control
 
Circuit:
* Arduino Robot
* Connect the IRreceiver to D2
* Remote control from Robot sensor pack
 
based on the IRremote library
by Ken Shirriff
http://arcfn.com
 
created 1 May 2013
by X. Yang
modified 12 May 2013
by D. Cuartielles
 
This example is in the public domain
*/
 
// include the necessary libraries
#include <IRremote.h>
#include <IRremoteTools.h>
#include <ArduinoRobot.h>
#include <Wire.h>
#include <SPI.h>
 
// Define a few commands from your remote control
#define IR_CODE_FORWARD 284154405
#define IR_CODE_BACKWARDS 284113605
#define IR_CODE_TURN_LEFT 284129925
#define IR_CODE_TURN_RIGHT 284127885
#define IR_CODE_CONTINUE -1
 
boolean isActing = false; //If the robot is executing command from remote
long timer;
const long TIME_OUT = 150;
 
void setup() {
// initialize the Robot, SD card, display, and speaker
Serial.begin(9600);
Robot.begin();
Robot.beginTFT();
Robot.beginSD();
 
// print some text to the screen
beginIRremote(); // Start the receiver
}
 
void loop() {
// if there is an IR command, process it
if (IRrecived()) {
  processResult();
  resumeIRremote(); // resume receiver
}
 
//If the robot does not receive any command, stop it
if (isActing && (millis() - timer >= TIME_OUT)) {
  Robot.motorsStop();
  isActing = false;
}
}
void processResult() {
unsigned long res = getIRresult();
switch (res) {
  case IR_CODE_FORWARD:
    changeAction(1, 1); //Move the robot forward
    break;
  case IR_CODE_BACKWARDS:
    changeAction(-1, -1); //Move the robot backwards
    break;
  case IR_CODE_TURN_LEFT:
    changeAction(-0.5, 0.5); //Turn the robot left
    break;
  case IR_CODE_TURN_RIGHT:
    changeAction(0.5, -0.5); //Turn the robot Right
    break;
  case IR_CODE_CONTINUE:
    timer = millis(); //Continue the last action, reset timer
    break;
}
}
void changeAction(float directionLeft, float directionRight) {
Robot.motorsWrite(255 * directionLeft, 255 * directionRight);
timer = millis();
isActing = true;
}