I am making a gear hobber out of a vertical mill.
I am installing an encoder on the spindle of a vertical mill to rotate a stepper on a dividing head with a 40:1 ratio. The stepper will have to spin at a ratio to the spindle of the mill.
1 tooth = 1 rotation of the spindle
40 full rotations of the stepper = 1 full rotation of the part
600 ppr on the encoder
200 steps per rotation on the stepper
1:3 encoder to stepper
I am fairly new to arduino but have pieced together some code that i think should work and seems to. The problem is that I feel that I am losing some crucial numbers in the remainders. I will post the code below. I need the Steps to be the last 0 + the remainder of the last step so that it can keep true.
Thanks to anyone that can help.
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 9, d5 = 8, d6 = 7, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define encoder_a 2 //encoder a = arduino pin 2
#define encoder_b 3 //encoder b = arduino pin 3
#define motor_step 4 //motor step = arduino pin 4
#define motor_direction 5 //motor direction = arduino pin 5
// constants won't change. They're used here to set pin numbers:
const int buttonup = 52; // the number of the pushbutton pin
const int buttondown = 53; // the number of the pushbutton pin
const int buttonadvance = 51; // the number of the pushbutton pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int numberofteeth = 80;
int buttonState3 = 0;
int rpm = 0;
int ratio = 0;
volatile long motor_position, encoder, encoderpos, remaining;
float steps = 0;
void setup ()
{
Serial.begin(9600);
// initialize the pushbutton pin as an input:
pinMode(buttonup, INPUT);
pinMode(buttondown, INPUT);
pinMode (buttonadvance, INPUT);
//set up the various outputs
pinMode(motor_step, OUTPUT);
pinMode(motor_direction, OUTPUT);
// then the encoder inputs
pinMode(encoder_a, INPUT);
pinMode(encoder_b, INPUT);
digitalWrite(encoder_a, HIGH);
digitalWrite(encoder_b, HIGH);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, encoderPinChangeA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, encoderPinChangeB, CHANGE);
encoder = 0;
}
void encoderPinChangeA()
{
if (digitalRead(encoder_a) == digitalRead(encoder_b))
{
encoder-= 1;
encoderpos-= 1;
}
else
{
encoder+= 1;
encoderpos+= 1;
}
}
void encoderPinChangeB()
{
if (digitalRead(encoder_a) != digitalRead(encoder_b))
{
encoder-= 1;
encoderpos-= 1;
}
else
{
encoder+= 1;
encoderpos+= 1;
}
}
void loop()
{
steps = encoderpos / 3 * 40 / numberofteeth + remaining;
if (steps > 1)
{
digitalWrite(motor_direction, HIGH);
digitalWrite(motor_step, HIGH);
digitalWrite(motor_step, LOW);
motor_position++;
encoder = 0;
encoderpos = 0;
}
else if (steps < -1)
{
digitalWrite(motor_direction, LOW);
digitalWrite(motor_step, HIGH);
digitalWrite(motor_step, LOW);
motor_position--;
encoder = 0;
encoderpos = 0;
}
}