To explain my code, it has many other functions like a keypad and LCD display. But generally is that I input a number using the keypad and if the number is correct (corresponding to book's number). It should move exactly to where that book is located in a library-like setting (small scaled library) and from then on it picks the book up (but I haven't got there yet).
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Stepper.h>
// Stepper Constants Right
const int ENAR=38;
const int IN1R=42;
const int IN2R=40;
const int ENBR=48;
const int IN4R=46;
const int IN3R=44;
//Stepper Constants Left
const int ENAL= 53;
const int IN1L= 50;
const int IN2L= 52;
const int ENBL= 49;
const int IN4L= 51;
const int IN3L= 47;
Stepper stepperR(400,IN1R,IN2R,IN3R,IN4R);
Stepper stepperL(400,IN1L,IN2L,IN3L,IN4L);
//LCD CONSTANTS
const int RS=4;
const int E=5;
const int D7=8;
const int D6=9;
const int D5=10;
const int D4=11;
LiquidCrystal lcd(RS,E,D4,D5,D6,D7); //LCD display initialization
// counters and logicals
int n=0; //counter
boolean A; //checker for code array
char code[8]; //Code for Book retrieval
char book1[8]= {'1','2','3','4','5','6','7','8'}; //Book Database
//constants for keypad
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[rows] = {36,34,32,30};
byte colPins[cols] = {28,26,24,22};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
stepperR.setSpeed(50); //Speed in RPM
stepperL.setSpeed(50); //Speed in RPM
//Initialize H bridge Enables for stepper motor R
pinMode(ENAR,OUTPUT);
pinMode(ENBR,OUTPUT);
digitalWrite(ENAR,HIGH);
digitalWrite(ENBR,HIGH);
//Initialize H bridge ENABLES for stepper motor L
pinMode(ENAL,OUTPUT);
pinMode(ENBL,OUTPUT);
digitalWrite(ENAL,HIGH);
digitalWrite(ENBL,HIGH);
//LCD initializing and first message appearance + setting cursor to first column second row
lcd.begin(16,2);
lcd.print("Enter Book Code:");
Serial.begin(115200);
lcd.setCursor(0,1);
}
///////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
char key = keypad.getKey();
if((int)key != 0 && n <8 && key != 'C' && key != 'A' && key != 'B' && key != 'D' && key != '*' && key != '#' ){ //Checks for the code which should be numbers and excludes symbols
code[n]=key;
n++;
lcd.print(key);
lcd.setCursor(n,1);
}
if((int)key !=0 && key == 'C'){ //Clearing LCD INPUT by user
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
n=0;
}
if((int)key!=0 && key == 'B'){ //Backspacing LCD input by user
n=n-1;
lcd.setCursor(n,1);
lcd.print(" ");
lcd.setCursor(n,1);
}
if((int)key !=0 && key== 'A'){ //Enters code and checks for validity
for(int i=0;i<8;i++){
A=code[i]==book1[i];
A=A*A;
}
if(A){ //When code is correct
clearlcd();
lcd.setCursor(0,0);
lcd.print("Book Found.");
n=0;
getBook(); //set path to book by function getBook
}
else{ //when code is wrong
n=0;
clearlcd();
lcd.setCursor(0,0);
lcd.print("Invalid Code");
delay(5000);
clearlcd();
lcd.setCursor(0,0);
lcd.print("Enter Book Code:");
lcd.setCursor(0,1);
}
}
} //loop close bracket
void clearlcd(){ //Clears LCD Screen Completely
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(" ");
}
void getBook(){ //Function to get a book
for(int i; i<3000;i++){
stepperL.step(1);
stepperR.step(1);
}
}
I am sorry if this code is a mess, but basically the function getBook() is what runs the motors.
Used a sample book code as 12345678 and wanted to try to run the whole thing in a straight line for 3000 steps.
@jremington Thank you, I did try this calculator and I also tried doing it manually.
For example 5 kg load.
Normal force=5*9.81= 49 N.
Divided by 2 for each wheel ~25 N
assuming a rolling resistance coefficient of about 0.03 (which I think is conservative).
F_resistance= 0.75 N
with 6 cm arm (wheel radius). Torque required=4.5 Ncm
and each motor can provide way more than that.
I may be mistaken though and my assumptions could be wrong.