Interrupt - Accelstepper problem

hi to all,

I have a code which is working in loop section. I want to add a interrupt to it. I have a LDR sensor which have digital output connected to digital pin2. when the output is high the code in loop will stop and motor will turn untill the ldr sensor’s output low.

I tried to add the this simple code but when the ldr output change motor takes just one step and stop. it doesnt continue to turn untill the output low.

I will be happy if someone help me about the code.

thanks.

#include <AccelStepper.h>

AccelStepper stepper1(1, 6, 5);

void setup(){
stepper1.setMaxSpeed(500); //
stepper1.setSpeed(100);//
attachInterrupt(0, Motor, CHANGE);

}

void loop()
{

}

void Motor(){
stepper1.run();
}

I tried to add the this simple code but when the ldr output change motor takes just one step and stop. it doesnt continue to turn untill the output low.

Well, that is what you told it to do. When the LDR changes state, the interrupt handler is called once per change.

In the interrupt handler, you call the poorly named run() method, which does NOT cause the stepper to step continuously. It causes the stepper to step ONCE.

I really can not imagine why you need an interrupt to notice when a ssslllooowww light dependent resistor changes from detect a light level that is above the threshold or one that is below the threshold.

Hi Paul,

Thanks for your reply,

how can I modify interrupt to run stepper continuously ?

how can I modify interrupt to run stepper continuously ?

Simple. Quit using interrupts.

PaulS:
Simple. Quit using interrupts.

I have 2 step motor in my project. while one stepper working I need to move another one to control lift machanism when the LDR value high that is why I am trying to do it with interrupts.

I used "if" statement in the code but it makes problem and the second motor which controls the lift doesnt work well.

that is why I am trying to do it with interrupts.

You do NOT need to use interrupts. If you are 30 milliseconds late detecting that the LDR changed state, it that REALLY going to matter?

Poll the LDR in loop(). If you find that the LDR is not polled often enough, stop writing blocking code.

PaulS:
You do NOT need to use interrupts. If you are 30 milliseconds late detecting that the LDR changed state, it that REALLY going to matter?

30 milliseconds is not matter but I need to block the code which is working in loop section untill the LDR state change. Can I do that without interrupts ?

Thank you.

I wrote this code to test. Motor1 will continue to turn untill LDR Value HIGH. When LDR Value HIGH Motor2 will run and Motor1 will stop. When LDR Value Low Motor2 stop, Motor1 continue to turn

is it right ?

Const int stepPin2 = 8;
Const int dirPin2 = 9;
Const int stepPin1 = 4;
Const int dirPin1 = 5;
int LDR = 3; // LDR connected to pin 3
int delayTime = 500;

void setup() {
pinMode(stepPin1,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(stepPin2,OUTPUT);
pinMode(dirPin2,OUTPUT);
attachInterrupt(1,ldr_aktif,RISING);
}

void loop() {

digitalWrite(dirPin2,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin2,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin2,LOW);
delayMicroseconds(500);
}

void ldr_aktif() {

if(digitalRead(LDR)==1);
{
digitalWrite(dirPin1,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin1,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin1,LOW);
delayMicroseconds(500);

delay(delayTime);
}
}

Thank you.

It is completely impractical to have code like that in an Interrupt Service Routine. if an ISR takes 100 microsecs to complete I would consider it much too slow.

Your ISR should simply record the fact that the interrupt has happened and leave your other code to act on that. For example

void ldr_aktif() {
  ldrHasTriggered = true;
}

...R