toneMultiple

はじめに

複数のピンに接続したスピーカーに、順に音を出します。

デジタルの6番ピンと7番ピン、8番ピンにスピーカが接続されている想定です。

プログラム

定義等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/*
  Multiple tone player

  Plays multiple tones on multiple pins in sequence

  circuit:
  - three 8 ohm speakers on digital pins 6, 7, and 8

  created 8 Mar 2010
  by Tom Igoe
  based on a snippet from Greg Borenstein

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMultiple
*/
 

コメントだけで特に何もしていません。

setup()

18
19
20
21
void setup() {

}
 

何もしません。何もしない場合でもsetup()を定義しておく必要があります。

loop()

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void loop() {
  // turn off tone function for pin 8:
  noTone(8);
  // play a note on pin 6 for 200 ms:
  tone(6, 440, 200);
  delay(200);

  // turn off tone function for pin 6:
  noTone(6);
  // play a note on pin 7 for 500 ms:
  tone(7, 494, 500);
  delay(500);

  // turn off tone function for pin 7:
  noTone(7);
  // play a note on pin 8 for 300 ms:
  tone(8, 523, 300);
  delay(300);
}

tone()を使って音を出します。delay()で音と音の間に少し時間を入れています。異なるピンでtone()を使う前には、noTone()を呼び出しておく必要があります。

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system