Understanding the rotary table code

have constructed a rotary table and i am trying to understand exactly what these lines refer to. I am using a Nema 23, 1.8 deg stepper.

I have a toothed belt connection. The motor turns 6 times to achieve on complete turn of the chuck (360 degrees)-motor cog has 10 teeth drives a 60 tooth cog.

First line is the stepper driver setting of 400 microsteps
Second line is the ratio of driven turns/Driver turns 60 divide by 10 equals 6
Third line is the determined by what exactly? and in my case should be ?

const int StepsPerRotation = 400; const int TableRatio = 6; const int Multiplier = (StepsPerRotation * TableRatio)/50; 

I appreciate any help! Thanks

You forgot to tell us exactly HOW Multiplier is used in the program! That will answer your question.

I am a bit inexperienced..
these lines/

 const int stepdelay = 1; float Degrees = 0; // Degrees from Serial input float ToMove = 0; // Steps to move float bob = 0; int cho = 0; void setup() 

Apparently you have a top secret code

Seemingly the code wants to divide the table into 50 equal segments (7.2 degrees of rotation each), but without the full code noone can tell you why 50 is used, or why the decision was made to use a gear ratio of 10 to 60 teeth.

You will get meaningful answers if you post the entire code, otherwise all responses are guesses.

No secret :slight_smile:
Here is the full code

#include <Mouse.h> #include <Keyboard.h> #include <KeyboardLayout.h> #include <Keyboard_da_DK.h> #include <Keyboard_de_DE.h> #include <Keyboard_es_ES.h> #include <Keyboard_fr_FR.h> #include <Keyboard_hu_HU.h> #include <Keyboard_it_IT.h> #include <Keyboard_pt_PT.h> #include <Keyboard_sv_SE.h> #include <Keyboard.h> #include <KeyboardLayout.h> #include <Keyboard_da_DK.h> #include <Keyboard_de_DE.h> #include <Keyboard_es_ES.h> #include <Keyboard_fr_FR.h> #include <Keyboard_hu_HU.h> #include <Keyboard_it_IT.h> #include <Keyboard_pt_PT.h> #include <Keyboard_sv_SE.h> /* 4x4 matrix keypad amd a 20 x 4 LCD. Edit StepsPerRotation & TableRatio(# of turns for 360 degrees)in line 29 A4988 Stepstick/Pololu driver 5/2/2015 */ #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <Keypad.h> const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = { {'D','C','B','A'}, {'#','9','6','3'}, {'0','8','5','2'}, {'*','7','4','1'} }; 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(0x27,20,4); // set the LCD address to 0x20 for a 16 chars and 4 line display // SCL - A5, SDA - A4, VCC - +5, Gnd - Gnd const int stp = 2; // connect pin 2 to step const int dir = 3; // connect pin 3 to dir const int StepsPerRotation = 400; // Set Steps per rotation of stepper NOTE the driver is set to Half step was 400 const int TableRatio = 6; //tonio motor turns 6 times to turn chuck cog 1 rev const int Multiplier = (StepsPerRotation * TableRatio)/60; // 400*90=36000/360 = 100 set by tonio const int stepdelay = 1; float Degrees = 0; // Degrees from Serial input float ToMove = 0; // Steps to move float bob = 0; int cho = 0; void setup() { lcd.init(); // initialize the lcd pinMode(stp, OUTPUT); pinMode(dir, OUTPUT); // Print welcome message to the LCD. //lcd.backlight(); //lcd.print("ROTARY TABLE INDEXER"); // lcd.setCursor(4,2); //lcd.print(" "); //lcd.setCursor(3,3); //lcd.print("3D TECHNOLOGIES"); lcd.backlight(); lcd.print("ROTARY TABLE INDEXER"); lcd.setCursor(4,1);lcd.print("MENDELEVIUM"); lcd.setCursor(2,2);lcd.print("3D TECHNOLOGIES"); lcd.setCursor(4,3);lcd.print("DESIGN_2025"); delay(2000); lcd.init(); cho = 0; char key = kpd.getKey(); lcd.print("ENTER SELECTION:"); lcd.setCursor(0,1); lcd.print("DEGREES = PRESS-A"); lcd.setCursor(0,2); lcd.print("DIVISIONS = PRESS-B"); lcd.setCursor(0,3); lcd.print("JOGGING = PRESS-C"); 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; } // end case } // end while cho=0 } // end setup void loop() // MAIN LOOP { lcd.clear(); char key = kpd.getKey(); bob = 0; // lcd.setCursor(7,0);lcd.print("Total: ");lcd.print(bob,2); // total steps // lcd.setCursor(0,1);lcd.print(" A FORWARD"); // lcd.setCursor(0,2);lcd.print(" B REVERSE"); //lcd.setCursor(0,3);lcd.print(" C RESET"); lcd.setCursor(0,1);lcd.print("INCR_ -ABS POS");// added by tonio // lcd.setCursor(0,3);lcd.print("FOR=A REV=B X=C"); // original code lcd.setCursor(0,3);lcd.print("A=FOR B=REV C=RESET"); //modified by tonio 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 = (Degrees*Multiplier); digitalWrite(dir, LOW); printadvance(); } if(key=='B') // REVERSE { bob = bob - Degrees; ToMove = (Degrees*Multiplier); digitalWrite(dir, HIGH); // pin 13 printadvance(); } } // end while not C loop lcd.init(); setup(); } // end main VOID float getjog() { float Degrees = 0; float num = 0.00; char key = kpd.getKey(); lcd.clear(); lcd.setCursor(3,0);lcd.print("JOGGING_CYCLE"); //lcd.setCursor(0,1);lcd.print("A=1 B=10 C=100 Steps"); //lcd.setCursor(0,1);lcd.print("STEPS-A=1, B=10, C=100"); lcd.setCursor(0,1);lcd.print("Ax1 Bx10 Cx100 STEPS"); lcd.setCursor(0,2);lcd.print("enter Degrees:");lcd.setCursor(0,3);lcd.print("OK = PRESS # ");lcd.print((char)60);lcd.print((char)45);lcd.print(" D"); while(key != '#') { switch (key) { case NO_KEY: break; case 'A': Degrees = 1; lcd.setCursor(14,2);lcd.print(Degrees); break; case 'B': Degrees = 10; lcd.setCursor(14,2);lcd.print(Degrees); break; case 'C': Degrees = 100; lcd.setCursor(14,2);lcd.print(Degrees); break; case 'D': num=0.00; lcd.setCursor(14,2);lcd.print(" "); lcd.setCursor(14,2); break; } key = kpd.getKey(); } return Degrees; } float getdivisions() { float Degrees = 0; float num = 0.00; char key = kpd.getKey(); 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 '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 getdegrees() { //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.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.setCursor(6,1);lcd.print("Moving");//original code lcd.setCursor(5,1);lcd.print("TURNING");//modified by tonio lcd.setCursor(4,2);lcd.print("Steps ");lcd.print(ToMove,0); lcd.setCursor(13,0);lcd.print(bob,2); rotation(ToMove,0); lcd.setCursor(6,1);lcd.print(" "); } void rotation(float tm, int d) { for(int i = 0; i < tm; i++) { digitalWrite(stp, HIGH); delay(stepdelay); digitalWrite(stp, LOW); delay(stepdelay); } } void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers { asm volatile (" jmp 0"); } 

definitely my settings are incorrect.

A side note, not focusing to the main question...

Why did you include this in the code ( and moreover - included it twice?).
The Keyboard.h has nothing to do with your 4x4 matrix keypad code, that you used in the sketch.
What is your Arduino board?

Are not able to find where Multiplier is used in YOUR code?

The comment shows someone's calculation of how many steps per degree, in your case the code is calculating the number of steps per six degrees (a full 360 degrees divided into 60 segments).

 const int StepsPerRotation = 400; // Set Steps per rotation of stepper NOTE the driver is set to Half step was 400 const int TableRatio = 6; //tonio motor turns 6 times to turn chuck cog 1 rev const int Multiplier = (StepsPerRotation * TableRatio) / 60; // 400*90=36000/360 = 100 set by tonio

It looks like you may want the steps per degree for this code, why you changed the calculation for Multiplier no one knows.

 lcd.setCursor(0, 1); lcd.print("INCR_ -ABS POS"); // added by tonio // lcd.setCursor(0,3);lcd.print("FOR=A REV=B X=C"); // original code lcd.setCursor(0, 3); lcd.print("A=FOR B=REV C=RESET"); //modified by tonio 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 = (Degrees * Multiplier); digitalWrite(dir, LOW); printadvance(); } if (key == 'B') // REVERSE { bob = bob - Degrees; ToMove = (Degrees * Multiplier); digitalWrite(dir, HIGH); // pin 13 printadvance(); } } // end while not C loop 

I am using a mega 2560

The Mega2560 board can't use Keyboard.h and Mouse.h libraries. So there is no point to include it in your code.

I believe the movement is controlled by the three lines right?
Correct me if I am wrong.....
first line sets the stepper driver setting (400 microsteps)
Second line sets the reduction 1:6
The third line I am at a loss how to set up... The values in the sketch are not correct as when I set command a number of degrees the output is not right.

They were in the original sketch I obtained...
I will take them off then..

There is a line:
ToMove = (Degrees*Multiplier);
This seems to calculate the number of steps to take to move a certain number of degrees.

Thus 'Multiplier' is the number of steps to move one degree.
It would be calculated from the following:
const int Multiplier = (StepsPerRotation * TableRatio)/360;

this line const int Multiplier = (StepsPerRotation * TableRatio)/360;
should not be altered in any way?

No,
I am suggesting that the line that is currently:

const int Multiplier = (StepsPerRotation * TableRatio)/60; // 400*90=36000/360 = 100 set by tonio 

should be changed to:

const int Multiplier = (StepsPerRotation * TableRatio)/360; 

This is an int variable with int arithmetic. Have you printed this out anywhere to see what it actually produces. I will suspect it is not what you think it will be.

2 Likes

Definitely the first line is incorrect. Because the stepper is a 1.8 degree stepper motor and 360/1.8 is equal to 200 am I Right?
that value should be 200 not 400....

const int StepsPerRotation = 400; 

The code comments specify the stepper is being used in half-step mode, which doubles the steps/revolution to 400. Full step mode would be 200.

The problem @Grumpy_Mike is referring to is that 360 does not divide evenly into StepsPerRotation * TableRatio, so the Multiplier is slightly off.

1 Like