【问题标题】:How to control a robot arm using ultrasonic sensor and Arduino如何使用超声波传感器和 Arduino 控制机械臂
【发布时间】:2016-11-29 17:50:12
【问题描述】:

我正在研究 arduino 和 HC_SR04。我搜索了大部分文档,但没有解决我们的问题。

我的问题是如何读取从库 <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
}

【问题讨论】:

    标签: c++ arduino arduino-ultra-sonic


    【解决方案1】:

    尝试使用distance = sonar.ping_cm(),而不是distance=digitalRead(echopin);

    【讨论】:

    • 感谢您的帮助
    猜你喜欢
    • 2013-07-01
    • 1970-01-01
    • 2014-10-17
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多