Hi all,
I have the following problem: I want to move a stepper motor with the Arduino Portenta Machine Control using the Accelstepper Library. When the motor has reached a limit switch, it should stop directly. I was hoping to realize this with an interrupt. So as soon as a "High" signal is present at the limit switch or digital pin 05 (DIN_READ_CH_PIN_05), the function stopmotor() should be called. Unfortunately, this does not happen or does not work. Apparently the function is not even called although a high signal is present at the PIN.
What is also strange is that I have to declare "void stopmotor()" before the setup(), otherwise I cannot compile. That could be the case, but I still find it strange. In any case, I can't get any further. Do any of you have a tip? Or is there another way to stop the stepper motor directly during rotation using a limit switch? I think when stepper.runToPosition() is running only an interrupt can stop it.
#include <Arduino.h>
#include <Arduino_PortentaMachineControl.h>
#include <pinDefinitions.h>
#include <AccelStepper.h>
#include <Wire.h>
#define motorInterfaceType 1
volatile bool runallowed = true;
AccelStepper stepper = AccelStepper(motorInterfaceType, PinNameToIndex(MC_DO_DO0_PIN), PinNameToIndex(MC_DO_DO1_PIN));
void stopmotor()
{
runallowed = false;
stepper.setCurrentPosition(0);
Serial.println("STOP");
stepper.stop();
stepper.disableOutputs();
}
void setup()
{
Serial.begin(9600);
Wire.begin();
if (!MachineControl_DigitalInputs.begin())
{
Serial.println("Failed to initialize the digital input GPIO expander!");
}
MachineControl_DigitalOutputs.begin();
attachInterrupt(digitalPinToInterrupt(DIN_READ_CH_PIN_05), stopmotor, RISING);
stepper.enableOutputs();
stepper.setMinPulseWidth(130);
stepper.setAcceleration(5000);
stepper.setMaxSpeed(3200);
}
void loop()
{
if (runallowed == true)
{
stepper.moveTo(16000);
stepper.runToPosition();
Serial.println("Rotation reached to 180°");
delay(10000);
}
}