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

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

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

File size: 3.9 KB
Line 
1/* Robot Inputs
2
3 This sketch shows you how to use the on-board
4 potentiometer and buttons as inputs.
5
6 Turning the potentiometer draws a clock-shaped
7 circle. The up and down buttons change the pitch,
8 while the left and right buttons change the tempo.
9 The middle button resets tempo and pitch.
10
11 Circuit:
12 * Arduino Robot
13
14 created 1 May 2013
15 by X. Yang
16 modified 12 May 2013
17 by D. Cuartielles
18
19 This example is in the public domain
20 */
21
22#include <ArduinoRobot.h>
23#include <Wire.h>
24#include <SPI.h>
25
26// default tempo and pitch of the music
27int tempo = 60;
28int pitch = 1000;
29
30void setup() {
31 // initialize the Robot, SD card, speaker, and display
32 Robot.begin();
33 Robot.beginTFT();
34 Robot.beginSpeaker();
35 Robot.beginSD();
36
37 // draw "lg0.bmp" and "lg1.bmp" on the screen
38 Robot.displayLogos();
39
40 // play a sound file
41 Robot.playFile("Melody.sqm");
42}
43
44void loop() {
45 // check the value of the buttons
46 keyDown(Robot.keyboardRead());
47
48 // check the value of the pot
49 drawKnob(Robot.knobRead());
50}
51
52// Draw the basic interface
53void renderUI() {
54 //fill the buttons blank
55 Robot.fill(255, 255, 255);
56 Robot.rect(53, 58, 13, 13); // left
57 Robot.rect(93, 58, 13, 13); // right
58 Robot.rect(73, 38, 13, 13); // up
59 Robot.circle(79, 64, 6); // middle
60 Robot.rect(73, 78, 13, 13); // down
61
62 //draw the knob
63 Robot.noFill();
64 Robot.circle(26, 116, 17); // knob
65
66 //draw the vertical bargraph
67 int fullPart = map(pitch, 200, 2000, 0, 58); //length of filled bargraph
68 Robot.fill(255, 255, 255);
69 Robot.rect(21, 30, 13, 58 - fullPart);
70 Robot.fill(0, 0, 255);
71 Robot.rect(21, 88 - fullPart, 13, fullPart); //58-fullPart+30
72
73 //draw the horizontal bargraph
74 fullPart = map(tempo, 20, 100, 0, 58); // length of filled bargraph
75 Robot.fill(255, 190, 0);
76 Robot.rect(53, 110, fullPart, 13);
77 Robot.fill(255, 255, 255);
78 Robot.rect(53 + fullPart, 110, 58 - fullPart, 13);
79}
80
81void keyDown(int keyCode) {
82 // use a static int so it is persistent over time
83 static int oldKey;
84 switch (keyCode) {
85 case BUTTON_LEFT:
86 //left button pressed, reduces tempo
87 tempo -= 5;
88 if (tempo < 20) tempo = 20; //lowest tempo 20
89 Robot.fill(255, 190, 0);
90
91 Robot.rect(53, 58, 13, 13);
92 break;
93 case BUTTON_RIGHT:
94 //right button pressed, increases tempo
95 tempo += 5;
96 if (tempo > 100) tempo = 100; //highest tempo 100
97 Robot.fill(255, 190, 0);
98 Robot.rect(93, 58, 13, 13);
99 break;
100 case BUTTON_UP:
101 //up button pressed, increases pitch
102 pitch += 120;
103 if (pitch > 2000) pitch = 2000;
104 Robot.fill(0, 0, 255);
105
106 Robot.rect(73, 38, 13, 13);
107 break;
108 case BUTTON_DOWN:
109 //down button pressed, reduces pitch
110 pitch -= 120;
111 if (pitch < 200) {
112 pitch = 200;
113 }
114 Robot.fill(0, 0, 255);
115
116 Robot.rect(73, 78, 13, 13);
117 break;
118 case BUTTON_MIDDLE:
119 //middle button pressed, resets tempo and pitch
120 tempo = 60;
121 pitch = 1000;
122 Robot.fill(160, 160, 160);
123
124 Robot.circle(79, 64, 6);
125 break;
126 case BUTTON_NONE:
127 //Only when the keys are released(thus BUTTON_NONE is
128 //encountered the first time), the interface will be
129 //re-drawn.
130 if (oldKey != BUTTON_NONE) {
131 renderUI();
132 }
133 break;
134 }
135 if (oldKey != keyCode) {
136 // change the song's tempo
137 Robot.tempoWrite(tempo);
138 // change the song's pitch
139 Robot.tuneWrite(float(pitch / 1000.0));
140 }
141 oldKey = keyCode;
142}
143
144//Draw a circle according to value
145//of the knob.
146void drawKnob(int val) {
147 static int val_old;
148 int r = map(val, 0, 1023, 1, 15);
149
150 //Only updates when the
151 //value changes.
152 if (val_old != r) {
153 Robot.noFill();
154
155 //erase the old circle
156 Robot.stroke(255, 255, 255);
157 Robot.circle(26, 116, r + 1);
158
159 //draw the new circle
160 Robot.stroke(255, 0, 255);
161 Robot.circle(26, 116, r);
162
163 Robot.stroke(0, 0, 0);
164
165 val_old = r;
166 }
167}
Note: See TracBrowser for help on using the repository browser.