在按下Arduino上的特定键盘按钮之前,如何使步进电机运行?

时间:2017-03-26 08:48:50

标签: c++ arduino keypad

这是我的代码:

#include <LiquidCrystal.h>
#include <Stepper.h>
#include <Key.h>
#include <Keypad.h>

const int stepsPerRevolution = 200;
const byte ROWS = 4; //four rows
const byte COLS = 1; //one column
char keys[ROWS][COLS] = {
  {'-'},
  {'+'},
  {'I'},
  {'0'}};
 byte rowPins[ROWS] = {6,7,8,9};
 byte colPins[COLS] = {10};

int count = 0;
LiquidCrystal lcd(A5,A4,A3,A2,A1,A0);
Stepper myStepper(stepsPerRevolution, 2,4,3,5);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int plannedSpeed = 50;

void setup() {
  lcd.begin(16,2);
  Stepper.setSpeed(plannedSpeed);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  char key = keypad.getKey();
  Serial.println(plannedSpeed);
  if (key == '-') {
    plannedSpeed = plannedSpeed - 1;
    Serial.println(plannedSpeed);
    delay(10);
  }
  if (key == '+') {
    plannedSpeed = plannedSpeed +1;
    Serial.println(plannedSpeed);
    delay(10);
      }
  if (key == 'I') {
    myStepper.step(stepsPerRevolution);
    Serial.print("Running at " );
    Serial.println(plannedSpeed);
    delay(10);
  }
}

我正在尝试对其进行编码,以便我可以使用键盘选择速度,并且当我按下“I&#39;按钮,当我按下&O 39按钮。我怎样才能做到这一点?谢谢!

1 个答案:

答案 0 :(得分:0)

我已更正您的代码并在必要时进行了评论。你也遇到了一些小问题:

  1. 您正在递减plannedSpeed而不会阻止其降至最低值以下,即0
  2. 您正在递增plannedSpeed而不会阻止其超越最大值,即stepsPerRevolution
  3. 启动步进电机时,您没有更改plannedSpeed的值。
  4. #include <LiquidCrystal.h>
    #include <Stepper.h>
    #include <Key.h>
    #include <Keypad.h>
    
    const int stepsPerRevolution = 200;
    const byte ROWS = 4; //four rows
    const byte COLS = 1; //one column
    char keys[ROWS][COLS] = {
      {'-'},
      {'+'},
      {'I'},
      {'0'}
    };
    byte rowPins[ROWS] = {6, 7, 8, 9};
    byte colPins[COLS] = {10};
    
    int count = 0;
    LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
    Stepper myStepper(stepsPerRevolution, 2, 4, 3, 5);
    Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
    
    int plannedSpeed = 50;
    void setup() {
      // put your setup code here, to run once:
      lcd.begin(16, 2);
      Stepper.setSpeed(plannedSpeed);
      Serial.begin(9600);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      char key = keypad.getKey();
      Serial.println(plannedSpeed);
      if (key == '-') {
        if (--plannedSpeed < 0) // Decrease plannedSpeed 
          plannedSpeed = 0; // Make sure it does not go below 0
        Serial.println(plannedSpeed);
        delay(10);
      }
      else if (key == '+') {
        if (++plannedSpeed > stepsPerRevolution) // Increase plannedSpeed 
          plannedSpeed = stepsPerRevolution; // Make sure it does not go above stepsPerRevolution
        Serial.println(plannedSpeed);
        delay(10);
      }
      else if (key == 'I') {
        plannedSpeed = stepsPerRevolution; // Set plannedSpeed to maximum
        myStepper.step(plannedSpeed); // Set stepper to plannedSpeed
        Serial.print("Running at " );
        Serial.println(plannedSpeed);
        delay(10);
      }
      else if (key == '0') {
        plannedSpeed = 0; // Set plannedSpeed to 0
        myStepper.step(plannedSpeed); // Set stepper to plannedSpeed
        Serial.print("Stopping at " );
        Serial.println(plannedSpeed);
        delay(10);
      }
    }