tyhenry / CheapStepper

Arduino library for the cheap but decent 28BYJ-48 5v stepper motor with ULN2003 board
GNU General Public License v3.0
122 stars 46 forks source link

"Centering" the stepper motor #16

Open drp0 opened 4 years ago

drp0 commented 4 years ago

I wanted to set the start position of the stepper and attached an opto sensor to allow the stepper motor to be moved until a hole in a rotated cardboard disc, caused the opto to read Low. This all went to plan until I realised that the library initialised its position from the first call to CheapStepper stepper (8,9,10,11) . I realised I needed a new library call (stepReset) to set stepN = 0 and hence establish a new base position.

void CheapStepper::stepReset(){

      stepN = 0; 
}

This works well.

My centering code;


byte a =  digitalRead(12);  // opto sensor on pin 12
  if(a==1){
  Serial.println("Centering..");
    while(a == 1){
    stepper.move (moveClockwise, 1);
    a = digitalRead(12);
    }
  stepper.stepReset(); // reset position count
  Serial.println("Centred"); Serial.flush();
  stepper.newMove(moveClockwise, 0); // fixes a restart issue
  }

Others may find this useful,

David

stutteringp0et commented 2 years ago

Expanding on the description above...I'm not a C developer, so I had to fumble with this until I figured it out.

You'll need to edit the library .cpp and .h files.

in CheapStepper.cpp, add this to the end:

void CheapStepper::stepReset(){

      stepN = 0; 
}

in CheapStepper.h, I added this right after void stop();

void stepReset();

Once you've edited those two files, this modification is complete. Worked like a champ for me!

Thanks @drp0 and @tyhenry !

ukarakaya commented 2 years ago

I added one more feature for my project. I needed to set a certain position instead of zero (0) position. In CheapStepper.cpp:

void CheapStepper::stepReset(int SetStepPos){

      stepN = SetStepPos; 
}

and in the CheapStepper.h :

void stepReset(int SetStepPos);