Hi,
I want this robot to function so that someone can insert a certain linear velocity (ex. 500) on the keypad, and the motor would move in a certain direction, stop, then wait for the # button to be pressed for it to home using the hall effect sensor. I also have a joystick to control the motor as well.
Right now, the robot starts up, the motor homes (Ex. Direction B) until a magnet is detected by the hall effect sensor, then it stops. It waits for input from either the joystick or keypad. The joystick can move it left and right at three different set speeds. A number can be inserted in the keypad, and when a non-number (A, B, C, D) is pressed, the motor moves a certain distance in one direction (Ex. Direction A) and then moves back in the opposite direction (Ex. Direction B). The * button on the keypad clears the input incase something wrong was inputted into the keypad and the # button homes the motor.
The things that aren't working for me is that I want the motor to move in (Ex. Direction A), and then stop. Then I can press the # button and the motor would home and then the whole process repeats again (Waits for either joystick or keypad input).
Also, it seems like whenever I insert a certain linearv (linear velocity) on the keypad, it doesn't matter and the motor moves in the same speed regardless of the number inputted using the keypad.
Here is the most updated piece of code that I have
#include <LiquidCrystal.h> //LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Keypad.h> //KEYPAD
#include <AccelStepper.h> //STEPPER MOTOR
#define X_pin A0 // Pin A0 connected to joystick x axis
#define Joy_switch 52 // Pin 4 connected to joystick switch
#define motorInterfaceType 1 // Define motor interface type
#define HALL_SENSOR 8
#define DIR 23
#define STEP 25
const int dirPin = 23; // Define pin connections
const int stepPin = 25;
boolean printflag = true;
boolean setdir = HIGH; // (HIGH = anti-clockwise / LOW = clockwise)
int step_speed = 10; // Speed of Stepper motor (higher = slower)
const byte ROWS = 4; // Constants for row and column sizes
const byte COLS = 4;
byte rowPins[ROWS] = {39, 41, 43, 45}; // Connections to Arduino
byte colPins[COLS] = {47, 49, 51, 53};
int val = 0;
unsigned long linearv = 0;
int flag = 0;
long oldTime = 0;
int valueOld = 0;
char hexaKeys[ROWS][COLS] = // Array to represent keys on keypad
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{':', '0', ';', 'D'}
};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);// Create keypad object
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin); // Creates an instance
// Sequence control
//-------------------------------------------------------------------
void Joystick() {
if (!digitalRead(Joy_switch)) { // If Joystick switch is clicked
printflag = true;
delay(500); // delay for deboucing
switch (step_speed) { // check current value of step_speed and change it
case 1:
step_speed = 10; // slow speed
break;
case 3:
step_speed = 1; // fast speed
break;
case 10:
step_speed = 3; // medium speed
break;
}
}
if (printflag == true) {
lcd.clear(); // Clear LCD display and print character
lcd.setCursor(0, 0);
lcd.print(step_speed);
printflag = false;
}
if (analogRead(X_pin) > 712) { // If joystick is moved Left
digitalWrite(dirPin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(stepPin, HIGH);
delay(step_speed);
digitalWrite(stepPin, LOW);
delay(step_speed);
}
if (analogRead(X_pin) < 312) { // If joystick is moved right
digitalWrite(dirPin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(stepPin, HIGH);
delay(step_speed);
digitalWrite(stepPin, LOW);
delay(step_speed);
}
}
int getKeypadIntegerMulti() {
int value = 0; // the number accumulator
int keyvalue; // the key pressed at current moment
int isnum;
do {
keyvalue = customKeypad.getKey(); // input the key
Joystick();
if (keyvalue) {
if (keyvalue == ';') {
isnum = 0;
keyvalue = 0;
value = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(value);
homefunction();
}
}
if (keyvalue) {
if (keyvalue == ':') {
isnum = 0;
keyvalue = 0;
value = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(value);
}
Serial.println(keyvalue);
isnum = (keyvalue >= '0' && keyvalue <= ';'); // is it a digit?
if (isnum) {
value = value * 10 + keyvalue - '0'; // accumulate the input number
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(value);
}
}
}
while (isnum || !keyvalue);
return value;
}
void homefunction() {
// Set motor speed pulse duration
int pd = 4000;
// Move motor until home position reached
while (digitalRead(HALL_SENSOR) == 1) {
digitalWrite(DIR, setdir);
digitalWrite(STEP, HIGH);
delayMicroseconds(pd);
digitalWrite(STEP, LOW);
delayMicroseconds(pd);
setdir = LOW;
}
}
//-------------------------------------------------------------------
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
myStepper.setMaxSpeed(100);
myStepper.setAcceleration(50);
myStepper.setSpeed(linearv);
myStepper.moveTo(200);
// Setup the stepper controller pins as Outputs
pinMode(DIR, OUTPUT);
pinMode(STEP, OUTPUT);
// Setup the Hall Effect switch as an Input
pinMode(HALL_SENSOR, INPUT);
pinMode(X_pin, INPUT);
pinMode(Joy_switch, INPUT_PULLUP);
// Home the motor
homefunction();
}
//-------------------------------------------------------------------
void loop() {
while (linearv == 0) // While don´t value
{
Serial.println("Running");
val = getKeypadIntegerMulti(); // Look keypad
linearv = (val / 10 / 15.7295 * 60); // Calc vel. value
}
if (flag == 0) // Sequence 0 Shown value on LCD
{
lcd.clear(); // Clear LCD display and print character
lcd.setCursor(0, 0);
lcd.print(linearv);
flag = 1; // Allow sequence 1
}
if (myStepper.distanceToGo() == 0) // Change direction once the motor reaches target position
{
flag++; // Incremente sequence count
if (flag == 2) // If sequence 1 complete
{
myStepper.moveTo(-myStepper.currentPosition());
}
if (flag == 3) // If sequence 2 complete
{
flag = 0; // Allow start
linearv = 0; // Zero vel. value
lcd.clear(); // Clear LCD display and print character
myStepper.moveTo(-myStepper.currentPosition()); // Return
}
}
myStepper.run(); // Move the motor one step
}
Hoping someone can help ![]()