2

Here is my code:

#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'm trying to code it so that I can choose the speed with the keypad, and the motor starts when I press the 'I' button, and stops when I press the 'O' button. How can I do this? Thanks!

dda
  • 6,030
  • 2
  • 25
  • 34
Luke B
  • 288
  • 2
  • 5
  • 18

1 Answers1

1

I have corrected your code and commented where necessary. You had some small problems I fixed as well:

  1. You were decrementing plannedSpeed without stopping it from dropping below the minimum value, i.e. 0.
  2. You were incrementing plannedSpeed without stopping it from going above the maximum value, i.e. stepsPerRevolution.
  3. You weren't changing the value of plannedSpeed when starting the stepper motor.
#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);
  }
}
Morgoth
  • 4,935
  • 8
  • 40
  • 66