Created
February 15, 2016 04:59
-
-
Save okn3/ddf3e52a41c40bbf658f to your computer and use it in GitHub Desktop.
超音波距離センサで取得した値をLCDで表示
This file contains hidden or 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
| /* | |
| 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 | |
| */ | |
| #include <Wire.h> | |
| #include <ST7032.h> | |
| #define trigPin 8 | |
| #define echoPin 9 | |
| const int speakerPin = 11; | |
| ST7032 lcd; | |
| void setup() { | |
| Serial.begin (9600); | |
| pinMode(trigPin, OUTPUT); | |
| pinMode(echoPin, INPUT); | |
| pinMode(speakerPin, OUTPUT); | |
| //LCDの設定 | |
| lcd.begin(16, 2); | |
| lcd.setContrast(30); | |
| lcd.print("start..."); | |
| } | |
| void loop() { | |
| int duration, distance; | |
| digitalWrite(trigPin, HIGH); | |
| delayMicroseconds(1000); | |
| digitalWrite(trigPin, LOW); | |
| //読み込み | |
| duration = pulseIn(echoPin, HIGH); | |
| distance = (duration/2) / 29.1; | |
| //例外処理 | |
| if (distance >= 400 || distance <= 0){ | |
| Serial.println("Out of range"); | |
| } | |
| else { | |
| lcd.print(" "); | |
| Serial.print("distance: "); | |
| Serial.print(distance); | |
| Serial.println("cm"); | |
| lcd.setCursor(0, 0); | |
| lcd.print("DISTANCE:"); | |
| lcd.setCursor(0, 1); | |
| lcd.print(distance); | |
| lcd.print("cm"); | |
| } | |
| Serial.println("----------------------"); | |
| delay(500); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment