Interrupts only displays but not reacting to any other command > <

Hi, I’ve got a stepper motor running in a slide slot, with two switches on both end so when the motor nearly runs out of the slot, it will move in the opposite direction.

I have tried all four conditions with Mega, when I ask it to display something when the interrupts are active, it works. However they do not respond to other commands like stop and reversing.

Here is a section of my code, please if anyone knows what I did wrong, just kindly let me know. Huge thanks.

#include <AccelStepper.h>
void setup()
{
  Serial.begin(9600);
  stepper.setMaxSpeed(2000);
  stepper.setAcceleration(800);
  myPID.SetMode(AUTOMATIC);
  myPID.SetSampleTime(controllerRate);
  
  attachInterrupt(2,limitb,LOW);
  attachInterrupt(3,limitt,FALLING);
  interrupts();
  
 }

void loop()
{}
void limitb()
{
    noInterrupts();
    stepper.setSpeed(500);
    stepper.runSpeed();
    interrupts();
    Serial.println("INTERUPT 2");
}

void limitt()
{
  noInterrupts();  
  stepper.setSpeed(-500);
  stepper.runSpeed();
  interrupts();
     Serial.println("INTERUPT 3");
}

please if anyone knows what I did wrong

Never call Serial.print in an interrupt.

Post all your code.

Thanks for your reply. I have deleted the line of serialprint, but still no luck.

#include <AccelStepper.h>
AccelStepper stepper(1, 2, 3);

int digitalIn2=21;
int digitalIn3=20;

void setup()
{
  Serial.begin(9600);
  stepper.setMaxSpeed(2000);
  stepper.setAcceleration(800);

  
  attachInterrupt(2,limitb,LOW);
  attachInterrupt(3,limitt,RISING);
  interrupts();
    Serial.println("Begin"); 
}


void loop()
{
   stepper.setSpeed(50);
   stepper.runSpeed();
}

void limitb()
{
    noInterrupts();
    stepper.setSpeed(500);
    stepper.runSpeed();
    interrupts();

}

void limitt()
{
   noInterrupts();  
   stepper.setSpeed(-500);
   stepper.runSpeed();
   interrupts();
}

OK, so "loop()" is being called as soon as it exits. Each time it is called (a few thousand times a second) it does this

 stepper.setSpeed(50);
   stepper.runSpeed();

Every now and then, you'll get an interrupt that does something like this

    stepper.setSpeed(500);
    stepper.runSpeed();

And a few microseconds later, "loop ()" gets called again.

Can you see the problem?

Thank you very much for your advice, I have added if statements so it is not looping constantly. However I’ve encountered another problem. The speed of the motor changes to the conditions set by the interrupts according to the serial monitor, but it only appears as a pulse when the motor is up and running. Initially I thought it might be due to the high speed and short time that the interrupts are running, and I have lowered the speed and increase the delays, but the performance remains the same.

My code now looks like this:

#include <AccelStepper.h>

AccelStepper stepper(1,2,3);
double motorSpeed=0;
volatile int t;
volatile int b;

const int sampleRate = 0.1;
const int controllerRate = 0.005;
const long serialPing = 1000;
unsigned long now = 0;
unsigned long lastMessage = 0;

void setup()
{
  Serial.begin(9600);
  stepper.setMaxSpeed(2000);
  stepper.setAcceleration(800);
  
  attachInterrupt(2,limitb,FALLING);
  attachInterrupt(3,limitt,RISING);
  interrupts(); 
  t=0;
  b=0;
  
  Serial.println("Begin"); 
  lastMessage = millis(); 
}


void loop()
{
  if(b<1&&t<1){
  motorSpeed=50;
    stepper.setSpeed(motorSpeed);
  stepper.runSpeed();
}
if(b==1){
motorSpeed=-50;
  stepper.setSpeed(motorSpeed);
  stepper.runSpeed();
delay(2000);
b=0;
}
if(t==1){
motorSpeed=300;
  stepper.setSpeed(motorSpeed);
  stepper.runSpeed();
t=0;
delay(1000);
}


now = millis();
  if(now - lastMessage > serialPing) {
    Serial.print("Speed = ");
    Serial.println(motorSpeed);
    Serial.print("\n");
    Serial.print("bottom switch = ");
    Serial.println(b);
    Serial.print("\n");
    Serial.print("top switch = ");
    Serial.println(t);
    Serial.print("\n");
      lastMessage = now;
  }
}


void limitb()
{
b=1;
}

void limitt()
{
   t=1;
}
const int sampleRate = 0.1;
const int controllerRate = 0.005;

No, not really.

Sorry those were for my controller and I've just deleted them. But it still does not do what it suppose to do.