Arduinoで遊ぶページ
Arduinoで遊んだ結果を残すページです
p12_KnockLock

はじめに

圧電素子からの入力でサーボを制御します。

プログラム

定義等

 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
/*
  Arduino Starter Kit example
  Project 12 - Knock Lock

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

  Parts required:
  - 1 megohm resistor
  - 10 kilohm resistor
  - three 220 ohm resistors
  - piezo
  - servo motor
  - push button
  - one red LED
  - one yellow LED
  - one green LED
  - 100 uF capacitor

  created 18 Sep 2012
  by Scott Fitzgerald
  Thanks to Federico Vanzati for improvements

  http://www.arduino.cc/starterKit

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

// import the library
#include <Servo.h>
// create an instance of the Servo library
Servo myServo;

const int piezo = A0;      // pin the piezo is attached to
const int switchPin = 2;    // pin the switch is attached to
const int yellowLed = 3;    // pin the yellow LED is attached to
const int greenLed = 4;    // pin the green LED is attached to
const int redLed = 5;   // pin the red LED is attached to

// variable for the piezo value
int knockVal;
// variable for the switch value
int switchVal;

// variables for the high and low limits of the knock value
const int quietKnock = 10;
const int loudKnock = 100;

// variable to indicate if locked or not
bool locked = false;
// how many valid knocks you've received
int numberOfKnocks = 0;
 

Servoライブラリを使うためにServo.hをインクルードしています。

その後、変数を定義しています。

setup()

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
void setup() {
  // attach the servo to pin 9
  myServo.attach(9);

  // make the LED pins outputs
  pinMode(yellowLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);

  // set the switch pin as an input
  pinMode(switchPin, INPUT);

  // start serial communication for debugging
  Serial.begin(9600);

  // turn the green LED on
  digitalWrite(greenLed, HIGH);

  // move the servo to the unlocked position
  myServo.write(0);

  // print status to the Serial Monitor
  Serial.println("the box is unlocked!");
}
 

myServo.attach()で、9番ピンに接続したサーボを利用可能状態にします。

pinMode()を使い、yellowLEDとredLed、greenLedを出力モードに、switchPinを入力モードに設定します。

Serial.begin()でシリアル通信の初期化を行います。通信速度は9600bpsです。

digitalWrite()を使い、greenLedをHIGHにし、緑色のLEDを点灯します。

myServo.write()で、鍵を開いた状態にします。

Serial.print()で、文字列をシリアルコンソールに表示します。

loop()

 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
void loop() {

  // if the box is unlocked
  if (locked == false) {

    // read the value of the switch pin
    switchVal = digitalRead(switchPin);

    // if the button is pressed, lock the box
    if (switchVal == HIGH) {
      // set the locked variable to "true"
      locked = true;

      // change the status LEDs
      digitalWrite(greenLed, LOW);
      digitalWrite(redLed, HIGH);

      // move the servo to the locked position
      myServo.write(90);

      // print out status
      Serial.println("the box is locked!");

      // wait for the servo to move into position
      delay(1000);
    }
  }

  // if the box is locked
  if (locked == true) {

    // check the value of the piezo
    knockVal = analogRead(piezo);

    // if there are not enough valid knocks
    if (numberOfKnocks < 3 && knockVal > 0) {

      // check to see if the knock is in range
      if (checkForKnock(knockVal) == true) {

        // increment the number of valid knocks
        numberOfKnocks++;
      }

      // print status of knocks
      Serial.print(3 - numberOfKnocks);
      Serial.println(" more knocks to go");
    }

    // if there are three knocks
    if (numberOfKnocks >= 3) {
      // unlock the box
      locked = false;

      // move the servo to the unlocked position
      myServo.write(0);

      // wait for it to move
      delay(20);

      // change status LEDs
      digitalWrite(greenLed, HIGH);
      digitalWrite(redLed, LOW);
      Serial.println("the box is unlocked!");

      numberOfKnocks = 0;
    }
  }
}
 

lockedがfalseのときの処理です。

digitalRead()を使ってSwitchPinの値をswitchValに代入します。

switchValがHIGHの場合、

lockedがtrueのときの処理です。

  • analogRead()を使いpiezoの値を読み取る
  • numberOfKnocksが3未満かつknockValが0より大きいときには、
    • checkForKnock()を呼び出し、戻り値がtrueであれば、nuberOfKnocksを1増やし、
    • Serial.print()Serial.println()でシリアルコンソールに文字列を表示します。
  • numberOfKnocksが3以上になれば、

checkForKnock()

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// this function checks to see if a detected knock is within max and min range
bool checkForKnock(int value) {
  // if the value of the knock is greater than the minimum, and larger
  // than the maximum
  if (value > quietKnock && value < loudKnock) {
    // turn the status LED on
    digitalWrite(yellowLed, HIGH);
    delay(50);
    digitalWrite(yellowLed, LOW);
    // print out the status
    Serial.print("Valid knock of value ");
    Serial.println(value);
    // return true
    return true;
  }
  // if the knock is not within range
  else {
    // print status
    Serial.print("Bad knock value ");
    Serial.println(value);
    // return false
    return false;
  }
}

valueがquietKnockより大きくloudKnockより小さいときは、

valueがquietKnock以下あるいはloudKnock以上のときは、

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.13

最終更新日

June 19, 2020

inserted by FC2 system