Arm Mbed OS support forum

Limit Switch and While Loop

We want to rotate the stepper motor(Radial stepper) using a while loop until and unless an event(event: touching limit switch) triggers it.
Problems: 1) As per code, the stepper motor has to keep on running until and unless it touches the limit switch (event). But the stepper motor stopped rotating without even touching the limit switch and the code got executed fully. Sometimes the stepper motor works as per the code.,some times it’s doesn’t. Note: As per electronic connections, all are working fine.

Connection : Stepper motor driver Pul+, Pul-, Dir +, Dir - connected to Nucleo Board F746ZG (PE_4,PE_5,PE_2,PF_8) respectively. Stepper motor driver Vdc+ and ground are connected to the source. Stepper motor Pul+, Pul-, Dir +, Dir - are connected to Stepper motor driver A+,A-,B+,B-.
Limit Switch: Normally open, on the trigger, gets closed. Limit switch ground connected to the board ground and another terminal connected to the board (PD_2).

Checked all the possibilities (changing all of them limit switch, board, stepper motor, stepper motor driver, getting enough power supply). Code is metioned below.

Thanks in advance

. . .
/**/
#include “mbed.h”
#include “math.h”
#include “FermeStepper.h”
#include “stepperMotor.h”
#include “FermeCantileverMotor.h”
#include “PinDetect.h” // Library for external interrupts
#include “Servo.h”
#include “string”
#include “rtos.h”
#include “DebounceIn.h” // Library for sampling in interrupts (mechanical contacts)

Serial pc(USBTX, USBRX);

sMotor Radial_Stepper(PE_4,PE_5,PE_2,PF_8);
DigitalIn Radial_Limit(PD_2); // Radial Stepper limit switch
DigitalOut Radial_Relay(PE_3); // Radial Stepper Motor Relay

int main()
{
int ch,radialsteps,speed;
float radial_delay = 1;

Radial_Limit.mode(PullUp);
while(1)
{
    pc.printf("\n\1 radial reset \nChoice:");
    pc.scanf("%d", &ch);
    

    switch(ch)
    {
        case 1: 
            
                Radial_Relay = 1;
                wait(radial_delay);
                Radial_Limit.mode(PullUp);
                
                while(Radial_Limit!=1)                                      // Spin Stepper until hits limit switch
                    Radial_Stepper.step(1, 1, 350);
                wait(1);   
            
    break;
   }
}

}
. . .

Hello,

I probbaly not undesstand it.

You apply PullUp and the button is open (the contacts are open, switch is off) that mean PD_2 is High, and then you want to looping until it is not taken down to Low. But for that case it seems to be your condition of the while loop wrong.

Just a teory, maybe I am absolutly wrong.
You are runnig the loop without any small delay (I do not know whats more than generating a pulse, inside the step method is) so maybe it sometimes leads to collision because motor is not able to react to so much fast pulses. The random aspect can be caused by CPU ussage for another tasks (rest of code, OS in background and so on).
So maybe try to add a small delay to the loop.

BR, Jan

Hi,

I do not understand your electrical connections.
Firstly as I see you do not have any internal (if this possible) or external pull-up for limit switch input. In this case digital input value is undefined and can be 0 or 1 and can change at any time.
Secondly - how do you connecting stepper? Do you have stepper driver with DIR and PULSE inputs? In this case you need only 2 outputs from board or even 1 if you not want to change direction. If you have simplest motor with ULN2003 driver (as it look like by library) - you connected correctly, just named signals wrong. There is just 4 signals for motor phases.