首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino Sonar和Stepper电机

Arduino Sonar和Stepper电机
EN

Stack Overflow用户
提问于 2019-03-16 05:20:12
回答 2查看 438关注 0票数 2

我正在尝试创建一个Arduino驱动的声纳/雷达。我目前有一个声纳传感器连接在发动机上,正在编写代码。问题出在下面的for循环中。传感器将ping,然后马达将移动,重复正确的次数。但是,无论距离是多少,声纳传感器返回的值都是0或1。如果能帮助我们找出问题所在,我们将不胜感激。

代码语言:javascript
复制
/*
   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);

}
EN

回答 2

Stack Overflow用户

发布于 2019-03-17 04:00:02

如果你想捕获距离,你可以做这个过程来验证你的传感器没有问题(我想布线是正确的):

代码语言:javascript
复制
// 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);
}

为了产生超声波,您需要将Trig设置为高状态10µs。这将发出一个8周期的声波脉冲串,它将以声速传播,并将在Echo引脚中接收。Echo引脚将以微秒为单位输出声波传播的时间。

声音的速度是340m/s或0.034 cm/µs,所以你可以除以2来捕获距离

票数 0
EN

Stack Overflow用户

发布于 2019-03-18 08:06:40

问题不在于代码。

事实证明,传感器是挑剔的需要几乎恰好5V运行。当伺服和传感器使用相同的电源时,当伺服运行时,电压将降至5V以下。

感谢所有帮助我们的人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55190804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档