如何在我的步进电机代码中添加两个按钮

问题描述 投票:0回答:1

您好,我在向代码中添加两个按钮时遇到问题,我不知道如何为我正在开发的项目添加它。

我在 Arduino uno 上使用引脚 8(按钮 1)和引脚 9(按钮 2),步进电机连接到 引脚 3(DIR+), 引脚 4 (EN +) 和引脚 5 (PUL +)

代码很完美,100%有效,它使电机转动 360 度,但如果可能的话,我也喜欢使用按钮手动转动电机。

#include <Stepper.h>

const int stepsPerRevolution = 25600;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 3 through 4:
Stepper myStepper(stepsPerRevolution, 3,4);
int timeCounter=0;

void setup() {
  
  Serial.begin(100); // initialize the serial port:
  myStepper.setSpeed(100); // set the speed at 60 rpm:
  }

void loop() {
  
  Serial.println("clockwise"); // step one revolution  in one direction:
  myStepper.step(stepsPerRevolution);
  delay(100);
  
  Serial.println("\ncounterclockwise"); // step one revolution in the other direction:
  myStepper.step(-stepsPerRevolution);
  delay(100);
  timeCounter++;
  if(timeCounter == 2 )
  myStepper.setSpeed(0);
}

我尝试使用按钮添加此代码,但无法使其工作。我需要将这两个代码合并为一个。

// defines pins numbers

const int dirPin  = 3;
const int stepPin = 4;
const int enPin   = 5;

const int switchOne     = 8;
const int switchTwo     = 9;

int p1buttonState = 0;         // current state of the button
int lastp1buttonState = 0;     // previous state of the button

int p2buttonState = 0;         // current state of the button
int lastp2buttonState = 0;     // previous state of the button
bool bPress = false;

bool isForward = false;
bool isBackward = false;

void setup() {

  Serial.begin(9600);
  pinMode( switchOne, INPUT_PULLUP);
  pinMode( switchTwo, INPUT_PULLUP);

  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);

}



void loop() {

  isForward = false;
  isBackward = false;

  p1buttonState = digitalRead(switchOne);
  p2buttonState = digitalRead(switchTwo);

  if (p1ButtonPress()) {

    digitalWrite(dirPin, HIGH);

    delay(5);
  }

  if (p2ButtonPress()) {

    digitalWrite(dirPin, LOW);

    delay(5);
  }

  if ( isForward || isBackward ) {

    for (int x = 0; x < 3200; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
  }

}

bool p1ButtonPress()
{
  bool isPress = false;
  // compare the p1buttonState to its previous state
  if (p1buttonState != lastp1buttonState) {
    // if the state has changed, increment the counter
    if (p1buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;
      Serial.println("Plaer One score");

    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
      isForward = true;
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastp1buttonState = p1buttonState;
  return isPress;
}

bool p2ButtonPress()
{

  bool isPress = false;
  // compare the p1buttonState to its previous state
  if (p2buttonState != lastp2buttonState) {
    // if the state has changed, increment the counter
    if (p2buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;
      Serial.println("Plaer Two score");

    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
      isBackward = true;
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop

  lastp2buttonState = p2buttonState;

  return isPress;
}
button arduino
1个回答
0
投票

这是一个使用 Bounce2 库的示例(根据 Juraj 的建议)。如果我对您的应用程序了解更多,我可以创建一个更量身定制的示例。我怀疑你的问题与时间有关。有关为什么不使用延迟的更多信息,请参阅此论坛帖子。编译前记得安装Bounce2库。

#include <Stepper.h>
#include <Bounce2.h>

/* Timing Defines */
const int debounceInterval = 5;

/* End Timing Defines */



/* Stepper Defines */
const int stepsPerRevolution = 25600;

// initialize the stepper library on pins 3 through 4:
Stepper myStepper(stepsPerRevolution, 3, 4);

/* End Stepper Defines */



/* Button Defines */
const int buttonOnePin = 8;
const int buttonTwoPin = 9;

const int stepsPerButtonPress = 25600 / 32;  // make the denominator larger for smaller increments

Bounce2::Button buttonOne = Bounce2::Button();
Bounce2::Button buttonTwo = Bounce2::Button();

/* End Button Defines */


void setup() {
  Serial.begin(9600);      // initialize the serial port
  while (!Serial)
    ;                       // This will cause your program to wait for the serial port to open


  myStepper.setSpeed(100);  // set the speed at 60 rpm:
  myStepper.step(stepsPerRevolution);

  buttonOne.attach(buttonOnePin, INPUT_PULLUP);
  buttonTwo.attach(buttonTwoPin, INPUT_PULLUP);
  buttonOne.interval(debounceInterval);
  buttonTwo.interval(debounceInterval);

  Serial.println("Lets spin a stepper!");
}

void loop() {
  // check for updates
  buttonOne.update();
  buttonTwo.update();

  // check buttonOne
  if (buttonOne.pressed()) {
    Serial.println("Step up");
    myStepper.step(stepsPerButtonPress);
  }

  // check buttonTwo
  if (buttonTwo.pressed()) {
    Serial.println("Step down");
    myStepper.step(-stepsPerButtonPress);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.