草图中的按钮传感器

草图中的按钮传感器

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

问题描述

限时删除!!

我正在使用 AccelStepper 库来控制我的步进电机,但我很难弄清楚如何在按下按钮时让我的电机停止.

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.

我可以在电机通过 moveTo 命令完成整个运动后使其停止,但我无法在它完成之前停止.我尝试使用嵌套在 while 循环中的 if 语句来让电机运行,但没有骰子.

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.

代码如下:

#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. 当你按下按钮时,它的状态将设置为 HIGH 你按下它的整个时间,它可以是几个循环.您最好使用一个状态变量,该变量仅在按下按钮一次时触发您想要执行的操作.

  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.

查看文档,您正在使用 stepper.runToPosition(),它会阻塞直到到达目的地.所以你点击的越多,它就越有可能被阻止.你最好只使用 stepper.moveTo()stepper.run() 方法,它们可以使用几个循环进行交互.

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.

在循环开始时,您将代码块直到 stepper.currentPosition 达到 -10000.所以你肯定会阻塞直到它到达那里,消除每个 loop() 迭代的所有反应性.

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.

您最好按如下方式处理 loop() 函数:

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 草图中的按钮传感器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:47