有麻烦我的步进电机在Arduino的草图我的按钮响应传感器 [英] Having trouble getting my stepper motor to respond to my button sensor in Arduino sketch

查看:412
本文介绍了有麻烦我的步进电机在Arduino的草图我的按钮响应传感器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 AccelStepper 库来控制我的步进电机,和我有困难找出如何让我的电机停止时,我的按钮被按下。

我可以得到电机停止一旦它完成了从的moveTo 命令其整个运动,但我不能让它停止在完成之前。我已经使用if嵌套在while循环中的语句我使用获得电机运行,但没有骰子尝试。

因为它的立场是如下code:

 的#include< AccelStepper.h>const int的buttonPin = 4; //按键引脚数INT buttonState = 0;
INT motorSpeed​​ = 9600; //每秒最大步(约3rps / 16微步)
INT motorAccel = 80000; //步/秒/秒加速
INT motorDirPin = 8; //数字引脚8
INT motorStepPin = 9; //数字引脚9
INT状态= 0;
INT currentPosition = 0;//建立accelStepper intance
//将1,告诉它我们使用一个驱动程序
AccelStepper步进(1,motorStepPin,motorDirPin);无效设置(){
    pinMode(buttonPin,输入);
    stepper.setMaxSpeed​​(motorSpeed​​);
    stepper.setSpeed​​(motorSpeed​​);
    stepper.setAcceleration(motorAccel);
    stepper.moveTo(-12000); //移动2000步(靠拢顶端)
}空隙环(){
    而(stepper.currentPosition()!= - 10000)
        stepper.run();    //移动到下一个状态
    buttonState = digitalRead(buttonPin);
    currentPosition = stepper.currentPosition();    //检查按钮是pssed $ P $。
    //如果是,则buttonState为HIGH:
    //如果步进在所需位置
    如果(buttonState == HIGH){//需要找到一种方法来改变目前的移动指挥
        stepper.stop();
        stepper.runToPosition();
        stepper.moveTo(12000);
    }    如果(stepper.distanceToGo()== 0)
        状态= 1;    如果(状态== 1){
        stepper.stop();
        stepper.runToPosition();
        stepper.moveTo(12000);
    }
    //这些必须调用尽可能多地保证手术顺利进行
    //任何延迟会引起痉挛的动作
    stepper.run();
}


解决方案

我看到你的code三个问题:


  1. 当你按下按钮,它的状态将被设置为 HIGH 整个时间的你是pressing它,它可以是几个循环。你最好使用一个状态变量触发要在按钮preSS做一次的。


  2. 在看文档,你使用<一个href=\"http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a344f58fef8cc34ac5aa75ba4b665d21c\"相对=nofollow> stepper.runToPosition() ,该块,直到它到达目的地。所以,你越是点击,它就越可能被阻塞。你最好只使用 stepper.moveTo() stepper.run()方法,使使用几个周期的互动。


  3. 在你的循环的开始,你让你的code座,直到 stepper.currentPosition 获取到 -10000 。所以,你一定会被阻塞,直到它到达那里,取消对每个循环中的所有反应性()迭代。


您可以更好地工作,你的循环()函数列如下:

  uint8_t有button_state = 0; //比照问题1。
空隙环(){
    如果(digitalRead(buttonPin)== HIGH){
        如果(button_state == 0){
            stepper.stop();
            stepper.moveTo(12000);
            button_state = 1;
        }
    }其他{
        button_state = 0;
    }
    如果(stepper.distanceToGo()== 0){
        stepper.stop();
        stepper.moveTo(12000);
    }
    stepper.run();
}

I'm using the AccelStepper library to control my stepper motor, and I'm having difficulty figuring out how to get my motor to stop when my button is pushed.

I can get the motor to stop once it completes its entire movement from the moveTo command, but I can't get it to stop before it finishes. I've tried using an if statement nested within the while loop I'm using to get the motor to run, but no dice.

The code as it stands is as follows:

#include <AccelStepper.h>

const int buttonPin=4;  //number of the pushbutton pin

int buttonState=0;
int motorSpeed = 9600; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 80000; //steps/second/second to accelerate
int motorDirPin = 8; //digital pin 8
int motorStepPin = 9; //digital pin 9
int state = 0;
int currentPosition=0;

//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin); 

void setup(){  
    pinMode(buttonPin,INPUT);  
    stepper.setMaxSpeed(motorSpeed);
    stepper.setSpeed(motorSpeed);
    stepper.setAcceleration(motorAccel);
    stepper.moveTo(-12000); //move 2000 steps (gets close to the top)
}

void loop(){
    while( stepper.currentPosition()!=-10000)
        stepper.run();

    // move to next state
    buttonState = digitalRead(buttonPin);
    currentPosition=stepper.currentPosition();

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    //if stepper is at desired location
    if (buttonState == HIGH ){//need to find a way to alter current move to command
        stepper.stop();
        stepper.runToPosition();
        stepper.moveTo(12000);
    }

    if(stepper.distanceToGo() == 0)
        state=1;

    if(state==1){
        stepper.stop();
        stepper.runToPosition();
        stepper.moveTo(12000);
    }
    //these must be called as often as possible to ensure smooth operation
    //any delay will cause jerky motion
    stepper.run();
}

解决方案

I see three problems in your code:

  1. when you push the button, its state will be set to HIGH for the whole time you're pressing it, and it can be several loops. You'd better use a state variable that triggers what you want to do on button press only once.

  2. looking at the documentation, you're using stepper.runToPosition(), which blocks until it reaches the destination. So the more you click, the more it could get blocked. You'd better use only the stepper.moveTo() and stepper.run() method that enables to use a few cycles for interactions.

  3. at the beginning of your loop, you make your code block until stepper.currentPosition gets to -10000. So you surely are blocking until it gets there, removing all reactivity on every loop() iteration.

you may better work your loop() function out as follows:

uint8_t button_state = 0; // cf problem 1.
void loop() {
    if (digitalRead(buttonPin) == HIGH) {
        if (button_state == 0) {
            stepper.stop();
            stepper.moveTo(12000);
            button_state = 1;
        }
    } else {
        button_state = 0;
    }
    if (stepper.distanceToGo() == 0) {
        stepper.stop();
        stepper.moveTo(12000);
    }
    stepper.run();
}

这篇关于有麻烦我的步进电机在Arduino的草图我的按钮响应传感器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆