|
发表于 2019-2-22 17:03:36
|
显示全部楼层
- #include "Servo.h"
- #define trigpin 3
- #define echopin 2
- Servo servo;
- int sound = 250;
- void setup( ) {
- Serial .begin (9600);
- pinMode(trigpin,OUTPUT);
- pinMode(trigpin,INPUT);
- servo.attach(4);
- }
- void loop( ) {
- long duration, distance;
- digitalWrite(trigpin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigpin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigpin, LOW);
- duration = pulseIn(echopin, HIGH);
- distance = (duration/2)/29.1;
- if (distance < 5) {
- Serial.println("the diatance is less than 5");
- servo . write ( 90 ) ;
- }
- else{
- servo.write(0);
- }
- if ( distance > 60){
- Serial . println ( " The distance is more than 60 ") ;
- }
- else{
- Serial.print(distance);
- Serial . println ( "cm " ) ;
- }
- delay(500);
- }
复制代码
不知道你这写得对不对。但是语法错误不少。 |
|