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

标签 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”按钮时电机启动,当我按下“O”按钮时电机停止。我怎样才能做到这一点?谢谢!

最佳答案

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

  1. 您正在减小 plannedSpeed,但没有阻止它降至最小值以下,即 0
  2. 您正在增加 plannedSpeed 而没有阻止它超过最大值,即 stepsPerRevolution
  3. 您在启动步进电机时没有更改 plannedSpeed 的值。
#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);
  }
}

关于c++ - 在按下 Arduino 上的特定键盘按钮之前,如何让步进电机运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43026807/

相关文章:

c++ - 如何实现 Matlab 的 mldivide(又名反斜杠运算符 "\")

arduino - 在 Arduino 上将整数/十进制转换为十六进制?

jQuery 键盘 : How to make superscript characters?

c++ - Hook 键盘以更改键码

java - 字母键盘问题,但使用字符串函数(Java)

C++ 从派生类和基类来回转换

C++ 最大数验证

Arduino GSM Shield 网络客户端 302

c++ - Arduino:从两个十进制字符串中获取十六进制值

c++ - 如何在 C++ 中将数字转换为字符串,反之亦然