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