DF创客社区
发现
DF创客商城
教程
产品资料库
搜索
热门搜索
人工智能
视觉传感器
AI 人工智能
行空板
主控板
二哈识图
热门活动
关闭
DF创客社区
发现
DF创客商城
教程
产品资料库
超声波信号控制舵机
2016-12-22
2.5万
helloword
UID: 28508
1
文章
0
粉丝
0
获取喜欢
0
0
AI 快速预览
详细
收起
本文介绍了如何使用URM37超声波传感器和Arduino UNO R3控制舵机。通过编写if语句,可以根据超声波传感器接收到的距离信号来控制舵机的运动。适合初学者和STEM教育场景,帮助用户理解传感器的应用和基本编程逻辑。
要实现:利用超声波接收的距离信号作为判断条件,让舵机程序运行,怎么处理if语句?
超声波传感器:URM37,只用
Arduino
UNO R3可以不用实现?
创作许可协议
本项目采用
None(不开放任何权利,保留所有权利)
进行许可。
其他主题
Arduino
添加到合集
0
0
相关推荐
DF创客社区2014年度编辑选择奖 教程篇
Ash1
3.9万
3
我的Q&A问答专栏
陈德熹
2
0
BLE Arduino开发神器 Bluno Beetle开启免费试用啦!
Ash1
2.3万
0
评论(4)
gada888
写的全点就好了
回复
gray6666
没遇到过,学习了
回复
多兰兰兰兰
回复
helloword
请问可以分享一下代码么
回复
helloword
作者
回复
gray6666
找到问题了,控制舵机的if语句没有加大括号,尴尬:L:L
回复
微笑的rockets
我的理解是,利用超声波的距离来控制舵机转角的控制。
这个程序不需要用if语句来完成,简单的用map函数就可以完成。
利用映射,将超声波距离和舵机转角进行连接即可。
回复
20060606
回复
微笑的rockets
不明白您说的意思
回复
luna
有点没有听懂,能截图或者说详细一点吗
回复
helloword
作者
回复
luna
// # Editor :Zrh from DFRobot
// # Data :29.08.2014
// # Product name:ultrasonic scanner
// # Product SKU:SEN0001
// # Version : 1.0
// # Description:
// # The sketch for using the URM37 autonomous mode from DFRobot
// # and writes the values to the serialport
// # Connection:
// # Vcc (Arduino) -> Pin 1 VCC (URM V4.0)
// # GND (Arduino) -> Pin 2 GND (URM V4.0)
// # Pin 3 (Arduino) -> Pin 4 ECHO (URM V4.0)
// # Pin TX1 (Arduino) -> Pin 8 RXD (URM V4.0)
// # Pin RX0 (Arduino) -> Pin 9 TXD (URM V4.0)
// # Working Mode: autonomous mode.
int URECHO = 3; // PWM Output 0-25000US,Every 50US represent 1cm
unsigned int Distance=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xaa,0xf0}; // distance measure command
#include
Servo myservo; // ***定义舵机对象,最多八个
int pos=0; // ***定义舵机转动位置
void setup(){ // Serial initialization
Serial.begin(9600); // Sets the baud rate to 9600
AutonomousMode_Setup();
myservo.attach(9); // ***设置舵机控制针脚
}
void loop()
{
AutonomousMode();
delay(30);
} //PWM mode setup function
void AutonomousMode_Setup(){
pinMode(URECHO, INPUT); // Sending Enable PWM mode command
for(int i=0;i<4;i++){
Serial.write(EnPwmCmd[i]);
}
}
void AutonomousMode(){
unsigned long DistanceMeasured=pulseIn(URECHO,LOW);
if(DistanceMeasured>=30000){ // the reading is invalid.
Serial.print("Invalid");
}
else{
Distance=DistanceMeasured/50; // every 50us low level stands for 1cm
Serial.print("Distance=");
Serial.print(Distance);
Serial.println("cm");
}
if(Distance>=10)
delay(10);
for(pos = 0; pos < 40; pos += 1)
{
myservo.write(pos);
delay(30);
}
// 180到0旋转舵机,每次延时15毫秒
for(pos = 40; pos>=1; pos-=1)
{
myservo.write(pos);
delay(10);
}
这是我在URM37程序的基础上修改的程序,但是舵机工作不受控制,请问是怎么回事呀?
}
回复
helloword
作者
回复
luna
// # Editor :Zrh from DFRobot
// # Data :29.08.2014
// # Product name:ultrasonic scanner
// # Product SKU:SEN0001
// # Version : 1.0
// # Description:
// # The sketch for using the URM37 autonomous mode from DFRobot
// # and writes the values to the serialport
// # Connection:
// # Vcc (Arduino) -> Pin 1 VCC (URM V4.0)
// # GND (Arduino) -> Pin 2 GND (URM V4.0)
// # Pin 3 (Arduino) -> Pin 4 ECHO (URM V4.0)
// # Pin TX1 (Arduino) -> Pin 8 RXD (URM V4.0)
// # Pin RX0 (Arduino) -> Pin 9 TXD (URM V4.0)
// # Working Mode: autonomous mode.
int URECHO = 3; // PWM Output 0-25000US,Every 50US represent 1cm
unsigned int Distance=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xaa,0xf0}; // distance measure command
#include
Servo myservo; // ***定义舵机对象,最多八个
int pos=0; // ***定义舵机转动位置
void setup(){ // Serial initialization
Serial.begin(9600); // Sets the baud rate to 9600
AutonomousMode_Setup();
myservo.attach(9); // ***设置舵机控制针脚
}
void loop()
{
AutonomousMode();
delay(30);
} //PWM mode setup function
void AutonomousMode_Setup(){
pinMode(URECHO, INPUT); // Sending Enable PWM mode command
for(int i=0;i<4;i++){
Serial.write(EnPwmCmd[i]);
}
}
void AutonomousMode(){
unsigned long DistanceMeasured=pulseIn(URECHO,LOW);
if(DistanceMeasured>=30000){ // the reading is invalid.
Serial.print("Invalid");
}
else{
Distance=DistanceMeasured/50; // every 50us low level stands for 1cm
Serial.print("Distance=");
Serial.print(Distance);
Serial.println("cm");
}
if(Distance>=10)
delay(10);
for(pos = 0; pos < 40; pos += 1)
{
myservo.write(pos);
delay(30);
}
// 180到0旋转舵机,每次延时15毫秒
for(pos = 40; pos>=1; pos-=1)
{
myservo.write(pos);
delay(10);
}
}
这是程序,舵机工作不受URM37超声波传感器的影响,请问大神,怎么回事呀?
回复
共4条回复,已加载2条,点击查看更多
共4条回复,已加载全部
收起
展开
- 没有更多了 -
helloword
UID: 28508
1
文章
0
粉丝
0
获取喜欢
创作许可协议
本项目采用
None(不开放任何权利,保留所有权利)
进行许可。
相关推荐
DF创客社区2014年度编辑选择奖 教程篇
Ash1
3.9万
3
我的Q&A问答专栏
陈德熹
2
0
BLE Arduino开发神器 Bluno Beetle开启免费试用啦!
Ash1
2.3万
0
这个程序不需要用if语句来完成,简单的用map函数就可以完成。
利用映射,将超声波距离和舵机转角进行连接即可。
// # Data :29.08.2014
// # Product name:ultrasonic scanner
// # Product SKU:SEN0001
// # Version : 1.0
// # Description:
// # The sketch for using the URM37 autonomous mode from DFRobot
// # and writes the values to the serialport
// # Connection:
// # Vcc (Arduino) -> Pin 1 VCC (URM V4.0)
// # GND (Arduino) -> Pin 2 GND (URM V4.0)
// # Pin 3 (Arduino) -> Pin 4 ECHO (URM V4.0)
// # Pin TX1 (Arduino) -> Pin 8 RXD (URM V4.0)
// # Pin RX0 (Arduino) -> Pin 9 TXD (URM V4.0)
// # Working Mode: autonomous mode.
int URECHO = 3; // PWM Output 0-25000US,Every 50US represent 1cm
unsigned int Distance=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xaa,0xf0}; // distance measure command
#include
Servo myservo; // ***定义舵机对象,最多八个
int pos=0; // ***定义舵机转动位置
void setup(){ // Serial initialization
Serial.begin(9600); // Sets the baud rate to 9600
AutonomousMode_Setup();
myservo.attach(9); // ***设置舵机控制针脚
}
void loop()
{
AutonomousMode();
delay(30);
} //PWM mode setup function
void AutonomousMode_Setup(){
pinMode(URECHO, INPUT); // Sending Enable PWM mode command
for(int i=0;i<4;i++){
Serial.write(EnPwmCmd[i]);
}
}
void AutonomousMode(){
unsigned long DistanceMeasured=pulseIn(URECHO,LOW);
if(DistanceMeasured>=30000){ // the reading is invalid.
Serial.print("Invalid");
}
else{
Distance=DistanceMeasured/50; // every 50us low level stands for 1cm
Serial.print("Distance=");
Serial.print(Distance);
Serial.println("cm");
}
if(Distance>=10)
delay(10);
for(pos = 0; pos < 40; pos += 1)
{
myservo.write(pos);
delay(30);
}
// 180到0旋转舵机,每次延时15毫秒
for(pos = 40; pos>=1; pos-=1)
{
myservo.write(pos);
delay(10);
}
这是我在URM37程序的基础上修改的程序,但是舵机工作不受控制,请问是怎么回事呀?
}
// # Data :29.08.2014
// # Product name:ultrasonic scanner
// # Product SKU:SEN0001
// # Version : 1.0
// # Description:
// # The sketch for using the URM37 autonomous mode from DFRobot
// # and writes the values to the serialport
// # Connection:
// # Vcc (Arduino) -> Pin 1 VCC (URM V4.0)
// # GND (Arduino) -> Pin 2 GND (URM V4.0)
// # Pin 3 (Arduino) -> Pin 4 ECHO (URM V4.0)
// # Pin TX1 (Arduino) -> Pin 8 RXD (URM V4.0)
// # Pin RX0 (Arduino) -> Pin 9 TXD (URM V4.0)
// # Working Mode: autonomous mode.
int URECHO = 3; // PWM Output 0-25000US,Every 50US represent 1cm
unsigned int Distance=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xaa,0xf0}; // distance measure command
#include
Servo myservo; // ***定义舵机对象,最多八个
int pos=0; // ***定义舵机转动位置
void setup(){ // Serial initialization
Serial.begin(9600); // Sets the baud rate to 9600
AutonomousMode_Setup();
myservo.attach(9); // ***设置舵机控制针脚
}
void loop()
{
AutonomousMode();
delay(30);
} //PWM mode setup function
void AutonomousMode_Setup(){
pinMode(URECHO, INPUT); // Sending Enable PWM mode command
for(int i=0;i<4;i++){
Serial.write(EnPwmCmd[i]);
}
}
void AutonomousMode(){
unsigned long DistanceMeasured=pulseIn(URECHO,LOW);
if(DistanceMeasured>=30000){ // the reading is invalid.
Serial.print("Invalid");
}
else{
Distance=DistanceMeasured/50; // every 50us low level stands for 1cm
Serial.print("Distance=");
Serial.print(Distance);
Serial.println("cm");
}
if(Distance>=10)
delay(10);
for(pos = 0; pos < 40; pos += 1)
{
myservo.write(pos);
delay(30);
}
// 180到0旋转舵机,每次延时15毫秒
for(pos = 40; pos>=1; pos-=1)
{
myservo.write(pos);
delay(10);
}
}
这是程序,舵机工作不受URM37超声波传感器的影响,请问大神,怎么回事呀?