Last active
August 29, 2018 01:15
-
-
Save likersacademia/b8ac42c745b8f60c038bccd3ce8f2dbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 15センチ以上だと、モーターが回転し、ブザーが鳴るプログラム | |
#include <Servo.h> //サーボモーターを動かす関数を用意します | |
#define echoPin 11 //距離センサーの「echo」が11番ピン | |
#define trigPin 12 //距離センサーの「trig」が12番ピン | |
#define buzzerPin 2 //ブザーが2番ピン | |
#define moterPin 3 //モーターが3番ピン | |
double Duration = 0; //受信した間隔 | |
double Distance = 0; //距離 | |
Servo myservo; | |
void setup() { | |
pinMode(echoPin,INPUT); | |
pinMode(trigPin,OUTPUT); | |
myservo.attach(moterPin,700,2300); | |
pinMode(buzzerPin, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); //超音波を出力 | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
Duration = pulseIn(echoPin, HIGH); //センサからの入力 | |
if (Duration > 0) { | |
Duration = Duration/2; //往復距離を半分 | |
Distance = Duration*340*100/1000000; //音速を340m/sに設定 | |
Serial.print("Distance:"); | |
Serial.print(Distance); | |
Serial.println("cm"); | |
//時計回り(when 1450~700 µsec) 逆時計回り(when 1550~2300 µsec) 1500に近い方が回転がおそくなる | |
if(Distance>15){ //Distance>15(15cmより大きい)→<、>と数値の変更ができます。 | |
myservo.write(700); //700はモーターが一番早く回る数値です。変更可能です。 | |
digitalWrite(buzzerPin, HIGH); //HIGH:鳴る LOW:鳴らない | |
delay(200); | |
}else{ | |
myservo.write(1500); //1500はモーターが止まる数値です。変更可能です。 | |
digitalWrite(buzzerPin, LOW); //HIGH:鳴る LOW:鳴らない | |
delay(200); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment