Arduino Mega 2560 LESSON:1 Board上のLEDの点滅速度の調整

提供:yonewiki
2026年9月28日 (月) 15:08時点におけるYo-net (トーク | 投稿記録)による版 (ページの作成:「Arduino Mega 2560に戻る。 == '''概要''' ==  Arduino IDEのBoard上のLという表示のあるLEDの明滅速度を調整します。  Arduino IDEにはいくつかの既存*.inoプロジェクトファイルを保持しています。開いて編集して、アップロードすれば動きが変わるそうな。  でも保持しているinoファイルは読み取り専用ファイルになっているので、開いて、名前を付けて保…」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)

Arduino Mega 2560に戻る。

概要

 Arduino IDEのBoard上のLという表示のあるLEDの明滅速度を調整します。


 Arduino IDEにはいくつかの既存*.inoプロジェクトファイルを保持しています。開いて編集して、アップロードすれば動きが変わるそうな。


 でも保持しているinoファイルは読み取り専用ファイルになっているので、開いて、名前を付けて保存をして、名前を付けて保存をしたファイルを右クリックして表示されるファイルプロパティで読み取り専用のチェックを消して再読み込みをする必要があります。


 まずは既存のBlink.inoを開いてみましょう。


  • [File]-[Example]-[01.Basics]-[Blink]を選択します。


  • [File]-[Save As…]を選択して「MyBlink」というファイル名で保存します。


 WindowsではC:\Users\(UserID)\Documents\Arduino\MyBlinkにMyBlink.inoファイルが生成されるので、このファイルを右クリックしてファイルプロパティを開いて、読み取り専用属性のチェックを外して「OK」ボタンを押します。


 その上でもう一度MyBlinkファイルを読み込み直してファイル上書きができる状態でOpenしたいので


  • [File]-[Open Recent]から「MyBlink」を選択します。今度は以下のようなソースファイルが編集可能状態で開きます。


/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly. 

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://docs.arduino.cc/hardware/ 

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://docs.arduino.cc/built-in-examples/basics/Blink/
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // change state of the LED by setting the pin to the HIGH voltage level
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // change state of the LED by setting the pin to the LOW voltage level
  delay(1000);                      // wait for a second
}

とこのような感じで太字、赤文字で記したところが、点滅の速度に関係する命令部分です。ここの1000は1000msを意味していてdelay関数は与えられた引数のmsだけ停止するような処理になっています。


void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // change state of the LED by setting the pin to the HIGH voltage level
  delay(25);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // change state of the LED by setting the pin to the LOW voltage level
  delay(25);                      // wait for a second
}

のように変更して、画面の左上のツールバーの→ボタンを押すとアップロードされます。アップロード中もLEDが明滅する仕様ですが、アップロードが終わると、変更した状況に応じたLEDの明滅が開始されます。

 


 


 

Arduino Mega 2560に戻る。