1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
| #include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>
String ProgramName = "Rotary Table Control"; // Name of this program
String VersionNumber = "V2-Rev4.69B FRED"; // The version Number of this program
// SET indicates a variable that will be stored in EEPROM if changed.
// The variable settings below will be used unless changed via the
// program Settings function.
int stepsPerFullRotation = 200; // SET Number of *full* steps per full 360 degree rotation for your motor
int microStepping = 1; // SET microsteps per full step; 1,2,4,8,16,etc.
float TableRatio = 1.0; // SET TableRatio for indexer = 26.851239669 for my hardware
int backlash = 0; // SET Number of microsteps needed to cancel out backlash
boolean backlashOn = false; // SET backlash correction is switched on (true) or off (false)
// - set to 0 if no backlash correction is desired
boolean beepStart = false; // SET beep on start of a move (true=ON, false=OFF)
boolean beepHalt = false; // SET beep on completion of a move (true=ON, false=OFF)
boolean start = 1;
boolean halt = 0;
byte beepPin = 13; // the pin to which the beeper is connected
int beepFrequency = 3800; // the beep audio frequency
float MicroStepsPerFullRotation = 0.0; // actual value calculated when values saved/retrieved from EEPROM
//stepper motor settings
/* SET maximum stepper speed.
Per the AccelStepper documentation: "The fastest motor
speed that can be reliably supported is about 4000 steps per second
at a clock frequency of 16 MHz on Arduino such as Uno etc.
*/
float StepperMaximumSpeed = 500.0; // SET maximum allowed stepper speed
float StepperAcceleration = 100.0; // SET acceleration rate for stepper motor
int percentMaxSpeed = 100; // SET percentage of maximum speed to use for reduced speed setting
int continuousRunStopMode = 1; // SET the method for stopping continuous running (see program comments above)
String stopMode[] = {"stopkey", "keypress", "reset" }; //Names of the stop modes
String prompt = "";
int clockDir = 1; // Direction of rotation initially set to clockWise
int clockWise = 1; // clockwise direction
int counterClockWise = -1; // counter-clockwise direction
boolean stopFlag = false; // flag to show whether the stopKey was used to stop the motor
// Set up key pad
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};
Keypad kpd = Keypad(makeKeymap(keys), rowPINS, colPINS, ROWS, COLS);
LiquidCrystal_I2C lcd(0x3F, 20, 4); // set the LCD address to 0x20 for a 16 chars and 4 line display
// some displays may use 0x3F for the address.
// Connections: SCL->A5, SDA->A4, VCC->+5, Gnd->Gnd
// Relay control
byte relayPin = 12; // set pin 12 for relay
float RelayDelay = 2000; // waiting time (milliseconds) after when relay set on and before continue the program
// stepper motor
const int stp = 2; // connect pin 2 to step
const int dir = 3; // connect pin 3 to dir
// Define a stepper and the pins it will use
AccelStepper stepper(1, stp, dir);
float Degrees = 0.0; // Number of degrees in a move
float TotalDegrees = 0.0; // total Number of degrees moved
float StepsPerIncrementTheoretical = 0.0; // Number of *theoretical* microsteps to move (can be a fractional Number)
float TotalStepsTheoretical = 0.0; // Theoretical total Number of steps moved
long stepToMoveTo = 0; // Target step value for the next move
// This information is needed for backlash correction. On initial start-up,
// the clockDir is undefined.
char key = kpd.getKey();
//=====================================================================================================
//=====================================================================================================
void setup() {
//debug Serial.begin( 9600 );
//debug Serial.println("ready");
getSettings(); // Read the stored program settings
pinMode(relayPin, OUTPUT); // Set relayPin mode to OUTPUT
digitalWrite(relayPin, HIGH); // Assumes active LOW relay board; set relay off before start of program
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on backlight
// Print welcome message and greeting menu to the LCD.
lcd.setCursor(0, 0); lcd.print(ProgramName);
lcd.setCursor(0, 1); lcd.print(VersionNumber);
lcd.setCursor(0, 2); lcd.print("Initial rotation?");
lcd.setCursor(0, 3); lcd.print("#=EXIT A=CW B=CCW");
makeBeep(halt);
stepperInitialize(); //reset stepper settings, steps and degrees to initial values
choiceInitialize(); //select choice from greeting menu
} //end Setup
//=====================================================================================================
//=====================================================================================================
void loop() {
stepperInitialize(); //reset stepper settings, steps and degrees to initial values
mainMenu(); //display the main menu
/*
Main menu choice values are:
key value
--- -------------------------
1 move by degrees
3 move by sides or teeth
4 jog move
6 arc move
end set degrees
end set jog
7 continuous move
9 update settings
*/
key = NO_KEY; //clear exit from previous menu
while (key != '#') //select choice from main menu
{
key = kpd.getKey();
switch (key)
{
case NO_KEY:
break;
case '1': //Move by degrees
Degrees = updateNumber("Degrees per move", 0, 0);
StepsPerIncrementTheoretical = Degrees / 360.0 * MicroStepsPerFullRotation;
indexMove();
key = '#'; //exit this loop
break;
case '3': //Move by sides
Degrees = 360.0 / (float) updateNumber("Sides or Teeth", 0, 0);
StepsPerIncrementTheoretical = Degrees / 360.0 * MicroStepsPerFullRotation;
indexMove();
key = '#'; //exit this loop
break;
case '4': //Jog move
getJog();
key = '#'; //exit this loop
break;
case '6': //Arc movement
choiceArc();
key = '#'; //exit this loop
break;
case '7': //Continuous movement
choiceContinuous(); //choose options for continuous running
key = '#'; //exit this loop
break;
case '9': // Update Settings
Settings();
key = '#'; //exit this loop
break;
}
} //end switchcase
} //end loop
//=====================================================================================================
//=====================================================================================================
/* updateNumber gets a float value with the prompt string provided. If a new number is not
entered then the current value of the float is returned.
To get an integer Number use
int getNumber("prompt", float someinteger, 0);
Screen display looks like this:
+--------------------+
| Provided prompt str|
| Value = |
| |
| #=ENTER D=CLEAR|
+--------------------+
*/
float updateNumber(String prompt, float FloatValue, int decPlaces) //decPlaces is the number of decimal places to display
{
float Num = 0.0;
float Decimal = 0.0;
float DecNum = 0.0;
int counter = 0;
lcd.clear();
lcd.print(prompt);
lcd.setCursor(0, 1); lcd.print("Value = ");
lcd.print(FloatValue, decPlaces);
lcd.setCursor(0, 3); lcd.print("#=EXIT D=CLEAR");
lcd.setCursor(8, 1);
boolean decOffset = false;
char key = kpd.getKey();
while (key != '#')
{
switch (key)
{
case NO_KEY:
break;
case '.':
if (!decOffset)
{
decOffset = true; //Set Decimal flag and print Decimal point (but not if already printed)
lcd.print(key);
}
break;
case 'D': //Clear data entry and reset Decimal point flag
Num = 0.0;
lcd.setCursor(8, 1);
lcd.print(" "); //clear data field with spaces
lcd.setCursor(8, 1);
decOffset = false;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (Num == 0.0) //Clear data entry field if new entry
{
lcd.setCursor(8, 1);
lcd.print(" "); //clear data field with spaces
lcd.setCursor(8, 1);
}
if (!decOffset)
{
Num = Num * 10.0 + (key - '0');
lcd.print(key);
}
else if ((decOffset) && (counter <= 4)) //Counter Number = Number of Decimal places minus one
{
Num = Num * 10.0 + (key - '0');
lcd.print(key);
counter++;
}
break;
} //end switchcase
DecNum = Num / pow(10, counter);
key = kpd.getKey();
} //end while not #
if (DecNum != 0) {
return DecNum; //return new number if one is entered
}
else
{
return FloatValue; //return the original number if no change
}
return DecNum;
} //end getNumber
// getBoolean toggles a yes/no or true/false and returns the result.
boolean getBoolean(String prompt, boolean flip)
{
lcd.clear();
lcd.home();
lcd.print(prompt);
lcd.setCursor(0, 1); lcd.print("Value = ");
lcd.setCursor(8, 1); if (flip) { //If the beep setting is currently set to True
lcd.print("ON ");
} else {
lcd.print("OFF");
}
lcd.setCursor(0, 3); lcd.print("#=EXIT D=Toggle");
char key = kpd.getKey();
while (key != '#')
{
switch (key)
{
case NO_KEY:
break;
case 'D': // Toggle boolean value
flip = !flip;
lcd.setCursor(8, 1); if (flip) {
lcd.print("ON ");
} else {
lcd.print("OFF");
}
break;
} //end switchcase
key = kpd.getKey();
} //end while
return flip;
} //end getBoolean
void mainMenu() //The initial menu for the system
{
lcd.clear();
lcd.setCursor(0, 0); lcd.print("1: Degrees 3: Sides");
lcd.setCursor(0, 1); lcd.print("4: Jog 6: Arc" );
lcd.setCursor(0, 2); lcd.print("7: Continuous");
lcd.setCursor(0, 3); lcd.print("9: Settings" );
} //end mainMenu
void choiceInitialize() //select choice from greeting menu
{
while (key != '#')
{
key = kpd.getKey();
switch (key)
{
case NO_KEY:
break;
case 'A': //Move one full rotation CW
{
stepperInitialize();
TotalStepsTheoretical = MicroStepsPerFullRotation;
moveToTarget();
break;
}
case 'B': //Move one full rotation CCW
{
stepperInitialize();
TotalStepsTheoretical = -MicroStepsPerFullRotation;
moveToTarget();
break;
}
} // end case
} // end while choice != #
} //end choiceInitialize
void jogMenu() //Menu for jog entry - used for jog move and arc end setting by jog
{
lcd.clear();
lcd.setCursor(0, 0); lcd.print("JOG sum=");
if (backlashOn) {
lcd.print(char(127));
}; //Include a back-arrow symbol if backlash is on
//print the current steps and degrees totals
lcd.setCursor(10, 0); lcd.print(" ");
lcd.setCursor(10, 0); lcd.print(TotalStepsTheoretical, 0);
/* */lcd.print(" "); lcd.print(TotalDegrees, 1); lcd.print((char)223);
lcd.setCursor(0, 1);
for (int i = 1; i < 4; i++)
{
lcd.print((float)i, 0); lcd.print("=+"); lcd.print((float)pow(10, i - 1), 0); lcd.print(" ");
}
lcd.setCursor(0, 2);
for (int i = 4; i < 7; i++)
{
lcd.print((float)i, 0); lcd.print("=-"); lcd.print((float)pow(10, i - 4), 0); lcd.print(" ");
}
lcd.setCursor(0, 3); lcd.print("#=EXIT");
}
void getJog() //Calculate jog movements
{
jogMenu(); //display the jog menu
char key = kpd.getKey();
while (key != '#')
{
key = kpd.getKey();
switch (key)
{
case NO_KEY: break;
case '1': case '2': case '3':
{
StepsPerIncrementTheoretical = pow(10.0, (float)(key - '0') - 1.0);
TotalStepsTheoretical = TotalStepsTheoretical + StepsPerIncrementTheoretical;
TotalDegrees = (TotalStepsTheoretical / MicroStepsPerFullRotation) * 360.0;
lcd.setCursor(10, 0); lcd.print(" ");
lcd.setCursor(10, 0); lcd.print(TotalStepsTheoretical, 0);
/* */lcd.print(" "); lcd.print(TotalDegrees, 1); lcd.print((char)223);
moveToTarget();
break;
}
case '4': case '5': case '6':
{
StepsPerIncrementTheoretical = -(pow(10.0, (float)(key - '0') - 4.0));
TotalStepsTheoretical = TotalStepsTheoretical + StepsPerIncrementTheoretical;
TotalDegrees = (TotalStepsTheoretical / MicroStepsPerFullRotation) * 360.0;
lcd.setCursor(10, 0); lcd.print(" ");
lcd.setCursor(10, 0); lcd.print(TotalStepsTheoretical, 0);
/* */lcd.print(" "); lcd.print(TotalDegrees, 1); lcd.print((char)223);
moveToTarget();
break;
}
case '#':
break;
}
} //end while
} //end getJog
void continuousMenu()
{
//LCD LINE 0
lcd.clear();
lcd.setCursor(0, 0); lcd.print("CONTINUOUS ["); lcd.print((String) stopMode[continuousRunStopMode]); lcd.print(']');
//LCD LINE 1 - note extra spaces to clear data fields
lcd.setCursor(0, 1); lcd.print("sp= ");
lcd.setCursor(3, 1); lcd.print(float (percentMaxSpeed), 0); lcd.print("%= ");
lcd.setCursor(8, 1); lcd.print( (percentMaxSpeed / 100.0) * StepperMaximumSpeed, 0);
lcd.setCursor(13, 1); lcd.print("ac= ");
lcd.setCursor(16, 1); lcd.print(float (StepperAcceleration), 0);
//LCD LINE 2
lcd.setCursor(0, 2); lcd.print("C=settings ");
if (continuousRunStopMode < 2) //If stop mode != reset then print stop-key option
{
lcd.setCursor(14, 2); lcd.print("D=STOP");
}
else
{
lcd.setCursor(13, 2); lcd.print(":reset:");
}
//LCD LINE 3
lcd.setCursor(0, 3); lcd.print("A=CW B=CCW ");
if (continuousRunStopMode < 2) //If stop mode != reset then print EXIT option
{
lcd.setCursor(14, 3); lcd.print("#=EXIT");
}
else
{
lcd.setCursor(13, 3); lcd.print(": STOP:");
}
}
void choiceContinuous()
{
continuousMenu();
while (key != '#') //select choice from continuousMenu
{
key = kpd.getKey();
switch (key)
{
case NO_KEY:
break;
case 'A': //Move clockWise
{
//Reset the maximum stepper speed as a percentage of the stored maximum
//StepperContinuousSpeed = (percentMaxSpeed / 100.0) * StepperMaximumSpeed;
accelerateThenRun(StepperAcceleration, (percentMaxSpeed / 100.0) * StepperMaximumSpeed , clockWise, 'D');
makeBeep(2);
break;
}
case 'B': //Move counterClockWise
{
//Reset the maximum stepper speed as a percentage of the stored maximum
//StepperContinuousSpeed = (percentMaxSpeed / 100.0) * StepperMaximumSpeed;
accelerateThenRun(StepperAcceleration, (percentMaxSpeed / 100.0) * StepperMaximumSpeed , counterClockWise, 'D');
makeBeep(2);
break;
}
case 'C': //Set new percent of max speed
{
choiceSettingsContinuous();
continuousMenu();
key = NO_KEY; //clear # from settings menu
break;
} //end case C
case '#': //Exit
{
break;
}
} // end case
} // end while choice != #
} //end choiceContinuous()
//Sub-menu for settings in Continuous mode
void choiceSettingsContinuous()
{
//Set percent of maximum speed
percentMaxSpeed = updateNumber("% of max speed:", percentMaxSpeed, 0);
//Set acceleration value
StepperAcceleration = updateNumber("Acceleration rate:", StepperAcceleration, 0);
//Set stop mode
getStopMode();
putSettings(); //save the new values to permanent storage
} // end choiceSettingsContinuous
void arcMenu()
{
lcd.clear();
lcd.print("ARC Movement");
if (backlashOn) {
lcd.print(char(127));
}; //Include a back-arrow symbol if backlash is on
//if (TotalStepsTheoretical == 0) //show next rotation direction
//{
// lcd.setCursor(8, 1); lcd.print ("CW>>");
//}
//else
//{
lcd.setCursor(8, 1); lcd.print ("<CCW");
//}
lcd.setCursor(2, 1); lcd.print (0.0, 2); lcd.print((char)223); //print 0.0 start position
lcd.setCursor(14, 1); lcd.print (TotalStepsTheoretical / MicroStepsPerFullRotation * 360.0, 2); lcd.print((char)223); //print value
lcd.setCursor(0, 2); lcd.print ("end set: B=Deg C=Jog");
lcd.setCursor(0, 3); lcd.print ("#=EXIT A=GO");
}
void choiceArc()
{
arcMenu();
while (key != '#') //select choice for arc move
{
key = kpd.getKey();
switch (key)
{
case NO_KEY:
break;
case 'A': //Move back and forth in arc; current position is start position
{
if (TotalStepsTheoretical > 0) // If stepper is at end of arc
{
//clockDir = counterClockWise;
//TotalStepsTheoretical = -TotalStepsTheoretical; // Set next target position back to start of arc
StepsPerIncrementTheoretical = TotalStepsTheoretical; //save current value of StepsPerIncrementTheoretical
TotalStepsTheoretical = 0.0;
lcd.setCursor(8, 1); lcd.print ("CW>>"); // Set direction for *next* move
}
else //else stepper is at start of arc
{
//clockDir = clockWise;
//TotalStepsTheoretical = -TotalStepsTheoretical; // Reset target position back to TotalStepsTheoretical
TotalStepsTheoretical = StepsPerIncrementTheoretical;
lcd.setCursor(8, 1); lcd.print ("<CCW"); //Set direction for *next* move
}
moveToTarget(); //target is always TotalStepsTheoretical for moveToTarget
break;
}
case 'B': //Set arc end position by number of degrees
{
TotalDegrees = updateNumber("Degrees in arc/move", TotalDegrees, 2);
TotalStepsTheoretical = TotalDegrees / 360.0 * MicroStepsPerFullRotation;
StepsPerIncrementTheoretical = TotalStepsTheoretical; // save the current value of TotalStepsTheoretical
if (TotalDegrees != 0.0) {
moveToTarget(); //Move immediately to the degree position
}
arcMenu();
break;
}
case 'C': //Set arc end position by jog moving rotary table to desired position
{
getJog();
StepsPerIncrementTheoretical = TotalStepsTheoretical; // (debug - need this?) save the current value of TotalStepsTheoretical
arcMenu();
break;
} //end case C
case '#': //Exit
{
break;
}
} // end switchcase
} // end while != #
} //end choiceArc()
void Settings() //Get the basic settings for the stepper motor, gear reduction, etc.
//Display the current value for each setting
//If no new Number is entered, keep the current value
{
//Get Number of steps per full revolution of the stepper motor
String prompt = "Full steps in 360";
prompt += char(223); //degree symbol
prompt += ":";
stepsPerFullRotation = updateNumber(prompt, stepsPerFullRotation, 0);
//Get the micro-stepping setting
microStepping = updateNumber("Micro-stepping:", microStepping, 0);
//Get the table (gear) ratio
TableRatio = updateNumber("Table Gear Ratio:", TableRatio, 6);
//Get the backlash correction
//If a number is entered for backlash correction, it will be stored even if backlash correction is turned off.
backlashOn = getBoolean("Fix backlash? ", backlashOn);
if (backlashOn) // If backlash correction is switched on
{
backlash = updateNumber("Backlash amount:", backlash, 0);
}
//Set start beep on or off
beepStart = getBoolean("Beep on start?", beepStart);
//Set halt beep on or off
beepHalt = getBoolean("Beep on stop?", beepHalt);
//Get the maximum motor speed
StepperMaximumSpeed = updateNumber("Max motor speed:", StepperMaximumSpeed, 0);
//Get the motor acceleration
StepperAcceleration = updateNumber("Acceleration rate:", StepperAcceleration, 0);
//Get the % of maximum speed for continuous run
percentMaxSpeed = updateNumber("% of max speed:", percentMaxSpeed, 0);
//Set the delay before continue program after turning relay on
RelayDelay = updateNumber("Delay for relay:", RelayDelay, 0);
//Get the continuousRunStopMode
getStopMode();
putSettings(); // Save the new settings to EEPROM
makeBeep(halt);
} //end settings
void getStopMode()
{
lcd.clear();
lcd.print("Continuous-stop mode");
lcd.setCursor(0, 1); lcd.print("Mode =");
lcd.setCursor(0, 3); lcd.print("#=EXIT D=Toggle");
int i = continuousRunStopMode;
if (i < 0 || i > 2) {
i = 0; //If stray bits are read from storage, default to a true value
}
lcd.setCursor(8, 1); lcd.print(" "); //Print the current mode value
lcd.setCursor(8, 1); lcd.print((String) stopMode[i]);
key = NO_KEY; //Clear previous # keypress
while (key != '#')
{
key = kpd.getKey();
switch (key)
{
case 'D':
{
i = i + 1; //Increment i, if > 2 start over at 0
if (i > 2) {
i = 0;
}
continuousRunStopMode = i;
lcd.setCursor(8, 1); lcd.print(" ");
lcd.setCursor(8, 1); lcd.print((String) stopMode[i]);
}
case '#':
break;
}
}
}
// Relay control
// This is made for active LOW relay board. If you have a different setup then swap HIGH & LOW values
void relaySwitchedOn(boolean relayMode)
{
if (relayMode)
{
digitalWrite(relayPin, LOW); //Set relay on
delay(RelayDelay); //Waiting before continue program
}
else
{
delay(150); //Small delay before set relay off
digitalWrite(relayPin, HIGH); //Set relay off
}
} // end relaySwitchedOn
void getSettings()
{
EEPROM.get(0, stepsPerFullRotation); // 2 byte int
EEPROM.get(2, microStepping); // 2 byte int
EEPROM.get(4, TableRatio); // 4 byte float
EEPROM.get(8, backlash); // 2 byte int
EEPROM.get(10, backlashOn); // 1 byte boolean
EEPROM.get(11, beepStart); // 1 byte boolean
EEPROM.get(12, beepHalt); // 1 byte boolean
EEPROM.get(13, StepperMaximumSpeed); // 4 byte float
EEPROM.get(17, StepperAcceleration); // 4 byte float
EEPROM.get(21, percentMaxSpeed); // 2 byte int
EEPROM.get(23, continuousRunStopMode); // 2 byte int
EEPROM.get(25, RelayDelay); // 4 byte float
// Set the total theoretical number of microsteps required for a full 360 degree revolution:
MicroStepsPerFullRotation = (float) stepsPerFullRotation * (float) microStepping * TableRatio;
} //end getSettings
void putSettings()
{
EEPROM.put(0, stepsPerFullRotation); // 2 byte int
EEPROM.put(2, microStepping); // 2 byte int
EEPROM.put(4, TableRatio); // 4 byte float
EEPROM.put(8, backlash); // 2 byte int
EEPROM.put(10, backlashOn); // 1 byte boolean
EEPROM.put(11, beepStart); // 1 byte boolean
EEPROM.put(12, beepHalt); // 1 byte boolean
EEPROM.put(13, StepperMaximumSpeed); // 4 byte float
EEPROM.put(17, StepperAcceleration); // 4 byte float - also changed/stored via choiceContinuous function
EEPROM.put(21, percentMaxSpeed); // 2 byte int - also changed/stored via choiceContinuous function
EEPROM.put(23, continuousRunStopMode); // 2 byte int
EEPROM.put(25, RelayDelay); // 4 byte float
// Re-calculate the MicroStepsPerFullRotation from the updated values
MicroStepsPerFullRotation = (float) stepsPerFullRotation * (float) microStepping * TableRatio;
} //end putSettings
//Initialize the AccelStepper settings, and degree and step position variables
void stepperInitialize()
{
// Initialize variables
Degrees = 0.0;
TotalDegrees = 0.0;
stepToMoveTo = 0; //Target position for stepper.moveTo()
stepper.setCurrentPosition(0); // Set stepper current position and speed to zero
TotalStepsTheoretical = 0.0;
stepper.setMaxSpeed(StepperMaximumSpeed);
stepper.setAcceleration(StepperAcceleration);
}
// indexMove calculates the theoretical step position and the end of the next move
void indexMove()
{
lcd.clear();
lcd.print("INDEXING =");
if (backlashOn) {
lcd.print(char(127));
}; //Include a back-arrow symbol if backlash is on
lcd.setCursor(11, 0); lcd.print(Degrees, 2); lcd.print((char)223); //print degrees per index step
lcd.setCursor(0, 1); lcd.print("Position =");
lcd.setCursor(11, 1); lcd.print(TotalDegrees, 2); lcd.print((char)223); //print total degrees moved
lcd.setCursor(0, 3); lcd.print("#=EXIT A=CW B=CCW");
while (key != '#') // # will return to start menu
{
key = kpd.getKey();
if (key == 'A') // MOVE CLOCKWISE
{
TotalDegrees = TotalDegrees + Degrees;
// When making a full rotation, restart count at 360 degrees
TotalDegrees = ((TotalDegrees / 360.0) - int (TotalDegrees / 360.0)) * 360.0;
TotalStepsTheoretical = TotalStepsTheoretical + StepsPerIncrementTheoretical;
lcd.setCursor(6, 2); lcd.print("Moving");
lcd.setCursor(11, 1); lcd.print(" "); // clear print field
lcd.setCursor(11, 1);
lcd.print(TotalDegrees, 2); lcd.print((char)223);
moveToTarget();
lcd.setCursor(6, 2); lcd.print(" ");
}
if (key == 'B') // MOVE COUNTERCLOCKWISE
{
TotalDegrees = TotalDegrees - Degrees;
// When making a full rotation, restart count at 360 degrees
TotalDegrees = ((TotalDegrees / 360.0) - int (TotalDegrees / 360.0)) * 360.0;
TotalStepsTheoretical = TotalStepsTheoretical - StepsPerIncrementTheoretical; // For reverse moves steps are negative
lcd.setCursor(6, 2); lcd.print("Moving");
lcd.setCursor(11, 1); lcd.print(" "); // clear print field
lcd.setCursor(11, 1);
lcd.print(TotalDegrees, 2); lcd.print((char)223);
moveToTarget();
lcd.setCursor(6, 2); lcd.print(" ");
}
} // end while not # loop
}
// Update display and move to the a target step number; apply backlash correction if
// rotation has reversed and backlash correction is on. Backlash correction is applied
// by resetting the stepper.currentPosition by an amount equal to the backlash correction
void moveToTarget()
{
relaySwitchedOn(true); //set relay on
makeBeep(start);
//Check to see if reversing direction
if (clockDir == clockWise && TotalStepsTheoretical < stepper.currentPosition() ) //change from CW to CCW?
{
stepper.setCurrentPosition( stepper.currentPosition() + (backlashOn * backlash) ); //apply backlash correction
clockDir = counterClockWise;
}
if (clockDir == counterClockWise && TotalStepsTheoretical > stepper.currentPosition() ) //change from CCW to CW?
{
stepper.setCurrentPosition( stepper.currentPosition() - (backlashOn * backlash) ); //apply backlash correction
clockDir = clockWise;
}
/* debug include this code to enable stopKey
while ( stepper.isRunning() && kpd.getKey() != 'D') //loop until motor stops or stopKey is pressed
{
stepper.run();
}
if (stepper.speed() != 0.0) {
stopFlag = true; //If stopKey was used set the stopFlag
}
stepper.stop(); //Reset target for deceleration and then stop
*/
stepper.moveTo(TotalStepsTheoretical); //Set target position for move
stepper.runToPosition();
relaySwitchedOn(false);
makeBeep(halt);
} //end moveToTarget
//*makeBeep beeps the specified number of times if the beeper function is switched on.
void makeBeep( int mode)
{
if (mode == start && beepStart) //Beep at start if beep switched on
{
tone(beepPin, beepFrequency, 290); //tone(pin,frequency,duration-milliseconds)
delay(340);
}
if (mode == halt && beepHalt) //Beep at stop if beep switched on
{
tone(beepPin, beepFrequency, 290); //tone(pin,frequency,duration-milliseconds)
delay(340);
}
}
/*Accelerate to designated speed and then continue running until key is pressed
- if key is pressed then decelerate to a stop
- if key pressed while decelerating then immediate stop
The initial target position is the highest possible long int = 2,147,483,647
Ignoring the acceleration phase, and assuming a highest possible speed of 4,000 steps/second
this is
2,147,483,647/4,000 = 536870 seconds or approx 6.2 days
In other words, the motor will run "continuously" until the stop button is pushed, or stop by itself
in 6.2 days (longer for lower maximum speeds). While this is not true continuous running, it should
be a good compromise for rotary table use.
acceleration = the acceleration rate in steps/sec/sec
StepperMaximumSpeed = speed to accelerate to and then run at
clockDir = direction of rotation (CW=1, CCW=-1)
stopKey = designated key to press to stop running
When numsteps is negative rotation is reversed and speed becomes negative also; this is addressed
by the clockDir variable.
*/
void accelerateThenRun(float acceleration, float StepperContinuousSpeed, int rotateDirection, char stopKey)
{
makeBeep(start);
switch (continuousRunStopMode)
{
case 0: //stop with stopkey
relaySwitchedOn(true);
stepper.setCurrentPosition(0); //Initialize position and speed to zero for next start
stepper.move(long (rotateDirection * 2147483647)); //Set direction; set target position to number that will not be reached
stepper.setMaxSpeed(rotateDirection * StepperContinuousSpeed);
stepper.setAcceleration(acceleration);
while ( kpd.getKey() != stopKey ) //Press stop key to stop acceleration phase
{
stepper.run();
}
//decelerate to a stop
stepper.stop(); //Reset target for deceleration
stepper.runToPosition();
makeBeep(halt);
relaySwitchedOn(false);
break;
case 1: //stop with detect key press
//Variation Rev4.50b : Use a digital pin read to check for stop button in continuous run. You will need to hold the
// stop button down for 1 - 3 seconds. There is no immediate stop option, but there is deceleration to a stop.
{
relaySwitchedOn(true);
stepper.setCurrentPosition(0); //Initialize position and speed to zero for next start
stepper.move(long (rotateDirection * 2147483647)); //Set direction; set target position to number that will not be reached
stepper.setMaxSpeed(rotateDirection * StepperContinuousSpeed);
stepper.setAcceleration(acceleration);
//while ( digitalRead(4) == HIGH ) //Press stop key to stop acceleration phase
while ( PIND & 0b00010000) //Read pin 4 "directly" for fastest read
{
stepper.run();
}
//decelerate to a stop
stepper.stop(); //Reset target for deceleration
stepper.runToPosition();
makeBeep(halt);
}
relaySwitchedOn(false);
break;
case 2: //stop with reset
relaySwitchedOn(true);
stepper.setCurrentPosition(0); //Initialize position and speed to zero for next start
stepper.move(long (rotateDirection * 2147483647)); //Set direction; set target position to number that will not be reached
stepper.setMaxSpeed(rotateDirection * StepperContinuousSpeed);
stepper.setAcceleration(acceleration);
stepper.runToPosition();
relaySwitchedOn(false);
break;
} //end switch case
} |