I am trying to make a step motor move for some steps every time a photosensor resistor triggered .
I have to use interupts so that I will be sure that arduino saw the photosensor
For my project I used the simple interrupt example of blinking a LED and I added my stepmotor rotattion for 100 steps.
The proplem is that the program freezes at first step of the step motor.
#include <Stepper.h>
int pin = 13;
volatile int state = LOW;
const int stepsPerRevolution = 48; //set step motor
Stepper mystepper(stepsPerRevolution, 8, 9, 10, 11); //pins for step motor
void setup()
{
mystepper.setSpeed(10);// set the speed of the motor to 30 RPMs
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, RISING);
}
void loop()
{
digitalWrite(pin, state);
}
void blink()
{
state = !state;
mystepper.step(100);
}
is there a problem with stepper.h library and interrupts ?
Oh dear - another newbie who does not understand what interrupt are for!
dalai:
I have to use interrupts so that I will be sure that Arduino saw the photo-sensor
No, you don't. You write your code so that it operates correctly in order to detect activity on the photo-sensor. This means that you do not lock up the processor executing "delay()" functions or "while" loops. In this case, only the process of moving the stepper will unduly delay matters but since you only do this as a consequence of detecting a change on the photo-sensor, it does not represent a problem.
If it was necessary to "keep an eye" on some sensor or other event whilst the stepping was in progress, then you would simply write the code to check the corresponding input in between steps. Just because a library is provided for performing a particular function does not mean that is the only or indeed, the "proper" way to perform that function in any given circumstance.
dalai:
The problem is that the program freezes at first step of the step motor.
As you would expect.
dalai:
is there a problem with stepper.h library and interrupts ?
No, no problem at all. This library however - as with many - uses interrupts. As a general rule, you should not be using library calls in interrupt routines.
The point is that you only require an interrupt when it must be serviced very quickly (microseconds, not milliseconds) and will be serviced within microseconds. Clearly your situation is not this.
thank you for your answer .It was very helpful to me
My project is a knife that moving up an down and cuts a material to slices(many pages of paper).The step motor will move the material 1 to 3 mm foward so the knife cuts it . Every time the knife finishes one cut ,the motor will start to move .
I thote that this movemend was to fast for arduino to sense the knife but as you say ,it will be ok.
I will take it from start and rewrite my code