超声波信号控制舵机

2016-12-222.5万
AI 快速预览详细 收起
本文介绍了如何使用URM37超声波传感器和Arduino UNO R3控制舵机。通过编写if语句,可以根据超声波传感器接收到的距离信号来控制舵机的运动。适合初学者和STEM教育场景,帮助用户理解传感器的应用和基本编程逻辑。
要实现:利用超声波接收的距离信号作为判断条件,让舵机程序运行,怎么处理if语句?

超声波传感器:URM37,只用Arduino UNO R3可以不用实现?

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(4)
gada888
gada888
写的全点就好了
gray6666
gray6666
没遇到过,学习了
多兰兰兰兰
多兰兰兰兰回复helloword
请问可以分享一下代码么
helloword
helloword
作者
回复gray6666
找到问题了,控制舵机的if语句没有加大括号,尴尬:L:L
微笑的rockets
微笑的rockets
我的理解是,利用超声波的距离来控制舵机转角的控制。
这个程序不需要用if语句来完成,简单的用map函数就可以完成。
利用映射,将超声波距离和舵机转角进行连接即可。
20060606
20060606回复微笑的rockets
不明白您说的意思
luna
luna
有点没有听懂,能截图或者说详细一点吗
helloword
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
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条回复,已加载全部
- 没有更多了 -