Gear Hobbing with arduino

Hi everyone! I'm trying to make a gear hobbing machine with arduino, I wrote a code that seems to work, but on the machine it just can't make any gear. I think the synchronization between the two spindles are not correct. I think my code that has the problem. Anyone has any ideas how I can fix it?

int pinA = 3; // encoder A channel
int pinB = 2; // encoder B channel
//400 pulse/rotation

float encoder = 0;
float numberofclicks = 0;

#include <AccelStepper.h>
AccelStepper stepper(1, 4, 5);// step 4, dir 5


void setup()
{
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(pinA), pinAInterrupt, RISING); 

  

 
  
  stepper.setSpeed(2000000000); 
  stepper.setAcceleration(200000000000); 
  
}

void loop()
{

  
  stepper.moveTo(numberofclicks); 
 
  while (stepper.distanceToGo() != 0) 
  {
    stepper.runSpeedToPosition(); 
   
  }

  numberofclicks = map(encoder, 0, 400, 0, 40); //gear ratio change
 
  
}

void pinAInterrupt()
{
 

  if (digitalRead(pinA) == digitalRead(pinB)) 
  {
    encoder++; 
  }
  else 
  {
    encoder--; 
  }
}

Welcome to the forum

Please do not post pictures of code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Two BILLION RPM? Isn't that a little ambitious?

Now, that's ambitious :slight_smile: Is it fully DIY or did you start with a manual machine and automate it?

What part isn't working? If you're using a quadrature encoder, the method you're using to decode it won't give you all the pulses.

Sorry for the late reply. I started with a manual machine, and I tried to automate it. So the problem I'm having is that when I start the hobbing it seems good but after a few rotations of the secondary spindle (the spindle that is driven by the stepper), it starts to mess up the gear. If I left it to run on one position it just completly shears off the gear. And I'm using a quadrature encoder, can you help me how to decode all of the pulses?
273776747_359209272503769_4981822436369253784_n

Please give more detail about the manual gear hobbing machine. Can you point to the model you started with? Can you point to instructions on how to use it manually?

You have an encoder and a stepper. How are they attached to your manual gear hobbling machine?

Easiest way is probably to use the encoder library

Synchronizing two moving spindles is not going to be an simple task, however...

Especially hard when you ONLY have STEPS to work with, even if they may be small steps.

Maybe check on CNCZone to see if someone has automated a hobbing machine. In a sense it's a similar problem to an electronic leadscrew and I know they've done lots of that.