在按下 Arduino 上的特定键盘按钮之前,如何让步进电机运行?
Posted
技术标签:
【中文标题】在按下 Arduino 上的特定键盘按钮之前,如何让步进电机运行?【英文标题】:How can I make a stepper motor run until I press a specific keypad button on Arduino? 【发布时间】:2017-03-26 08:48:49 【问题描述】:这是我的代码:
#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
。
您在增加 plannedSpeed
时没有阻止它超过最大值,即 stepsPerRevolution
。
您没有在启动步进电机时更改 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);
【讨论】:
以上是关于在按下 Arduino 上的特定键盘按钮之前,如何让步进电机运行?的主要内容,如果未能解决你的问题,请参考以下文章