Arduino Sonar和步进马达
Arduino Sonar and Stepper Motor
我正在尝试创建一个arduino驱动的声纳/雷达。我目前在电动机上附有声纳传感器并处理代码。问题在于下面的for循环。传感器将ping和电动机将移动,重复正确的次数。但是,无论距离如何,声纳传感器返回的值是0或1。识别问题的任何帮助将不胜感激。
/*
Nathan Verdonk
3/15/2019
*/
#include <NewPing.h>
#include <Stepper.h>
const int stepsPerRevolution = 2048; // Steps per revolution
const int rotSpeed = 10; // Speed of rotation in RPM
const int triggerPin = 7; // Trigger pin on sonar sensor
const int echoPin = 6; // Echo pin on sonar sensor
const int maxDistance = 300; // Max distance expected from sensor in cm; do not exceed 400
int val;
Stepper stepper1(stepsPerRevolution, 8, 10, 9, 11); // initialize the stepper library on pins 8 through 11:
NewPing sonar1(triggerPin, echoPin, maxDistance); // initialize the new ping library with predefined values
void setup() {
stepper1.setSpeed(rotSpeed);
Serial.begin(115200);
}
void loop() {
for(int i = 0; i < 50; i++){
delay(50);
val = sonar1.ping_cm();
Serial.println(val);
stepper1.step(1);
}
delay(3000);
}
问题不在代码上。
事实证明,传感器很挑剔,需要几乎完全需要5 V来运行。使用伺服和传感器使用相同的电源,当伺服运行时,电压将降至5 V以下。
感谢所有帮助的人。
如果要捕获距离,则可以执行此过程以验证传感器没有问题(我想接线是正确的):
// defines pins numbers
const int triggerPin = 7;
const int echoPin = 6;
// defines variables
long duration;
int distance;
void setup() {
pinMode(triggerPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
}
void loop() {
delay(50);
// Clears the triggerPin
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the triggerPin on HIGH state for 10 micro seconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
为了生成超声波,您需要将触发器设置为高状态10 µs。这将发出8个周期的声音爆发,该爆发将以速度声音传播,并将在Echo Pin中接收。回声引脚将在微秒内输出声波传播的时间。
声音的速度为340 m/s或0.034 cm/µs,因此您除以2,以捕获距离
相关文章:
- 需要反转音频步进公式
- Arduino Nano:A4988 使用串行输入时通过定时器进行步进控制不稳定
- 锁定步进pthread互斥
- 在 ODEINT 中通过 if 语句选择步进器
- boost odeint 中的受控误差步进器是否支持复杂的数据类型?
- 创建带有限幅器的步进器控件
- 班上的Arduino步进电动机指针不起作用
- 生成最多给定数字 N 的步进数字
- Boost ODEINT:具有自定义类和向量空间代数的受控步进器
- 我如何仅使用一个Arduino uno才能在同一时间和同一方向上旋转两个步进电动机
- 如何使步进电机运行,直到我按下 Arduino 上的特定键盘按钮
- Arduino Sonar和步进马达
- 这些步进回合是否等效
- 使用C++同步控制多个步进器
- 接口 C++ 以使用 Arduino 控制电机步进器
- 步骤程序?开始、结束、步进整数
- 如何使用ODEINT的标签系统为各种步进器类型做特定工作
- 通过替换模板<类步进器来要求>
- GDB 继续而不是步进
- cpp std::list 使用步进添加新元素