Hi, im making a project where i input a value and the arduino moves a stepper motor to that location. I'm using a 16x2 LCD for output, and want to add a 4x4 matrix keypad to enter the value instead of in the serial monitor. However, no matter how i try to implement it, it always results in an error.
Below is the code i'm using:
#include <Keypad.h>
#include "AccelStepper.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
// AccelStepper Setup
AccelStepper stepperX(1, 12, 11); // 1 = Easy Driver interface
// NANO Pin 2 connected to STEP pin of Easy Driver
// NANO Pin 3 connected to DIR pin of Easy Driver
// Define the Pins used
#define home_switch 10 // Pin 9 connected to Home Switch (MicroSwitch)
// Stepper Travel Variables
long TravelX; // Used to store the X value entered in the Serial Monitor
int move_finished=1; // Used to check if move is completed
long initial_homing=-1; // Used to Home Stepper at startup
void setup() {
//lcd setup
lcd.clear();
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
pinMode(home_switch, INPUT_PULLUP);
delay(5);
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepperX.setMaxSpeed(800.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(800.0); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
lcd.setCursor(0,0);
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepperX.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepperX.run(); // Start moving the stepper
delay(1);
lcd.print("INITIALIZING!");
delay(250);
lcd.clear();
delay(250);
}
stepperX.setCurrentPosition(0); // Set the current position as zero for now
stepperX.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing=1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
stepperX.moveTo(initial_homing);
initial_homing++;
stepperX.run();
delay(1);
}
stepperX.setCurrentPosition(0);
lcd.setCursor(0,0);
lcd.print("HOMING COMPLETED");
//delay(5000);
Serial.println("Homing Completed");
Serial.println("");
stepperX.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepperX.setAcceleration(500.0); // Set Acceleration of Stepper
// Print out Instructions on the Serial Monitor at Start
lcd.setCursor(0,1);
lcd.print("enter value:");
Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
}
void loop() {
while (Serial.available()>0) {
//motor part
move_finished=0; // Set variable for checking move of the Stepper
//TravelX= Serial.parseInt(); // Put numeric value from buffer in TravelX variable
TravelX= Serial.parseInt();
if (TravelX < 0 || TravelX > 140250) { // Make sure the position entered is not beyond the HOME or MAX position
Serial.println("");
Serial.println("Please enter a value greater than zero and smaller or equal to 70250.....");
Serial.println("");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("invalid value.");
} else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Moving to:");
Serial.print("Moving stepper into position: ");
lcd.print(TravelX);
Serial.println(TravelX);
stepperX.moveTo(TravelX); // Set new moveto position of Stepper
delay(1000); // Wait 1 seconds before moving the Stepper
}
}
if (TravelX >= 0 && TravelX <= 140250) {
// Check if the Stepper has reached desired position
if ((stepperX.distanceToGo() != 0)) {
stepperX.run(); // Move Stepper into position
}
// If move is completed display message on Serial Monitor
if ((move_finished == 0) && (stepperX.distanceToGo() == 0)) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("COMPLETED!");
Serial.println("COMPLETED!");
Serial.println("");
Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
lcd.setCursor(0,1);
lcd.print("enter value:");
move_finished=1; // Reset move variable
}
}
//stepperX.stop();
}