我正在尝试创建一个由 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以下。
感谢所有提供帮助的人。
关于c++ - Arduino 声纳和步进电机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55190804/