/*
My revision of homemodelenginemachinist member, bmac2's code.
https://www.homemodelenginemachinist.com/threads/arduino-rotary-table-for-dummies.26744/
Auto Indexer Control
(12/29/19)
= Given =
Rotary Table gear ratio: 90:1
360 degrees = 1296000 seconds
Servo Motor gear ratio: 5:1
Servo Motor ppr: 2048
Servo Motor ppr per Rotary Table Revolution: ((2048 * 5:1) * 90:1) = 921600
(1296000 / 921600) = 1.40625
Therefore, the Rotary Table will rotate 1.40625 seconds per Motor ppr.
*/
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Wire.h>
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] = {11,10,9,8};
byte colPINS[COLS] = {7,6,5,4};
/*
Initialize instance of Classes, Keypad and LiquidCrystal_I2C.
*/
Keypad kypd = Keypad(makeKeymap(keys),rowPINS,colPINS, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27,20,4);
/*
Set Up Variables
*/
const float shorty = (3600 / (1296000 / ((2048 * 90) * 5)));
const int dwell = 1;
float degrees = 0;
float curpos = 0;
long steps = 0;
int evnt = 0;
/*
Assign Pins
*/
const int step = 2;
const int direction = 3;
void setup()
{
lcd.init(); // Initialize the LCD
pinMode(step, OUTPUT);
pinMode(direction, OUTPUT);
lcd.backlight();
lcd.print("Auto Indexer Control"); // Print welcome message to the LCD.
lcd.setCursor(4,2);
lcd.print(" ");
lcd.setCursor(3,3);
lcd.print("updated 2019");
delay(2000);
lcd.init();
evnt = 0;
char key = kypd.getKey();
lcd.print("Enter Selection:");
lcd.setCursor(0,1);
lcd.print("Degrees = A");
lcd.setCursor(0,2);
lcd.print("Divisions = B");
lcd.setCursor(0,3);
lcd.print("JOG = C");
while(evnt == 0)
{
key = kypd.getKey();
switch (key)
{
case NO_KEY: break;
case 'A': degrees = getdegrees();
lcd.clear();
evnt = 1;
break;
case 'B': degrees = getdivisions();
evnt = 2;
break;
case 'C': degrees = getjoginc();
lcd.clear();
evnt = 3;
break;
}
}
}
void loop()
{
lcd.clear();
char key = kypd.getKey();
curpos = 0;
lcd.setCursor(7,0);
lcd.print("Total: ");
lcd.print(curpos,2); // total steps
lcd.setCursor(0,3);
lcd.print("FOR=A REV=B X=C");
while(key != 'C') // C will return to start menu
{
lcd.setCursor(0, 0);
lcd.print(degrees, 2);
lcd.print((char)223);
key = kypd.getKey();
if(key == 'A') // FORWARD
{
curpos = curpos + degrees;
steps = getsteps(degrees);
digitalWrite(direction, LOW);
printadvance();
}
if(key =='B') // REVERSE
{
curpos = curpos - degrees;
steps = getsteps(degrees);
digitalWrite(direction, HIGH);
printadvance();
}
}
lcd.init();
setup();
}
int getjoginc()
{
int joginc = 0;
char key = kypd.getKey();
lcd.clear();
lcd.setCursor(6,0);
lcd.print("Jogging");
lcd.setCursor(0,1);
lcd.print("A=1 B=10 C=100 Steps");
lcd.setCursor(0,2);
lcd.print("Enter Degrees:");
lcd.setCursor(0,3);
lcd.print("OK = # ");
lcd.print((char)60);
lcd.print((char)45);
lcd.print(" D");
while(key != '#')
{
switch (key)
{
case NO_KEY: break;
case 'A': joginc = 1;
lcd.setCursor(14,2);
lcd.print(degrees);
break;
case 'B': joginc = 10;
lcd.setCursor(14,2);
lcd.print(degrees);
break;
case 'C': joginc = 100;
lcd.setCursor(14,2);
lcd.print(degrees);
break;
case 'D': lcd.setCursor(14,2);
lcd.print(" ");
lcd.setCursor(14,2);
break;
}
key = kypd.getKey();
}
return joginc;
}
float getdivisions()
{
char key = kypd.getKey();
float dgrs = 0;
String divstr;
int divs = 0;
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Enter Division:");
lcd.setCursor(0,3);
lcd.print("OK = # ");
lcd.print((char)60);
lcd.print((char)45);
lcd.print(" D");
lcd.setCursor(16,1);
while(key != '#')
{
switch (key)
{
case NO_KEY: break;
case 'D': lcd.setCursor(16,1);
lcd.print(" ");
lcd.setCursor(16,1);
divstr = "";
break;
case '0': divstr += ("0");
case '1': divstr += ("1");
case '2': divstr += ("2");
case '3': divstr += ("3");
case '4': divstr += ("4");
case '5': divstr += ("5");
case '6': divstr += ("6");
case '7': divstr += ("7");
case '8': divstr += ("8");
case '9': divstr += ("9");
lcd.print(key);
break;
}
key = kypd.getKey();
}
dgrs = (360 / divstr.toInt());
divstr = "";
return dgrs;
}
float getdegrees()
{
char key = kypd.getKey();
float dgrs = 0;
String dgrstr;
lcd.clear(); // initialize LCD
lcd.setCursor(0, 1);
lcd.print("Enter Degrees:");
lcd.setCursor(0, 3);
lcd.print("OK = # ");
lcd.print((char)60);
lcd.print((char)45);
lcd.print(" D");
lcd.setCursor(15, 1);
while(key != '#')
{
switch (key)
{
case NO_KEY: break;
case '.': dgrstr += (".");
break;
case 'D': lcd.setCursor(15, 1);
lcd.print(" ");
lcd.setCursor(15, 1);
dgrstr = "";
break;
case '0': dgrstr += ("0");
case '1': dgrstr += ("1");
case '2': dgrstr += ("2");
case '3': dgrstr += ("3");
case '4': dgrstr += ("4");
case '5': dgrstr += ("5");
case '6': dgrstr += ("6");
case '7': dgrstr += ("7");
case '8': dgrstr += ("8");
case '9': dgrstr += ("9");
break;
}
key = kypd.getKey();
}
dgrs = dgrstr.toFloat();
dgrstr = "";
return dgrs;
}
long getsteps(float dgrs)
{
return round(dgrs * shorty);
}
void printadvance()
{
lcd.setCursor(6, 1);
lcd.print("Moving");
lcd.setCursor(4, 2);
lcd.print("Steps ");
lcd.print(steps, 0);
lcd.setCursor(13, 0);
lcd.print(curpos, 2);
index(steps, 0);
lcd.setCursor(6, 1);
lcd.print(" ");
}
void index(long stps, int dw)
{
for(int i = 0; i < steps; i++)
{
digitalWrite(step, HIGH);
delay(dwell);
digitalWrite(step, LOW);
delay(dwell);
}
}
void software_Reset() // Initializes program without resetting the peripherals and registers.
{
asm volatile (" jmp 0");
}