Rotary table code Used on Arduino Mega
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
//#include <I2CScanner.h>
const byte ROWS = 4;
const byte COLS = 4;
//****************USER DEFINED**************
//setup vars
const int stp = 10; // connect pin D10 to step
const int dir = 11; // connect pin D11 to dir
const int StepsPerRotation = 1600; // Set Steps per rotation of stepper NOTE the driver is set to Half step
const int TableRatio = 90; // ratio of rotary table
const int stepdelay = 1000; //this is in microseconds
int delay1 = 2000; //this is used for Splash Screen display time
byte colPINS[ROWS] = {2, 3, 4, 5}; //pin 1 of keypad to pin 12
byte rowPINS[COLS] = {6, 7, 8, 9};
int pot = A0; //potentiometer wiper
int pot_movement = 0;
int pot_movement_mapped = 0;
int adj_speed = 3000; //Speed adjustment (Max mapped uS) Making this LOWER will INCREASE min RPM, making this HIGHER will DECREASE min RPM
int feed_pot_thresh=5; //Adjust this variable to change when the potentiometer makes the motor move (bigger means more movement to initialise motor)
int inc = 0;
int ledbrightness = 120;
int led_inc = 0;
int ind_green = 44;
int ind_red = 46;
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address as determined by I2C scanner
/*
byte degss[] = {
B00100,
B01110,
B01110,
B01110,
B11111,
B00000,
B00100,
B00000
};
*/
// SCL - A5, SDA - A4, VCC - +5, Gnd - Gnd
//***********************************************
// **************define Debug Macro**************
//#define DEBUG 1 //delete slashes for printing debug info to serial monitor
#ifdef DEBUG
#define DEBUG_Print(x) Serial.print(x)
#define DEBUG_PrintDec(x,y) Serial.print(x,y)
#define DEBUG_Println(x) Serial.println(x)
#else
#define DEBUG_Print(x)
#define DEBUG_PrintDec(x,y)
#define DEBUG_Println(x)
#endif
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'.', '0', '#', 'D'}
};
Keypad kpd = Keypad(makeKeymap(keys), rowPINS, colPINS, ROWS, COLS);
int moved;
long Multiplier;
long cumSteps = 0;
float Degrees = 0.; // Degrees from keypad
long ToMove = 0; // Steps to move
float bob = 0.; //this tracks current position in degrees
int cho = 0;
void setup()
{
//lcd.createChar(0, degss);
#ifdef DEBUG
Serial.begin(9600);
#endif
Multiplier = (long)TableRatio * StepsPerRotation;
lcd.init(); // initialize the lcd
pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
// Print welcome message to the LCD.
lcd.backlight();
lcd.print("Rotary Table Control");
lcd.setCursor(1, 2);
lcd.print("The Home Engineer.");
lcd.setCursor(8, 3);
lcd.print("2019");
//delay1 = 500;
//delay (delay1);
// fade in from min to max in increments of 5 points - indicator LEDs:
while (led_inc != 2) {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ind_green, fadeValue);
analogWrite(ind_red, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ind_green, fadeValue);
analogWrite(ind_red, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
led_inc = (led_inc + 1);
}
digitalWrite(ind_red, HIGH);
digitalWrite(ind_green, LOW);
lcd.init();
cho = 0;
// *************************************************
char key = kpd.getKey();
lcd.print("Degrees "); lcd.print(" = A");
lcd.setCursor(0, 1);
lcd.print("Divisions = B");
lcd.setCursor(0, 2);
lcd.print("JOG = C");
lcd.setCursor(0, 3);
lcd.print("Feed = D");
while (cho == 0)
{
key = kpd.getKey();
switch (key)
{
case NO_KEY:
break;
case 'A':
Degrees = getdegrees();
lcd.clear();
cho = 1;
break;
case 'B':
Degrees = getdivisions();
cho = 2;
break;
case 'C':
Degrees = getjog();
lcd.clear();
cho = 3;
break;
case 'D':
Degrees = getfeed();
lcd.clear();
cho = 4;
break;
} // end case
} // end while cho=0
DEBUG_Print("90 = 1"); DEBUG_Println(TableRatio);
DEBUG_Print("800 = 1"); DEBUG_Println(StepsPerRotation);
DEBUG_Print("800 = 360"); DEBUG_Println(Multiplier);
} // end setup
void loop() // MAIN LOOP
{
lcd.clear();
char key = kpd.getKey();
bob = 0;
cumSteps = 0;
lcd.setCursor(7, 0); lcd.print("Total: "); lcd.print(" "); lcd.setCursor(14, 0); lcd.print(bob, 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(abs(Degrees), 2); lcd.print((char)223);
key = kpd.getKey();
if (key == 'A') // FORWARD
{
bob = bob + Degrees;
ToMove = (bob * Multiplier) / 360 + 0.5 - cumSteps;
cumSteps = cumSteps + ToMove;
digitalWrite(dir, LOW);
printadvance();
}
if (key == 'B') // REVERSE
{
bob = bob - Degrees;
ToMove = -1*((bob * Multiplier) / 360 + 0.5 - cumSteps);
cumSteps = cumSteps - ToMove;
digitalWrite(dir, HIGH); // pin 13
printadvance();
}
} // end while not C loop
lcd.init();
setup();
} // end main VOID
float getjog() // used to set mechanical ratio and steps per revolution
{
digitalWrite(ind_red, HIGH);
digitalWrite(ind_green, LOW);
float Degrees = 0;
float num = 0.00;
char key = kpd.getKey();
lcd.clear();
lcd.setCursor(6, 0); lcd.print("Jogging");
lcd.setCursor(0, 1); lcd.print("A=0.5 B=1 C=5 Deg's ");
lcd.setCursor(0, 2); lcd.print("Choose 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':
Degrees = 0.5;
lcd.setCursor(16, 2); lcd.print(Degrees, 1);
break;
case 'B':
Degrees = 1;
lcd.setCursor(16, 2); lcd.print(Degrees, 1);
break;
case 'C':
Degrees = 5;
lcd.setCursor(16, 2); lcd.print(Degrees, 1);
break;
case 'D':
num = 0.00;
lcd.setCursor(15, 2); lcd.print(" ");
lcd.setCursor(15, 2);
break;
}
key = kpd.getKey();
}
return Degrees;
}
float getdivisions()
{
digitalWrite(ind_green, HIGH);
digitalWrite(ind_red, LOW);
lcd.noBlink();
float Degrees = 0;
float num = 0.00;
char key = kpd.getKey();
lcd.clear();
lcd.blink();
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 '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
num = num * 10 + (key - '0');
lcd.print(key);
break;
case 'D':
num = 0.00;
lcd.setCursor(16, 1); lcd.print(" ");
lcd.setCursor(16, 1);
break;
}
Degrees = 360 / num;
key = kpd.getKey();
}
return Degrees; //num;
}
float getfeed()
{
digitalWrite(ind_green, HIGH);
digitalWrite(ind_red, LOW);
lcd.noBlink();
//int key = 0;
float num = 0.00;
float decimal = 0.00;
float decnum = 0.00;
int counter = 0;
lcd.clear();
//lcd.init();
lcd.setCursor(2, 0);
lcd.print("Use potentiometer");
lcd.setCursor(2, 1);
lcd.print("to set the speed");
lcd.setCursor(5, 2);
lcd.print("of rotation");
delay(1500);
lcd.clear();
char key = kpd.getKey();
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);
lcd.blink();
bool decOffset = false;
while (key != '#')
{
switch (key)
{
case NO_KEY:
break;
case '.':
if (!decOffset)
{
decOffset = true;
}
lcd.print(key);
break;
case 'D':
num = 0.00;
lcd.setCursor(15, 1); lcd.print(" ");
lcd.setCursor(15, 1);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (!decOffset)
{
num = num * 10 + (key - '0');
lcd.print(key);
}
else if ((decOffset) && (counter <= 1))
{
num = num * 10 + (key - '0');
lcd.print(key);
counter++;
}
break;
} //end case
decnum = num / pow(10, counter);
key = kpd.getKey();
} //end while not #
return decnum;
} // end getdegrees
float getdegrees()
{
digitalWrite(ind_red, HIGH);
digitalWrite(ind_green, LOW);
lcd.noBlink();
//int key = 0;
float num = 0.00;
float decimal = 0.00;
float decnum = 0.00;
int counter = 0;
lcd.clear();
//lcd.init();
char key = kpd.getKey();
lcd.blink();
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);
bool decOffset = false;
while (key != '#')
{
switch (key)
{
case NO_KEY:
break;
case '.':
if (!decOffset)
{
decOffset = true;
}
lcd.print(key);
break;
case 'D':
num = 0.00;
lcd.setCursor(15, 1); lcd.print(" ");
lcd.setCursor(15, 1);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (!decOffset)
{
num = num * 10 + (key - '0');
lcd.print(key);
}
else if ((decOffset) && (counter <= 1))
{
num = num * 10 + (key - '0');
lcd.print(key);
counter++;
}
break;
} //end case
decnum = num / pow(10, counter);
key = kpd.getKey();
} //end while not #
return decnum;
} // end getdegrees
void printadvance() // print function
{
lcd.noBlink();
lcd.setCursor(6, 1); lcd.print("Moving");
lcd.setCursor(14, 0); lcd.print(bob, 2);
rotation(ToMove, 0);
lcd.setCursor(6, 1); lcd.print(" ");
}
void rotation(long tm, int d)
{
DEBUG_Print("Total Degrees = "); DEBUG_PrintDec(bob, 3); DEBUG_Print(" Steps to move = "); DEBUG_Print(tm); DEBUG_Print(" Total Steps = "); DEBUG_Println(cumSteps);
if (cho == 4 || cho == 2) {
for (long i = 0; i < tm; i++) //tm == To Movement
{
//pot_movement = analogRead(pot);
pot_movement_mapped = map (analogRead(pot), 0, 1023, 0, adj_speed);
if(pot_movement_mapped<(adj_speed-feed_pot_thresh)){//only move if the pot is above a threshold
digitalWrite(stp, HIGH);
delayMicroseconds(pot_movement_mapped);
digitalWrite(stp, LOW);
delayMicroseconds(pot_movement_mapped);
}
else{//decrement i if the motor hasn't been moved to ensure full motion is acheived
i--;
}
}
}
else {
for (long i = 0; i < tm; i++) //tm == Total Movement
{
digitalWrite(stp, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stp, LOW);
delayMicroseconds(stepdelay);
}
}
}
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile (" jmp 0");
}

LinkBack URL
About LinkBacks

Reply With Quote

Bookmarks