-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm.html
228 lines (224 loc) · 6.45 KB
/
pwm.html
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html>
<html lang="ja">
<head>
<title>PWMサンプル - RBoardDocument</title>
<link rel="shortcut icon" href="./image/bug.ico">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-176702602-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-176702602-1');
</script>
<link rel="stylesheet" type="text/css" href="main.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/styles/a11y-dark.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/highlight.min.js"></script>
<script type="text/javascript" src="./style.js"></script>
<link href="https://use.fontawesome.com/releases/v5.11.0/css/all.css" rel="stylesheet">
</head>
<body>
<div id="header"><h1> PWMサンプル </h1></div>
<div id="main">
<h3 class="link" id="pwm_out">PWM(外部LED)</h3>
<pre><code class="ruby">
PWM.new()
PWM.pin(16)
PWM.start(2)
PWM.cycle(0x95A,4)
while true
PWM.rate(80,2)
sleep 1
PWM.rate(40,2)
sleep 1
PWM.rate(20,2)
sleep 1
end
</code></pre>
<hr id="first_boader">
<h3 class="link" id="pwm_buzzer">圧電ブザー</h3>
http://wiki.seeedstudio.com/Grove-Buzzer/
<pre><code class="ruby">
PWM.new()
PWM.pin(16)
PWM.start(2)
while true
PWM.cycle(0x95A,4)
sleep 1
PWM.cycle(0x84D,4)
sleep 1
PWM.cycle(0x76A,4)
sleep 1
PWM.cycle(0x6EF,4)
sleep 1
PWM.cycle(0x639,4)
sleep 1
PWM.cycle(0x58C,4)
sleep 1
PWM.cycle(0x4F3,4)
sleep 1
end
</code></pre>
<h4>簡易mml再生</h4>
<pre><code class="ruby">
TBL_FREQ = [0x95A, 0x84D, 0x76A, 0x6EF, 0x639, 0x58C, 0x4F3,
0x4AB, 0x428, 0x3B4, 0x37F, 0x31D, 0x2BF, 0x278]
TBL_LENGTH = {1=>2000, 2=>1000, 4=>500, 8=>250}
PWM.new()
PWM.pin(16)
PWM.start(2)
def tone( freq )
if freq == 0
PWM.cycle(0,4)
return
end
PWM.cycle(freq ,4)
end
def play( mml )
mnote = nil
mlength = 4
octabe = 0
mml.each_char {|ch|
# 音階指示か?
if "CDEFGABR".index(ch)
play_sub( mnote, octabe, mlength )
mnote = ch
next
end
# 音長か?
if TBL_LENGTH[ch.to_i]
mlength = ch.to_i
next
end
# オクターブ上下か?
if ch == "<" || ch == ">"
play_sub( mnote, octabe, mlength )
if ch == "<"
octabe += 1
else
octabe -= 1
end
mnote = nil
next
end
}
play_sub( mnote, octabe, mlength )
end
def play_sub( mnote, octabe, mlength )
if !mnote
return
end
if mnote == "R"
sleep_ms( TBL_LENGTH[mlength] || 1 )
return
end
idx = "CDEFGAB".index(mnote)
if idx
idx = octabe * 7 + idx
if TBL_FREQ[idx]
tone( TBL_FREQ[idx] )
#display( idx )
sleep_ms( TBL_LENGTH[mlength] || 1 )
tone( 0 )
end
return
end
end
mml3 = "<C8DE4>C8DE4<C8DE4>C8DE4<C8DE4>C8DE4<E8FG4EDC A8AAFDDEFGGGEC4R8 EF4>A<C8>BAB<C2"
play mml3
</code></pre>
<hr id="first_boader">
<h3>SG90(サーボモータ)</h3>
http://akizukidenshi.com/catalog/g/gM-08761/
<pre><code class="ruby">
PWM.new()
PWM.pin(18)
PWM.start(4)
PWM.cycle(0xC35,6)
while true
PWM.rate(3,4)
sleep 1
PWM.rate(6,4)
sleep 1
PWM.rate(12,4)
sleep 1
end
</code></pre>
</div>
<div id="submenu">
<div id="submenu_header">もくじ</div>
<ul id="submenu_body">
<i class="fas fa-home"></i><a href="./index.html">HOME</a>
<li><details>
<summary>プログラム書き込み</summary>
<ul id="subuul">
<li><a href="./ide_install.html">mruby/c IDEインストール</a></li>
<li><a href="./write_program.html">PG書き込み</a></li>
</ul>
</details></li>
<li><details>
<summary>ファームウェアアップデート</summary>
<ul id="subuul">
<li><a href="./update.html">アップデート手順</a></li>
</ul>
</details></li>
<li><details>
<summary>ボード一覧</summary>
<ul id="subuul">
<li><a href="./board_list.html">ボード一覧</a></li>
</ul>
</details></li>
<li><details>
<summary>RBoardにおける各クラス</summary>
<ul id="subuul">
<li><a href="./class.html">各クラス</a></li>
<li><a href="./mrubyc_class.html">mruby/c IO 各クラス</a></li>
</ul>
</details></li>
<li><details>
<summary>サンプルプログラム</summary>
<ul id="subuul">
<li><a href="./adc.html">ADCサンプル</a></li>
<li><a href="./i2c.html">I2Cサンプル</a></li>
<li><a href="./pwm.html">PWMサンプル</a></li>
<li><a href="./uart.html">UARTサンプル</a></li>
<li><a href="./etc.html">その他サンプル</a></li>
</ul>
</details></li>
<li><details>
<summary>その他</summary>
<ul id="subuul">
<li><a href="./sensor_module.html">対応センサーモジュール</a></li>
<li><a href="./console.html">ターミナルソフトの設定</a></li>
</ul>
</details></li>
<li><details>
<summary>チュートリアル</summary>
<ol id="subuol">
<li><a href="./tutorial_mrubyc.html">mruby/cとは?</a></li>
<li><a href="./tutorial_led.html">LEDを制御する</a></li>
<li><a href="./tutorial_rgb_led.html">フルカラーLED</a></li>
<li><a href="./tutorial_reed_switch.html">ドア開閉センサ</a></li>
<li><a href="./tutorial_buzzer.html">圧電ブザー</a></li>
<li><a href="./tutorial_cds.html">光センサ</a></li>
<li><a href="./tutorial_temp.html">温度センサ</a></li>
</ol>
</details></li>
</ul>
<a href="https://www.rubyworld-conf.org/" target="_blank"><img src="rwc2020_banner_a02.png"></a>
</div>
</div>
</body>
</html>