source: rtos_arduino/trunk/arduino_lib/libraries/Robot_Control/examples/explore/R08_Remote_Control/R08_Remote_Control.ino@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 2.2 KB
Line 
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
40boolean isActing = false; //If the robot is executing command from remote
41long timer;
42const long TIME_OUT = 150;
43
44void 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
55void 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}
68void 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}
88void changeAction(float directionLeft, float directionRight) {
89 Robot.motorsWrite(255 * directionLeft, 255 * directionRight);
90 timer = millis();
91 isActing = true;
92}
93
Note: See TracBrowser for help on using the repository browser.