// the setupfunction runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(12, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
38
Blinkのコード
// the setupfunction runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(12, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
40
Blinkのコード
// の行はコメント。
プログラムの挙動には影響しない。
どのような処理をしているか等のメモを書く
範囲指定の場合は、/* ∼ */
41.
// the setupfunction runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(12, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
41
Blinkのコード
{ } で囲まれた範囲が、setup()
という名前で呼び出される。
Arduinoの起動時に1回呼ばれ
るので、初期化処理を行う。
42.
// the setupfunction runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(12, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
42
Blinkのコード
pinModeは、ピンの使い方を指定する関数。
12番ピンをOUTPUT(出力)として使う。
43.
// the setupfunction runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(12, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
43
Blinkのコード
loop()は、繰り返し呼び出される関数。
センサーの読み取りやLEDの点等など、主要
な機能はここに書く。
44.
// the setupfunction runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(12, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
44
Blinkのコード
digitalWrite()は、指定したピンの状態を設定します。
HIGHは5Vを流し、LOWは電圧をかけない。
45.
// the setupfunction runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(12, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
45
Blinkのコード
delay()は、指定された時間だけ処理を止める。
時間の単位はミリ秒。1ミリ秒は1/1000秒。
52
スケッチの説明
const int buttonPin= 2;
const int ledPin = 12;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
buttonPinを2番ピン
ledPinを12番ピンと設定する
53.
53
スケッチの説明
const int buttonPin= 2;
const int ledPin = 12;
// variables will change:
int buttonState = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
buttonPinの状態を記憶する。
HIGH = 1, LOW = 0
54.
54
スケッチの説明
const int buttonPin= 2;
const int ledPin = 12;
// variables will change:
int buttonState = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
ledPinを出力モードに
buttonPinを入力モードに設定
55.
55
スケッチの説明
void loop() {
buttonState= digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
buttonPinの状態を読み取り、
buttonStateへ格納
56.
56
スケッチの説明
void loop() {
buttonState= digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
buttonStateがHIGHならば、
ledPinをHIGH(LEDを点灯)
57.
57
スケッチの説明
void loop() {
buttonState= digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
buttonStateがHIGHでなければ、
ledPinをLOW(LEDを消灯)