p15_HackingButtons

はじめに

フォトカプラをON/OFFします。

プログラム

定義等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*
  Arduino Starter Kit example
  Project 15 - Hacking Buttons

  This sketch is written to accompany Project 15 in the Arduino Starter Kit

  Parts required:
  - battery powered component
  - 220 ohm resistor
  - 4N35 optocoupler

  created 18 Sep 2012
  by Scott Fitzgerald

  https://store.arduino.cc/genuino-starter-kit

  This example code is part of the public domain.
*/

const int optoPin = 2; // the pin the optocoupler is connected to
 

変数を定義しています。

setup()

22
23
24
25
26
void setup() {
  // make the pin with the optocoupler an output
  pinMode(optoPin, OUTPUT);
}
 

pinMode()を使いoptoPindを出力モードに設定します。

loop()

27
28
29
30
31
32
33
34
void loop() {
  digitalWrite(optoPin, HIGH);  // pull pin 2 HIGH, activating the optocoupler

  delay(15); // give the optocoupler a moment to activate

  digitalWrite(optoPin, LOW);  // pull pin 2 low until you're ready to activate again
  delay(21000);                // wait for 21 seconds
}

digitalWrite()で、optoPinをHIGHにし、delay()で15ミリ秒待ちます。

digitalWrite()で、optoPinをLOWにし、delay()で21000ミリ秒待ちます。

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system