作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我的代码:
#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”按钮时电机停止。我怎样才能做到这一点?谢谢!
最佳答案
我已经更正了您的代码并在必要时进行了评论。你有一些小问题我也解决了:
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);
}
}
关于c++ - 在按下 Arduino 上的特定键盘按钮之前,如何让步进电机运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43026807/
我是一名优秀的程序员,十分优秀!