More Related Content
Similar to 小ゼミ進捗 0615 (20)
小ゼミ進捗 0615
- 6. 動作プログラム
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
}
int reading = 0;
void loop() {
// step 1: instruct sensor to read echoes
Wire.beginTransmission(112);
Wire.write(byte(0x00));
Wire.write(byte(0x50));
Wire.endTransmission();
// step 2: wait for readings to happen
delay(70);
// step 3: instruct sensor to return a particular echo
reading
Wire.beginTransmission(112);
Wire.write(byte(0x02));
Wire.endTransmission();
// step 4: request reading from sensor
Wire.requestFrom(112, 2);
// step 5: receive reading from sensor
if (2 <= Wire.available()) {
reading = Wire.read();
reading = reading << 8;
reading |= Wire.read();
Serial.println(reading);
}
delay(250);
}
- 9. サーボモータ SG90
#include<Servo.h>
Servo servo1;
void setup() {
servo1.attach(4);
}
void loop() { //ループ関数提起
servo1.write(40); //初期角度指定
Delay(2000); //次の移動までの時間差
servo1.write(90); //2番目の角度指定
Delay(2000); //次の移動までの時間差
servo1.write(140); //3番目角度の指定
Delay(2000); //次の移動までの時間差
servo1.write(90); //4番目角度の指定
Delay(2000); //次の移動までの時間差
}
- 12. Arduino タイマーライブラリ
・Timer0 : 8bitのタイマーでArduinoの時間管理を用途として利用される.
delay(),mills(),micros()などがあり,UNOでは5.6番ピンで利用する.
・Timer1 : 16bitのタイマーでUNOではServoライブラリと9.10番ピンのPWMで利用する.
・Timer2 : 8bitのタイマーでUNOではtone()と3.11番ピンのPWMで利用する.
・・・上記のTimerの説明にある通り,AVRタイマー機能はArduinoの
各種ライブラリによって利用されており,タイマーライブラリを使用する際は
利用されていないタイマーを使うなど競合を避けなければならない.
PWMもまた,タイマーを利用しているため同時に使用することができない.