1 | /* Robot Logo
|
---|
2 |
|
---|
3 | This sketch demonstrates basic movement of the Robot.
|
---|
4 | When the sketch starts, press the on-board buttons to tell
|
---|
5 | the robot how to move. Pressing the middle button will
|
---|
6 | save the pattern, and the robot will follow accordingly.
|
---|
7 | You can record up to 20 commands. The robot will move for
|
---|
8 | one second per command.
|
---|
9 |
|
---|
10 | This example uses images on an SD card. It looks for
|
---|
11 | files named "lg0.bmp" and "lg1.bmp" and draws them on the
|
---|
12 | screen.
|
---|
13 |
|
---|
14 | Circuit:
|
---|
15 | * Arduino Robot
|
---|
16 |
|
---|
17 | created 1 May 2013
|
---|
18 | by X. Yang
|
---|
19 | modified 12 May 2013
|
---|
20 | by D. Cuartielles
|
---|
21 |
|
---|
22 | This example is in the public domain
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include <ArduinoRobot.h> // include the robot library
|
---|
26 | #include <Wire.h>
|
---|
27 | #include <SPI.h>
|
---|
28 |
|
---|
29 | int commands[20]; // array for storing commands
|
---|
30 |
|
---|
31 | void setup() {
|
---|
32 | // initialize the Robot, SD card, and display
|
---|
33 | Robot.begin();
|
---|
34 | Robot.beginTFT();
|
---|
35 | Robot.beginSD();
|
---|
36 |
|
---|
37 | // draw "lg0.bmp" and "lg1.bmp" on the screen
|
---|
38 | Robot.displayLogos();
|
---|
39 | }
|
---|
40 |
|
---|
41 | void loop() {
|
---|
42 |
|
---|
43 | Robot.drawBMP("intro.bmp", 0, 0); //display background image
|
---|
44 |
|
---|
45 | iniCommands(); // remove commands from the array
|
---|
46 | addCommands(); // add commands to the array
|
---|
47 |
|
---|
48 | delay(1000); // wait for a second
|
---|
49 |
|
---|
50 | executeCommands(); // follow orders
|
---|
51 |
|
---|
52 | Robot.stroke(0, 0, 0);
|
---|
53 | Robot.text("Done!", 5, 103); // write some text to the display
|
---|
54 | delay(1500); // wait for a moment
|
---|
55 | }
|
---|
56 |
|
---|
57 | // empty the commands array
|
---|
58 | void iniCommands() {
|
---|
59 | for (int i = 0; i < 20; i++)
|
---|
60 | commands[i] = -1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | // add commands to the array
|
---|
64 | void addCommands() {
|
---|
65 | Robot.stroke(0, 0, 0);
|
---|
66 | // display text on the screen
|
---|
67 | Robot.text("1. Press buttons to\n add commands.\n\n 2. Middle to finish.", 5, 5);
|
---|
68 |
|
---|
69 | // read the buttons' state
|
---|
70 | for (int i = 0; i < 20;) { //max 20 commands
|
---|
71 | int key = Robot.keyboardRead();
|
---|
72 | if (key == BUTTON_MIDDLE) { //finish input
|
---|
73 | break;
|
---|
74 | } else if (key == BUTTON_NONE) { //if no button is pressed
|
---|
75 | continue;
|
---|
76 | }
|
---|
77 | commands[i] = key; // save the button to the array
|
---|
78 | PrintCommandI(i, 46); // print the command on the screen
|
---|
79 | delay(100);
|
---|
80 | i++;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | // run through the array and move the robot
|
---|
85 | void executeCommands() {
|
---|
86 | // print status to the screen
|
---|
87 | Robot.text("Excuting...", 5, 70);
|
---|
88 |
|
---|
89 | // read through the array and move accordingly
|
---|
90 | for (int i = 0; i < 20; i++) {
|
---|
91 | switch (commands[i]) {
|
---|
92 | case BUTTON_LEFT:
|
---|
93 | Robot.turn(-90);
|
---|
94 | break;
|
---|
95 | case BUTTON_RIGHT:
|
---|
96 | Robot.turn(90);
|
---|
97 | break;
|
---|
98 | case BUTTON_UP:
|
---|
99 | Robot.motorsWrite(255, 255);
|
---|
100 | break;
|
---|
101 | case BUTTON_DOWN:
|
---|
102 | Robot.motorsWrite(-255, -255);
|
---|
103 | break;
|
---|
104 | case BUTTON_NONE:
|
---|
105 | return;
|
---|
106 | }
|
---|
107 | // print the current command to the screen
|
---|
108 | Robot.stroke(255, 0, 0);
|
---|
109 | PrintCommandI(i, 86);
|
---|
110 | delay(1000);
|
---|
111 |
|
---|
112 | // stop moving for a second
|
---|
113 | Robot.motorsStop();
|
---|
114 | delay(1000);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | // convert the button press to a single character
|
---|
119 | char keyToChar(int key) {
|
---|
120 | switch (key) {
|
---|
121 | case BUTTON_LEFT:
|
---|
122 | return '<';
|
---|
123 | case BUTTON_RIGHT:
|
---|
124 | return '>';
|
---|
125 | case BUTTON_UP:
|
---|
126 | return '^';
|
---|
127 | case BUTTON_DOWN:
|
---|
128 | return 'v';
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | // display a command
|
---|
133 | void PrintCommandI(int i, int originY) {
|
---|
134 | Robot.text(keyToChar(commands[i]), i % 14 * 8 + 5, i / 14 * 10 + originY);
|
---|
135 | }
|
---|
136 |
|
---|