Last active
September 19, 2018 14:17
-
-
Save likersacademia/bfc8d51ee38f853f0f4b19c05dec3a06 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
// 距離20cm以内でスイッチが押されるとLEDが光り、ブザーも鳴る | |
#define ledPin 2 //LEDが2番ピン | |
#define buzzerPin 9 //ブザーが9番ピン | |
#define echoPin 5 //距離センサーの「echo」が5番ピン | |
#define trigPin 6 //距離センサーの「trig」が6番ピン | |
#define buttonPin 12 //スイッチが12番ピン | |
bool buttonState = 0; //スイッチのこと | |
double Duration = 0; //受信した間隔(距離センサーのこと) | |
double Distance = 0; //距離(距離センサーのこと) | |
void setup() { | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); //LEDのこと | |
pinMode(buzzerPin, OUTPUT); //ブザーのこと | |
pinMode(echoPin,INPUT); //距離センサーのこと | |
pinMode(trigPin,OUTPUT); //距離センサーのこと | |
pinMode(buttonPin, INPUT); //スイッチのこと | |
} | |
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"); | |
//距離センサーのこと。ここまで | |
} | |
buttonState = digitalRead(buttonPin); //スイッチのこと | |
if (Distance < 20 && buttonState == HIGH) { //Distance<20(20cmより小さい)→<、>と数値の変更ができます。buttonState == HIGH:ボタンが押されたら | |
digitalWrite(ledPin, HIGH); //HIGH:光る LOW:光らない(LEDのこと) | |
digitalWrite(buzzerPin, HIGH); //HIGH:鳴る LOW:鳴らない(ブザーのこと) | |
delay(200); | |
} else { | |
digitalWrite(ledPin, LOW); //HIGH:光る LOW:光らない(LEDのこと) | |
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