2017-03-26 10 views
1
여기

내 코드입니다 :Arduino의 특정 키패드 버튼을 누를 때까지 스테퍼 모터가 실행되게하려면 어떻게해야합니까?

#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'버튼. 어떻게해야합니까? 감사!

답변

0

나는 귀하의 코드를 수정하고 필요한 부분에 의견을 달았습니다. 즉 0 최소 값 이하로 떨어지는을 중지하지 않고

  1. 당신은 plannedSpeed을 감소시키는했다 : 당신은 나뿐만 아니라 고정 몇 가지 작은 문제가 있었다.
  2. 최대 값 (예 : stepsPerRevolution)을 초과하지 않고 plannedSpeed을 증가 시켰습니다.
  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); 
    } 
}