[136] | 1 | /* 08 Remote Control
|
---|
| 2 |
|
---|
| 3 | If you connect a IR receiver to the robot,
|
---|
| 4 | you can control it like a RC car.
|
---|
| 5 | Using the remote control comes with sensor
|
---|
| 6 | pack, You can make the robot move around
|
---|
| 7 | without even touching it!
|
---|
| 8 |
|
---|
| 9 | Circuit:
|
---|
| 10 | * Arduino Robot
|
---|
| 11 | * Connect the IRreceiver to D2
|
---|
| 12 | * Remote control from Robot sensor pack
|
---|
| 13 |
|
---|
| 14 | based on the IRremote library
|
---|
| 15 | by Ken Shirriff
|
---|
| 16 | http://arcfn.com
|
---|
| 17 |
|
---|
| 18 | created 1 May 2013
|
---|
| 19 | by X. Yang
|
---|
| 20 | modified 12 May 2013
|
---|
| 21 | by D. Cuartielles
|
---|
| 22 |
|
---|
| 23 | This example is in the public domain
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | // include the necessary libraries
|
---|
| 27 | #include <IRremote.h>
|
---|
| 28 | #include <IRremoteTools.h>
|
---|
| 29 | #include <ArduinoRobot.h>
|
---|
| 30 | #include <Wire.h>
|
---|
| 31 | #include <SPI.h>
|
---|
| 32 |
|
---|
| 33 | // Define a few commands from your remote control
|
---|
| 34 | #define IR_CODE_FORWARD 284154405
|
---|
| 35 | #define IR_CODE_BACKWARDS 284113605
|
---|
| 36 | #define IR_CODE_TURN_LEFT 284129925
|
---|
| 37 | #define IR_CODE_TURN_RIGHT 284127885
|
---|
| 38 | #define IR_CODE_CONTINUE -1
|
---|
| 39 |
|
---|
| 40 | boolean isActing = false; //If the robot is executing command from remote
|
---|
| 41 | long timer;
|
---|
| 42 | const long TIME_OUT = 150;
|
---|
| 43 |
|
---|
| 44 | void setup() {
|
---|
| 45 | // initialize the Robot, SD card, display, and speaker
|
---|
| 46 | Serial.begin(9600);
|
---|
| 47 | Robot.begin();
|
---|
| 48 | Robot.beginTFT();
|
---|
| 49 | Robot.beginSD();
|
---|
| 50 |
|
---|
| 51 | // print some text to the screen
|
---|
| 52 | beginIRremote(); // Start the receiver
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void loop() {
|
---|
| 56 | // if there is an IR command, process it
|
---|
| 57 | if (IRrecived()) {
|
---|
| 58 | processResult();
|
---|
| 59 | resumeIRremote(); // resume receiver
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | //If the robot does not receive any command, stop it
|
---|
| 63 | if (isActing && (millis() - timer >= TIME_OUT)) {
|
---|
| 64 | Robot.motorsStop();
|
---|
| 65 | isActing = false;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | void processResult() {
|
---|
| 69 | unsigned long res = getIRresult();
|
---|
| 70 | switch (res) {
|
---|
| 71 | case IR_CODE_FORWARD:
|
---|
| 72 | changeAction(1, 1); //Move the robot forward
|
---|
| 73 | break;
|
---|
| 74 | case IR_CODE_BACKWARDS:
|
---|
| 75 | changeAction(-1, -1); //Move the robot backwards
|
---|
| 76 | break;
|
---|
| 77 | case IR_CODE_TURN_LEFT:
|
---|
| 78 | changeAction(-0.5, 0.5); //Turn the robot left
|
---|
| 79 | break;
|
---|
| 80 | case IR_CODE_TURN_RIGHT:
|
---|
| 81 | changeAction(0.5, -0.5); //Turn the robot Right
|
---|
| 82 | break;
|
---|
| 83 | case IR_CODE_CONTINUE:
|
---|
| 84 | timer = millis(); //Continue the last action, reset timer
|
---|
| 85 | break;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | void changeAction(float directionLeft, float directionRight) {
|
---|
| 89 | Robot.motorsWrite(255 * directionLeft, 255 * directionRight);
|
---|
| 90 | timer = millis();
|
---|
| 91 | isActing = true;
|
---|
| 92 | }
|
---|
| 93 |
|
---|