Arduino/Arduinoの勉強はじめました
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[FrontPage]]
#contents
2013/05/12からのアクセス回数 &counter;
通勤時間を利用してManningのArduino in Actionを読み終わっ...
部品の代替方法を説明します。
[[Arduino in Action>http://www.manning.com/mevans/]]
は分かりやすく書かれたArduinoの教材です。
((まだMEAPというプレ・リリース版ですが、もうすぐ正式版が...
ここでは、3章と6章の紹介ですが、その他は、以下の記事で紹...
-- &new(Arduino/ArduinoInAction.Chap5); モータ制御
-- &new(Arduino/ArduinoInAction.Chap7); LCDを使う
-- &new(Arduino/ArduinoInAction.Chap8); イーサネット接続
** 3章 入力と出力 [#rd5433f6]
著者のJordan Hochenbaumさんが、Arduinoを使った新しい音楽...
所々に音を使った例が挿入されています。
*** アナログ入力 [#c9793e75]
Arduinoとブレッドボードはとても相性がよく、ブレッドボード...
最初の例題で使うアナログ入力では10KΩの半固定抵抗を使いま...
スイッチサイエンスからブレッドボードにそのままささる、
[[つまみの大きい半固定抵抗 10KΩ>http://www.switch-science...
が販売されているので、1個あると便利です。
&ref(10K.png);
さっそく図3.3にある回路をブレッドボードに組んでみましょう。
&ref(Fig3_3.png);
組み上がったブレッドボードは、以下の通りです。
Atmega32U4とArduinoのピン番号の対応は、[[Arduino/ATmega32...
&ref(Bread3_3.png);
*** スケッチを描いて、動かしてみよう [#e4fdbaec]
早速例題のスケッチを入力して、動かしてみます。
#pre{{
// 3.1 Time to get analog
int sensorPin = A0;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leon...
}
}
void loop() {
if (Serial) {
sensorValue = analogRead(sensorPin);
Serial.print("Sensor = ");
Serial.println(sensorValue, DEC);
delay(1000);
}
}
}}
Arduino IDEのツールメニューからシリアルモニターを選択する...
値として表示されます。
&ref(Out3_3.png);
*** 音の出す [#d03582bd]
音というとアナログ出力をイメージするかも知れませんが、Ard...
スピーカをデジタルピンに接続して音を出力します。
3.3章にでてくるピエゾ圧電素子を5枚接続して音を鳴らす例題...
代わりに
[[3x4のマトリックス式ボタンパッド>http://www.switch-sc...
を使用しました。
&ref(keypad.png);
このパッドは、Arduinoのライブラリー
[[Keypad>http://playground.arduino.cc/Code/Keypad]]
を使っています。
ダウンロードして展開したKeypadを、ユーザのホームディレク...
音の出力には、tone関数を使用するのですが、Arduino IDE 1.0...
Leonardで使用する場合には、Arduino IDEを1.0.1よりも新しい...
*** マトリックス式ボタンパッドの接続 [#s96ab282]
マトリックス式ボタンパッドからは、7本の線が出ており、コ...
これをArduinoのデジタルピンに以下の様に接続します。
- 1:右端列 - D5
- 2:中列 - D4
- 3:左端列 - D3
- 4:下端行 - D6
- 5:中下行 - D7
- 6:中上行 - D8
- 7:上端行 - D9
*** スケッチと動作確認 [#cde8b0b2]
スケッチは、以下の様になります。
#pre{{
#include <Keypad.h>
// Tone
int toneDuration = 40;
int speakerPin = 2;
int index = 0;
const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {6, 7, 8, 9}; //connect to the row p...
byte colPins[cols] = {3, 4, 5}; //connect to the column p...
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPin...
int tones[]={262,294,330,392,440}; // C, D, E, G, A
void setup() {
Serial.begin(9600);
}
// サンプル曲
// 123 123 4321232 123 123 4321231 4434554 33221
// 咲いた 咲いた チューリップの花が
// 並んだ 並んだ 赤白黄色 どの花みてもきれいだな
void loop() {
char key = keypad.getKey();
if (key != NO_KEY){
Serial.println(key);
if (key >= '1' && key <= '5') {
index = key - '1';
tone(speakerPin, tones[index], toneDuration);
}
}
}
}}
試しに、123 123 4321232と押してみて下さい。懐かしい曲が聞...
* 6章 物体の検出 [#jb37484f]
** 超音波センサー [#r7884511]
6章で最初に紹介されている超音波距離センサーは、Devantech ...
秋月でParallax Pingが2500円だったので、Amazonの「サインス...
279円と信じられない金額(送料は1個に付き300円)だったので...
&ref(HC-SR04.png);
Arduino in Actionの回路は図6.5のようになっていますが、
&ref(Fig6_5.png);
図ではSRF05を使っているので、HC-SR04との接続は以下の様に...
- VCC: 5V
- triger: D8
- echo: D9
- GBD: GND
*** スケッチ [#w93cc85c]
Arduino in Actionとはデバイスが異なるため、
[[Arduino and HC-SR04 ultrasonic sensor>http://trollmaker...
のサイトの情報を使ってスケッチを動かしました。
使ったスケッチは以下の通りです。
#pre{{
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 9 Trig to Arduino pin 8
More info at: http://goo.gl/kJ8Gl
*/
#define trigPin 8
#define echoPin 9
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
}}
上記サイトの説明では、pingは超音波を発信してそのエコーを...
例えば、\( \Delta t = 500 \) μ秒だとすると、その半分の 25...
音の速度cは、以下の式で求まります。
$$
c = 343.5 + 0.6*温度
$$
気温が20℃だとすると、 c = 331.5 + 0.6 * 20 = 343.5 m/s と...
単位をcm/μ秒に変換すると、
$$
c = 343.5 * 100/1000000 = 0.0345 cm / \mu s
$$
これを使うと\( \Delta t = 500 \)の距離は、D = 250 * 0.034...
スケッチで使われている29.1という値は、音の伝搬を距離当た...
$$
音の伝搬の割合 = 1 / 0.03435 = 29.1 \mu s
$$
パルスが戻ってくるまでの時間\(\Delta t \)が変数durationに...
$$
D = (\Delta t /2 ) / 29.1
$$
*** 動作確認 [#f8be4bcb]
実際に動作させると40cmくらいの距離でout of rangeとなりま...
&ref(Bread6_5.png);
** 赤外線センサー [#ab78b6e7]
地方に住んでいるとセンサーや部品を一つ一つ確認しながら購...
赤外線センサーの例を試しみてみました。
データシートでは、電圧と距離の関係が以下のようになってい...
&ref(Sharp-GP2Y0A02.png);
*** 回路とブレッドボード [#v85ea4da]
図6.8では、スピーカーを付けていますが、以下のスケッチでは...
ブレッドボードの配線もすごく簡単です。
&ref(Bread6_8.png);
*** 動作確認 [#jac4ca30]
以下のスケッチで、センサーの動作を確認しました。
#pre{{
int IRpin = 0; // anal...
float ratio = 5.0/1024;
void setup() {
Serial.begin(9600); // star...
}
void loop() {
float volts = analogRead(IRpin);
float distance = 65*pow(volts*ratio, -1.10); // work...
Serial.println(distance); // prin...
delay(100); // arbi...
}
}}
どうも、このセンサーArduino DuemilanoveとATmega32U4では、...
** 物体進入センサー [#bce7bd1c]
6章の最後に登場するのが、物体進入センサー Parallax PIRで...
今回は秋月で扱っている
[[Parallax PIRセンサー RevA>http://akizu...
を使用しました。((800円と手頃な値段も嬉しいです))
残念ながら、Arduino in Actionの例題では上手く動作しなかっ...
[[ArduinoのPIRsense code>http://playground.arduino.cc/Cod...
のサイトを参考に動作確認しました。
接続は、PIRセンサー:Arduinoで
- 赤:VCC(5V)
- 黒:GND
- 青:D3
&ref(PIR.png);
テストに使用したスケッチは、以下のとおりです
#pre{{
/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors o...
* Determines the beginning and end of continuous motion ...
*
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / ...
* @date: 3. September 2006
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonComm...
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
* The Parallax PIR Sensor is an easy to use digital infr...
* (http://www.parallax.com/detail.asp?product_id=555-280...
*
* The sensor's output pin goes to HIGH if motion is pres...
* However, even if motion is present it goes to LOW from...
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-pha...
* assuming continuous motion is present during these pha...
*
*/
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs ac...
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3; //the digital pin connected to the PIR...
int ledPin = 13;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes...
if(lockLow){
//makes sure we wait for a transition to LOW bef...
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes t...
if(takeLowTime){
lowIn = millis(); //save the time of the...
takeLowTime = false; //make sure this is on...
}
//if the sensor is low for more than the given pau...
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only execut...
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
}}
動かしてみると、以下の様に人を検出します。
このセンサーは人の動きを検出するため、人がいても動かない...
LOWの時間がpause時間(msec)続いたら検出を終了するように...
この辺は、先述のセンサーと異なるところです。
#pre{{
....................... done
SENSOR ACTIVE
---
motion detected at 30 sec
motion ended at 73 sec
---
motion detected at 78 sec
motion ended at 95 sec
---
motion detected at 107 sec
}}
** コメント [#jbdb3dbf]
#vote(おもしろかった[67],そうでもない[0],わかりずらい[20])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
終了行:
[[FrontPage]]
#contents
2013/05/12からのアクセス回数 &counter;
通勤時間を利用してManningのArduino in Actionを読み終わっ...
部品の代替方法を説明します。
[[Arduino in Action>http://www.manning.com/mevans/]]
は分かりやすく書かれたArduinoの教材です。
((まだMEAPというプレ・リリース版ですが、もうすぐ正式版が...
ここでは、3章と6章の紹介ですが、その他は、以下の記事で紹...
-- &new(Arduino/ArduinoInAction.Chap5); モータ制御
-- &new(Arduino/ArduinoInAction.Chap7); LCDを使う
-- &new(Arduino/ArduinoInAction.Chap8); イーサネット接続
** 3章 入力と出力 [#rd5433f6]
著者のJordan Hochenbaumさんが、Arduinoを使った新しい音楽...
所々に音を使った例が挿入されています。
*** アナログ入力 [#c9793e75]
Arduinoとブレッドボードはとても相性がよく、ブレッドボード...
最初の例題で使うアナログ入力では10KΩの半固定抵抗を使いま...
スイッチサイエンスからブレッドボードにそのままささる、
[[つまみの大きい半固定抵抗 10KΩ>http://www.switch-science...
が販売されているので、1個あると便利です。
&ref(10K.png);
さっそく図3.3にある回路をブレッドボードに組んでみましょう。
&ref(Fig3_3.png);
組み上がったブレッドボードは、以下の通りです。
Atmega32U4とArduinoのピン番号の対応は、[[Arduino/ATmega32...
&ref(Bread3_3.png);
*** スケッチを描いて、動かしてみよう [#e4fdbaec]
早速例題のスケッチを入力して、動かしてみます。
#pre{{
// 3.1 Time to get analog
int sensorPin = A0;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leon...
}
}
void loop() {
if (Serial) {
sensorValue = analogRead(sensorPin);
Serial.print("Sensor = ");
Serial.println(sensorValue, DEC);
delay(1000);
}
}
}}
Arduino IDEのツールメニューからシリアルモニターを選択する...
値として表示されます。
&ref(Out3_3.png);
*** 音の出す [#d03582bd]
音というとアナログ出力をイメージするかも知れませんが、Ard...
スピーカをデジタルピンに接続して音を出力します。
3.3章にでてくるピエゾ圧電素子を5枚接続して音を鳴らす例題...
代わりに
[[3x4のマトリックス式ボタンパッド>http://www.switch-sc...
を使用しました。
&ref(keypad.png);
このパッドは、Arduinoのライブラリー
[[Keypad>http://playground.arduino.cc/Code/Keypad]]
を使っています。
ダウンロードして展開したKeypadを、ユーザのホームディレク...
音の出力には、tone関数を使用するのですが、Arduino IDE 1.0...
Leonardで使用する場合には、Arduino IDEを1.0.1よりも新しい...
*** マトリックス式ボタンパッドの接続 [#s96ab282]
マトリックス式ボタンパッドからは、7本の線が出ており、コ...
これをArduinoのデジタルピンに以下の様に接続します。
- 1:右端列 - D5
- 2:中列 - D4
- 3:左端列 - D3
- 4:下端行 - D6
- 5:中下行 - D7
- 6:中上行 - D8
- 7:上端行 - D9
*** スケッチと動作確認 [#cde8b0b2]
スケッチは、以下の様になります。
#pre{{
#include <Keypad.h>
// Tone
int toneDuration = 40;
int speakerPin = 2;
int index = 0;
const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {6, 7, 8, 9}; //connect to the row p...
byte colPins[cols] = {3, 4, 5}; //connect to the column p...
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPin...
int tones[]={262,294,330,392,440}; // C, D, E, G, A
void setup() {
Serial.begin(9600);
}
// サンプル曲
// 123 123 4321232 123 123 4321231 4434554 33221
// 咲いた 咲いた チューリップの花が
// 並んだ 並んだ 赤白黄色 どの花みてもきれいだな
void loop() {
char key = keypad.getKey();
if (key != NO_KEY){
Serial.println(key);
if (key >= '1' && key <= '5') {
index = key - '1';
tone(speakerPin, tones[index], toneDuration);
}
}
}
}}
試しに、123 123 4321232と押してみて下さい。懐かしい曲が聞...
* 6章 物体の検出 [#jb37484f]
** 超音波センサー [#r7884511]
6章で最初に紹介されている超音波距離センサーは、Devantech ...
秋月でParallax Pingが2500円だったので、Amazonの「サインス...
279円と信じられない金額(送料は1個に付き300円)だったので...
&ref(HC-SR04.png);
Arduino in Actionの回路は図6.5のようになっていますが、
&ref(Fig6_5.png);
図ではSRF05を使っているので、HC-SR04との接続は以下の様に...
- VCC: 5V
- triger: D8
- echo: D9
- GBD: GND
*** スケッチ [#w93cc85c]
Arduino in Actionとはデバイスが異なるため、
[[Arduino and HC-SR04 ultrasonic sensor>http://trollmaker...
のサイトの情報を使ってスケッチを動かしました。
使ったスケッチは以下の通りです。
#pre{{
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 9 Trig to Arduino pin 8
More info at: http://goo.gl/kJ8Gl
*/
#define trigPin 8
#define echoPin 9
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
}}
上記サイトの説明では、pingは超音波を発信してそのエコーを...
例えば、\( \Delta t = 500 \) μ秒だとすると、その半分の 25...
音の速度cは、以下の式で求まります。
$$
c = 343.5 + 0.6*温度
$$
気温が20℃だとすると、 c = 331.5 + 0.6 * 20 = 343.5 m/s と...
単位をcm/μ秒に変換すると、
$$
c = 343.5 * 100/1000000 = 0.0345 cm / \mu s
$$
これを使うと\( \Delta t = 500 \)の距離は、D = 250 * 0.034...
スケッチで使われている29.1という値は、音の伝搬を距離当た...
$$
音の伝搬の割合 = 1 / 0.03435 = 29.1 \mu s
$$
パルスが戻ってくるまでの時間\(\Delta t \)が変数durationに...
$$
D = (\Delta t /2 ) / 29.1
$$
*** 動作確認 [#f8be4bcb]
実際に動作させると40cmくらいの距離でout of rangeとなりま...
&ref(Bread6_5.png);
** 赤外線センサー [#ab78b6e7]
地方に住んでいるとセンサーや部品を一つ一つ確認しながら購...
赤外線センサーの例を試しみてみました。
データシートでは、電圧と距離の関係が以下のようになってい...
&ref(Sharp-GP2Y0A02.png);
*** 回路とブレッドボード [#v85ea4da]
図6.8では、スピーカーを付けていますが、以下のスケッチでは...
ブレッドボードの配線もすごく簡単です。
&ref(Bread6_8.png);
*** 動作確認 [#jac4ca30]
以下のスケッチで、センサーの動作を確認しました。
#pre{{
int IRpin = 0; // anal...
float ratio = 5.0/1024;
void setup() {
Serial.begin(9600); // star...
}
void loop() {
float volts = analogRead(IRpin);
float distance = 65*pow(volts*ratio, -1.10); // work...
Serial.println(distance); // prin...
delay(100); // arbi...
}
}}
どうも、このセンサーArduino DuemilanoveとATmega32U4では、...
** 物体進入センサー [#bce7bd1c]
6章の最後に登場するのが、物体進入センサー Parallax PIRで...
今回は秋月で扱っている
[[Parallax PIRセンサー RevA>http://akizu...
を使用しました。((800円と手頃な値段も嬉しいです))
残念ながら、Arduino in Actionの例題では上手く動作しなかっ...
[[ArduinoのPIRsense code>http://playground.arduino.cc/Cod...
のサイトを参考に動作確認しました。
接続は、PIRセンサー:Arduinoで
- 赤:VCC(5V)
- 黒:GND
- 青:D3
&ref(PIR.png);
テストに使用したスケッチは、以下のとおりです
#pre{{
/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors o...
* Determines the beginning and end of continuous motion ...
*
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / ...
* @date: 3. September 2006
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonComm...
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
* The Parallax PIR Sensor is an easy to use digital infr...
* (http://www.parallax.com/detail.asp?product_id=555-280...
*
* The sensor's output pin goes to HIGH if motion is pres...
* However, even if motion is present it goes to LOW from...
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-pha...
* assuming continuous motion is present during these pha...
*
*/
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs ac...
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3; //the digital pin connected to the PIR...
int ledPin = 13;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes...
if(lockLow){
//makes sure we wait for a transition to LOW bef...
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes t...
if(takeLowTime){
lowIn = millis(); //save the time of the...
takeLowTime = false; //make sure this is on...
}
//if the sensor is low for more than the given pau...
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only execut...
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
}}
動かしてみると、以下の様に人を検出します。
このセンサーは人の動きを検出するため、人がいても動かない...
LOWの時間がpause時間(msec)続いたら検出を終了するように...
この辺は、先述のセンサーと異なるところです。
#pre{{
....................... done
SENSOR ACTIVE
---
motion detected at 30 sec
motion ended at 73 sec
---
motion detected at 78 sec
motion ended at 95 sec
---
motion detected at 107 sec
}}
** コメント [#jbdb3dbf]
#vote(おもしろかった[67],そうでもない[0],わかりずらい[20])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
ページ名:
SmartDoc