2016-11-29 18 views
0

私はarduinoとHC_SR04について取り組んでいます。私はほとんどの文書を検索しましたが、私たちの問題を解決しませんでした。超音波センサーとArduinoを使用してロボットアームを制御する方法

私の質問は、ライブラリ<NewPing>から取得された値を読み取る方法です。

#define echopin 11 //set echopin 
#define trigpin 12 //set trigpin 

#include <Servo.h>; 

Servo robotArm; 

#include <NewPing.h> 

#define MAX_DISTANCE 400 

NewPing sonar(trigpin, echopin, MAX_DISTANCE); 

int distance; 

void setup() { 
    // put your setup code here, to run once: 
    Serial.begin(9600); 
    robotArm.attach(9); //attach our servo 
    robotArm.writeMicroseconds(150); 
} 

void loop() { 
    // put your main code here, to run repeatedly: 
    robotArm.write(90); //always set to servo 90 to position it to the middle 
    //codes of ultrasonic sensor 
    distance=digitalRead(echopin); 
    if (distance <= 20) //if ultrasonic sensor detects on obstacle less than 20 cm in 90 degree angle 
    { 
    robotArm.write(0); //dervo rotates at full speed to the right 
    delay(60); 
    } 

    else 
    { 
    robotArm.write(90); //else servo stays at 90 degree angle 
    delay(60); 
    } 
    Serial.print(distance); //print distance 
    Serial.println("cm"); //print distance unit cm 
} 

答えて

0

代わりのdistance=digitalRead(echopin);distance = sonar.ping_cm()を使用してみてください。

+0

ご協力ありがとうございます –

関連する問題