Note: this page refers to a product that is retired.

RobotInputs

Learn how to control the knob and the keyboard.

Inputs

This sketch shows you how to use the control board potentiometer and buttons as a keyboard. It turns the robot into a mobile music machine, have some fun with the music by pressing different buttons.

Hardware Required

  • Arduino Robot

Instruction

  1. Upload the example, you can keep USB plugged in once finished.

  2. After the starting screen, you will hear the music playing.

  3. On the screen, the vertical blue bar indicates the tempo of the music. The horizontal bar in orange shows the pitch. The cross shape in the middle represents the buttons, and the circle on the bottom left corresponds to the knob.

  4. Press different buttons, or try rotating the knob. You should see the interface on the screen change.

Try it out

Test the inputs on the robot
Test the inputs on the robot

Code

1/* Robot Inputs
2
3 This sketch shows you how to use the on-board
4
5 potentiometer and buttons as inputs.
6
7 Turning the potentiometer draws a clock-shaped
8
9 circle. The up and down buttons change the pitch,
10
11 while the left and right buttons change the tempo.
12
13 The middle button resets tempo and pitch.
14
15 Circuit:
16
17 * Arduino Robot
18
19 created 1 May 2013
20
21 by X. Yang
22
23 modified 12 May 2013
24
25 by D. Cuartielles
26
27 This example is in the public domain
28
29 */
30
31#include <ArduinoRobot.h>
32#include <Wire.h>
33
34// default tempo and pitch of the music
35int tempo = 60;
36int pitch = 1000;
37
38void setup() {
39
40 // initialize the Robot, SD card, speaker, and display
41
42 Robot.begin();
43
44 Robot.beginTFT();
45
46 Robot.beginSpeaker();
47
48 Robot.beginSD();
49
50 // draw "lg0.bmp" and "lg1.bmp" on the screen
51
52 Robot.displayLogos();
53
54 // play a sound file
55
56 Robot.playFile("Melody.sqm");
57}
58
59void loop() {
60
61 // check the value of the buttons
62
63 keyDown(Robot.keyboardRead());
64
65 // check the value of the pot
66
67 drawKnob(Robot.knobRead());
68}
69
70// Draw the basic interface
71void renderUI() {
72
73 //fill the buttons blank
74
75 Robot.fill(255, 255, 255);
76
77 Robot.rect(53, 58, 13, 13); // left
78
79 Robot.rect(93, 58, 13, 13); // right
80
81 Robot.rect(73, 38, 13, 13); // up
82
83 Robot.circle(79, 64, 6); // middle
84
85 Robot.rect(73, 78, 13, 13); // down
86
87 //draw the knob
88
89 Robot.noFill();
90
91 Robot.circle(26, 116, 17); // knob
92
93 //draw the vertical bargraph
94
95 int fullPart = map(pitch, 200, 2000, 0, 58); //length of filled bargraph
96
97 Robot.fill(255, 255, 255);
98
99 Robot.rect(21, 30, 13, 58 - fullPart);
100
101 Robot.fill(0, 0, 255);
102
103 Robot.rect(21, 88 - fullPart, 13, fullPart); //58-fullPart+30
104
105 //draw the horizontal bargraph
106
107 fullPart = map(tempo, 20, 100, 0, 58); // length of filled bargraph
108
109 Robot.fill(255, 190, 0);
110
111 Robot.rect(53, 110, fullPart, 13);
112
113 Robot.fill(255, 255, 255);
114
115 Robot.rect(53 + fullPart, 110, 58 - fullPart, 13);
116}
117
118void keyDown(int keyCode) {
119
120 // use a static int so it is persistent over time
121
122 static int oldKey;
123
124 switch (keyCode) {
125
126 case BUTTON_LEFT:
127
128 //left button pressed, reduces tempo
129
130 tempo -= 5;
131
132 if (tempo < 20) {
133
134 tempo = 20; //lowest tempo 20
135
136 }
137
138 Robot.fill(255, 190, 0);
139
140 Robot.rect(53, 58, 13, 13);
141
142 break;
143
144 case BUTTON_RIGHT:
145
146 //right button pressed, increases tempo
147
148 tempo += 5;
149
150 if (tempo > 100) {
151
152 tempo = 100; //highest tempo 100
153
154 }
155
156 Robot.fill(255, 190, 0);
157
158 Robot.rect(93, 58, 13, 13);
159
160 break;
161
162 case BUTTON_UP:
163
164 //up button pressed, increases pitch
165
166 pitch += 120;
167
168 if (pitch > 2000) {
169
170 pitch = 2000;
171
172 }
173
174 Robot.fill(0, 0, 255);
175
176 Robot.rect(73, 38, 13, 13);
177
178 break;
179
180 case BUTTON_DOWN:
181
182 //down button pressed, reduces pitch
183
184 pitch -= 120;
185
186 if (pitch < 200) {
187
188 pitch = 200;
189
190 }
191
192 Robot.fill(0, 0, 255);
193
194 Robot.rect(73, 78, 13, 13);
195
196 break;
197
198 case BUTTON_MIDDLE:
199
200 //middle button pressed, resets tempo and pitch
201
202 tempo = 60;
203
204 pitch = 1000;
205
206 Robot.fill(160, 160, 160);
207
208 Robot.circle(79, 64, 6);
209
210 break;
211
212 case BUTTON_NONE:
213
214 //Only when the keys are released(thus BUTTON_NONE is
215
216 //encountered the first time), the interface will be
217
218 //re-drawn.
219
220 if (oldKey != BUTTON_NONE) {
221
222 renderUI();
223
224 }
225
226 break;
227
228 }
229
230 if (oldKey != keyCode) {
231
232 // change the song's tempo
233
234 Robot.tempoWrite(tempo);
235
236 // change the song's pitch
237
238 Robot.tuneWrite(float(pitch / 1000.0));
239
240 }
241
242 oldKey = keyCode;
243}
244
245//Draw a circle according to value
246//of the knob.
247void drawKnob(int val) {
248
249 static int val_old;
250
251 int r = map(val, 0, 1023, 1, 15);
252
253 //Only updates when the
254
255 //value changes.
256
257 if (val_old != r) {
258
259 Robot.noFill();
260
261 //erase the old circle
262
263 Robot.stroke(255, 255, 255);
264
265 Robot.circle(26, 116, r + 1);
266
267 //draw the new circle
268
269 Robot.stroke(255, 0, 255);
270
271 Robot.circle(26, 116, r);
272
273 Robot.stroke(0, 0, 0);
274
275 val_old = r;
276
277 }
278}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.