2018-4-28 15:46:41 [显示全部楼层]
7909浏览
查看: 7909|回复: 6

[求助] 向大佬们求助~!步进电机与超声波传感器如何同时使用

[复制链接]
我的步进电机用的是42步进电机FIT0349,型号为42BYGH40-18-22A-C5.18。使用了步进电机驱动扩展板DRI0023。



超声波传感器使用的是SRF05。


想要的效果是超声波测得的距离值,控制灯带亮度与步进电机转速。但发现无法将步进电机程序与超声波传感器编入一个文件中:'(向大佬们求助,看看我哪里代码错了。

超声波测距仪程序:(使用的是MODE2,trig与echo同pin)
[mw_shl_code=cpp,true]//import
#include <NewPing.h>    //sonar
#include <Adafruit_NeoPixel.h>    //LED

//define pins
#define N_TRIG_PIN  9    // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define N_ECHO_PIN  9    // Arduino pin tied to echo pin on the ultrasonic sensor.

#define MAX_DISTANCE 400    // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define SONAR_NUM 1
#define PING_INTERVAL 33

#define LED_PIN 3    // Pin tied to LED

boolean human = 0;

int pingTimer; // Holds the times when the next ping should happen for each sensor.
int cm;         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

//set
NewPing sonar =
  NewPing(N_TRIG_PIN, N_ECHO_PIN, MAX_DISTANCE);  // NewPing setup of pins and maximum distance.
  
Adafruit_NeoPixel strip = Adafruit_NeoPixel(20, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup(){
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  pingTimer = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.

  strip.begin();
  strip.show();
}

void loop(){
    if (millis() >= pingTimer) {         // Is it this sensor's time to ping?
      pingTimer += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      sonar.timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      cm = 0;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar.ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }
}

//handle functions
void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar.check_timer()) {
    cm = sonar.ping_result / US_ROUNDTRIP_CM;
    pingResult();
  }
}

void pingResult() { // Sensor got a ping, do something with the result.
  // The following code would be replaced with your code that does something with the ping result.
  if(cm > 100){
    cm = 0;
  }
  human = cm;
  Serial.println(human);
  if(human == 0) {
    for(int i=0;i<20;i++){
      strip.setPixelColor(i, 100, 100, 100);
      strip.show();
    }
  }
  else{
    for(int i=0;i<5;i++){
      int d = int(255 - cm*1.55);
      strip.setPixelColor(i, d, d, d);
      strip.show();
    }
    for(int j=5;j<20;j++){
      if(cm < 3){
        cm = 0;
      }
      strip.setPixelColor(j, cm, cm, cm);
      strip.show();
    }
  }
  Serial.println(cm);
}
[/mw_shl_code]

步进电机程序:
[mw_shl_code=cpp,true]int Mdirpin = 7;   //电机Y方向引脚
int Msteppin = 6;  //电机Y驱动引脚
int Men= 8;        //电机Y使能引脚

void setup()
{
  pinMode(Mdirpin,OUTPUT);
  pinMode(Msteppin,OUTPUT);
  pinMode(Men,OUTPUT);
  
  digitalWrite(Men, 0); //低电平使能
}
void loop()
{
stepper();
}

void stepper(){
  digitalWrite(Mdirpin, 1);
  for(int j=0;j<=10000;j++){
    digitalWrite(Msteppin, 1);  //上升沿步进
    digitalWrite(Msteppin, 0);
    delay(1);
  }
}[/mw_shl_code]

将两个程序放在一起时,就会有个不运行。估计是因为delay的缘故,但不知道怎么改:'(再次求助大佬们!!多谢~~

步进电机

步进电机

步进电机拓展板

步进电机拓展板

超声波距离传感器

超声波距离传感器

Grey  中级技匠

发表于 2018-4-28 16:59:29

这几个引脚应该是X轴的,你看看连接图,有没有插错口?
回复

使用道具 举报

love_comic  学徒
 楼主|

发表于 2018-4-28 18:12:39

Grey 发表于 2018-4-28 16:59
这几个引脚应该是X轴的,你看看连接图,有没有插错口?

应该没错,这个两个程序单独运行都是对的
回复

使用道具 举报

love_comic  学徒
 楼主|

发表于 2018-4-28 18:16:02

在一个群里问的,有人说用状态机,是能把两个程序勉强放在一起。但是会出现步进电机噪音很大,会失步,有时还会跳过传感器的程序
回复

使用道具 举报

青夏  NPC

发表于 2018-4-29 14:29:15

metro库了解一下
回复

使用道具 举报

pATAq  版主

发表于 2018-5-1 16:37:18

凭直觉感觉不能用胶水粘起来,涉及优先级、抢占啊什么的,像楼上说的,参考以下多线程或者中断:
https://blog.csdn.net/ling3ye/article/details/73478534

Arduino 使用Metor库 简单实现多线程编程
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail