如何使用超声波传感器和arduino控制机器人手臂 [英] How to control a robot arm using ultrasonic sensor and arduino

查看:129
本文介绍了如何使用超声波传感器和arduino控制机器人手臂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #define echopin 11 // set echopin 
#define trigpin 12 //设置trigpin
#include < ; servo.h > ;
Servo robotArm;
#include < newping.h >
#define MAX_DISTANCE 400
NewPing声纳(trigpin,echopin,MAX_DISTANCE);
int 距离;
void setup(){
// 将您的设置代码放在这里,运行一次:
Serial.begin( 9600 );
robotArm.attach( 9 ); // 附加我们的伺服
robotArm.writeMicroseconds( 150 );
}

void loop(){
// 将主代码放在此处,重复运行:
robotArm.write( 90 ) ; // 始终设置为伺服90以将其定位到中间
// 超声波传感器代码
distance = digitalRead(echopin);
if (距离< = 20 // 如果超声波传感器检测到90度角度小于20厘米的障碍物
{
robotArm.write( 0 ); // dervo全速旋转到右边
延迟( 60 );
}
else
{
robotArm.write( 90 ); // 其他伺服保持90度角
延迟( 60 );
}
Serial.print(距离); // 打印距离
Serial.println( cm); // 打印距离单位cm
}





我尝试过:



我正在研究arduino和HC_SR04。我搜索了大部分文件,但我没有解决我们的问题。我的问题是如何读取从NewPing库中获取的值。谢谢

解决方案

目前尚不清楚,您要求的是什么,但您要搜索的第一点是在 NewPing文档



提示:对于清晰的代码,在顶部写入标题是最先进的,而不是实现和初始化:



  #include   <   servo.h  > ; 
#include < newping.h >
// 以常量开头
#define echopin 11 //设置echopin(我希望你知道为什么这个值)
#define trigpin 12 // set trigpin(我希望你知道为什么这个值)

const int MAX_DISTANCE = 400 ; // 更适合类型检查
// 实现全局对象
Servo robotArm;
NewPing声纳(trigpin,echopin,MAX_DISTANCE);
int distance = MAX_DISTANCE; // initialize< ; /newping.h>< /servo.h>


#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
}



What I have tried:

I'm working about arduino and HC_SR04. I searced most of documents, but I didnt solve our problem. My question is that how to read a value that is taken from library NewPing. Thanks

解决方案

It is not clear, what you are asking for, but the first point where you should search is in the NewPing documentation.

Tip: for clear code it is state of art to write the headers at top, and than implement and initialize like that:

#include <servo.h>;
#include <newping.h>
//start with constants
#define echopin 11 //set echopin (I hope you know why that value)
#define trigpin 12 //set trigpin (I hope you know why that value)

const int MAX_DISTANCE = 400;//better for type checks
//implement global objects
Servo robotArm;
NewPing sonar(trigpin, echopin, MAX_DISTANCE);
int distance = MAX_DISTANCE;//initialize</newping.h></servo.h>


这篇关于如何使用超声波传感器和arduino控制机器人手臂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆