Stepper motor homing issue with reflective IR sensor

I'm using an Arduino UNo to control astepper motor. I need to set a home position, as it is for a rev counter. I've got the motor running OK and connected an IR sensor and some reflective tape on the motor.
My plan is to use the setup() routine for initialisation, and the main for detecting RPM.
Step the motor anticlockwise until the sensor goes high (interrupt on pin 2)
Reset the interrupt
Step the motor clockwise 25 steps cockwise so the poiner is at zero.

It finds the home position correctly, but never goes clockwise!


// Include the Arduino Stepper Library
#include <Stepper.h>

// Number of steps per output rotation
const int stepsPerRevolution = 200;

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 11, 10, 9, 8);

void setup(){
pinMode (2, INPUT); // home1 sensor LED output
attachInterrupt(0, homeposition , RISING); // will generate interrupt when pin 2 is high

myStepper.setSpeed(20); // set the motor speed at 20 rpm:

myStepper.step (-200); //move stepper anticlockise until home sensor activated
delay(50);
}

void loop() {
// will fill this in later
}

void homeposition() {
detachInterrupt(0);
EIFR = 0x01; // external interrupt flag reset
delay (50);
myStepper.setSpeed (20);
myStepper.step (50); // now set pointer to 0
}

Your problem is likely caused by trying to move the stepper motor inside an interrupt function.
The approach to finding the starting point is faulty. Write a separate function to just test the pin and if not at the set point, move the stepper ONE step and do it all over again until you find the set point. Never try an interrupt while searching for the starting point of a stepper.
Paul

Hi Paul,

I'll sit and do some rewriting over the weekend.

I thought that using an interrupt would be a better option!

I'm still learning.

73
G3YSW

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.