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
}