Lathe threading problem

Hi guys,
I am new in here.I try to drive step motor with encoder.Encoder is 1000 pulse and has 2 output.I use only a output.I want to make treading system for dıy lathe.one revolution of the stepper motor 200 pulses.When arduino receives 25 pulse by the encoder,I want that step motor goes 1 pulse as way.The encoder turn 1 spin =1000 pulse, Step motor goes to 40 pulse that same as 2mm per revelotion
My mill 300 rpm ,step motor must be 60 rpm.
But I have a problem that code is so slow.
AND receiving fast that encoder, Arduino makes fault.

int counter = 0;
#define dirPin 8
#define stepPin 9

void setup() {
Serial.begin (9600);
pinMode(2, INPUT_PULLUP); // Encoderden çıkan yeşil yada sarı kabloyu Arduinonun 2. pinine
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, HIGH);
attachInterrupt(digitalPinToInterrupt(2), Interrupt_function, RISING);

}

void Interrupt_function() {
counter++;
Serial.println (counter);
if( counter >= 25){
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
counter=0;
}
}

void loop() {
}

Hi,
Take most of the code out of the interrupt function.
Just have

counter++;

in the interrupt function.

Put the rest of your code in the void loop() section.

Tom... :smiley: :+1: :coffee: :australia:

Visit Youtuber Clough42! He has developed an electronic leadscrew system.
According to his analysis You waist Your time trying this on an Arduino.

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

Never inside an ISR!

Better still use the counter instead of the delays to phase the outputs:

void Interrupt_function() 
{
  counter = (counter+1) % 26 ;   // assuming divide/remainder is fast
  digitalWrite (stepPin, counter < 13) ;
}

Much simpler and faster, this is just a divide-by-26 operation.

Before you get too far along in this project. Be aware that using a stepper motor anywhere in a lathe will cause "chatter" while cutting metal. ANY pulsing anywhere will cause the cutting force to vary plus and minus, which is the definition of chatter. Use a DC motor and control it.
Paul

The guy, Clough42, used a continuous servo motor. Commutator DC motors have a similar none linear movement named "cogging".

I've always have wanted to make CNC lathe. Do you have pictures of your lathe project?

That is why so many lathe conversions have used DC motors from treadmills.
Paul

Hi,

Have you seem how many they must sell on home shopping channels.
There must be millions of them out there gathering garage dust.
Also they are a tough motor designed for high and very dynamic torque loads.

Tom.... :smiley: :+1: :coffee: :australia:

i see them listed quite often for free on FB Marketplace and Craig's List.
Paul