超声波信号控制舵机
要实现:利用超声波接收的距离信号作为判断条件,让舵机程序运行,怎么处理if语句?超声波传感器:URM37,只用Arduino UNO R3可以不用实现?
有点没有听懂,能截图或者说详细一点吗
luna 发表于 2016-12-22 16:28
有点没有听懂,能截图或者说详细一点吗
@ 一下大神看看 我的理解是,利用超声波的距离来控制舵机转角的控制。
这个程序不需要用if语句来完成,简单的用map函数就可以完成。
利用映射,将超声波距离和舵机转角进行连接即可。 dsweiliang 发表于 2016-12-22 21:19
@ 一下大神看看
谢谢你的解答,能不能举一个具体的代码作为例子? luna 发表于 2016-12-22 16:28
有点没有听懂,能截图或者说详细一点吗
// # 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 autonomousmode 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: autonomousmode.
int URECHO = 3; // PWM Output 0-25000US,Every 50US represent 1cm
unsigned int Distance=0;
uint8_t EnPwmCmd={0x44,0x02,0xaa,0xf0}; // distance measure command
#include <Servo.h>
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);
}
}
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超声波传感器的影响,请问大神,怎么回事呀? luna 发表于 2016-12-22 16:28
有点没有听懂,能截图或者说详细一点吗
// # 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 autonomousmode 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: autonomousmode.
int URECHO = 3; // PWM Output 0-25000US,Every 50US represent 1cm
unsigned int Distance=0;
uint8_t EnPwmCmd={0x44,0x02,0xaa,0xf0}; // distance measure command
#include <Servo.h>
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);
}
}
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程序的基础上修改的程序,但是舵机工作不受控制,请问是怎么回事呀?
} 没遇到过,学习了 gray6666 发表于 2016-12-24 20:29
没遇到过,学习了
找到问题了,控制舵机的if语句没有加大括号,尴尬:L:L helloword 发表于 2016-12-26 19:36
找到问题了,控制舵机的if语句没有加大括号,尴尬
请问可以分享一下代码么 写的全点就好了 Rockets 发表于 2016-12-23 11:21
我的理解是,利用超声波的距离来控制舵机转角的控制。
这个程序不需要用if语句来完成,简单的用map函数就可 ...
不明白您说的意思
页:
[1]