gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:10:10 25 4
gpt4 key购买 nike

这是我的代码:

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com