While loop problem

Hi everyone,

I am currently working with a stepper motor connected with a driver (M542) and an Arduino controller.
The controller receives a digital entry coming from a robot.
The motor has to turn when this input is HIGH and stops when this input becomes LOW. The problem is that the motor always turns, whether the entry is low or high.

Here is my code with a while loop. (I have also tried with a “if” condition).

int PUL = 8 ; //Pulse : HIGH 4-5V - LOW 0-0.5V
int DIR = 9 ; //Direction
int ENA = 10 ; //Enable/disable driver
int ROBOT = 11 ; //input


void setup() {
  // put your setup code here, to run once:
  
  pinMode(PUL,OUTPUT);
  pinMode(DIR,OUTPUT);
  pinMode(ENA,OUTPUT);
  pinMode(ROBOT,INPUT);


  digitalWrite(DIR,HIGH);
  //digitalWrite(ENA,HIGH);
  delayMicroseconds(10);

  Serial.begin(9600);

}


void loop() {
  // put your main code here, to run repeatedly:

 Serial.println(digitalRead(ROBOT));

 while (digitalRead(ROBOT)==HIGH) {
  
  digitalWrite(PUL,HIGH);
  delayMicroseconds(500);
  digitalWrite(PUL,LOW);
  delayMicroseconds(500);
 }



}

Programme_essai.ino (597 Bytes)

How is the input wired ? Do you have a pull down resistor in the circuit or is the input floating at an unknown voltage ?

Next time, please insert short code (forum allows a post to be up to 9000 characters) in the post instead of attaching it. Use code tags ([code] and [/code]) so it looks like below

OP's code

int PUL = 8 ; //Pulse : HIGH 4-5V - LOW 0-0.5V
int DIR = 9 ; //Direction
int ENA = 10 ; //Enable/disable driver
int ROBOT = 11 ; //input


void setup() {
  // put your setup code here, to run once:
  
  pinMode(PUL,OUTPUT);
  pinMode(DIR,OUTPUT);
  pinMode(ENA,OUTPUT);
  pinMode(ROBOT,INPUT);


  digitalWrite(DIR,HIGH);
  digitalWrite(ENA,HIGH);
  delayMicroseconds(10);



}


void loop() {
  // put your main code here, to run repeatedly:


 while (digitalRead(ROBOT)==HIGH) {
  digitalWrite(PUL,HIGH);
  delayMicroseconds(500);
  digitalWrite(PUL,LOW);
  delayMicroseconds(500);

 }
}

Good point, you will definitely need a pull down resister.

First put a 10kΩ resister from the Input Pin to ground.
As far as the code goes a "while loop" isn't going to work.
Secondly with a delay of 500µs the motor will go on and off so quickly while holding the button down that it may seem like it's always on.

This is crude but replace the entire "while loop" with this.

if(digitalRead(ROBOT) == HIGH)
  {
    digitalWrite(PUL, HIGH);
  }

  else
    {
       digitalWrite(PUL, LOW);
    }

The motor should spin when the button is held down and stop when it is released.

I see that you will also want to control the direction of the motor at some point.
The best way to do this is to use an H Bridge Motor Controller.

Hope this helps.

The input sends 24V, I use an adaptator to get only 5V so as to avoid to spoil the controller.
I am using a Stepper Motor Driver from Leadshine M542.

I tried your code, the motor is always spinning. I checked the input from the robot, it works properly.
At the beginning it is at 0, then it raises to 1, then back to 0.

I have just wired PUL because ENA and DIR are not necessary for now.

Better than using an external pull down resistor would be to use the built in pull up resistor. This is activated using

pinMode(pinNumber, INPUT_PULLUP);

Rewire the switch to take the pin to GND when the switch is closed and change the logic of the program to test for LOW on the pin to indicate that the switch is closed.

Why go to all this bother you are probably asking

  1. Less wiring
  2. No worries about component values
  3. No change to code needed if you start by using INPUT_PULLUP as your standard way of defining the mode
  4. It works !

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Thank you for your advices.
UKHeliBob I tried to use INPUT_PULLUP in my programm, but the motor never stopped spinning even when the Robot Input was low.

int PUL = 8 ; //Pulse : HIGH 4-5V - LOW 0-0.5V
int DIR = 9 ; //Direction
int ENA = 10 ; //Enable/disable driver
int ROBOT = 11 ; //input

void setup() {
  // put your setup code here, to run once:
  
  pinMode(PUL,OUTPUT);
  pinMode(DIR,OUTPUT);
  pinMode(ENA,OUTPUT);
  pinMode(ROBOT,INPUT_PULLUP);


  digitalWrite(DIR,HIGH);
  //digitalWrite(ENA,HIGH);
  delayMicroseconds(10);

  Serial.begin(9600);

}


void loop() {
  // put your main code here, to run repeatedly:

 Serial.println(digitalRead(ROBOT));

 while (digitalRead(ROBOT)==HIGH) {
  
  digitalWrite(PUL,HIGH);
  delayMicroseconds(500);
  digitalWrite(PUL,LOW);
  delayMicroseconds(500);
 }

}

I didn’t connect Pin 9 and 10 for now.

When I juste write :
digitalWrite(PUL,HIGH) ;
Nothing happens and same when I code :
digitalWrite(PUL,LOW);

Here is my circuit.

Please confirm how you have got the ROBOT pin wired.

I put a picture of my wiring. To sum up :

I plugged my Input and the Ground from the robot to A1 and A2 on the converter.
Then I wire 14 NO (black wire) to Arduino on Pin 11 and 11 COM (green wire) on the Ground Pin.

The two other wires you can see are for the PUL OUTPUT (pin 8).

The picture that you attached to reply #9 shows the Arduino connected to a DIN Rail Relay Socket. Did you upload the correct picture ?

Yes it is the correct picture, I may have use the wrong terms to describe my circuit.

Please can you provide a complete diagram of your circuit. Hand drawn and photographed is OK.

Hi,
Edit your code to add the line below.

pinMode(ROBOT,INPUT);
pinMode(ROBOT,INPUT_PULLUP);

So you declare the INPUT before you use pullup.

Tom... :slight_smile:

TomGeorge:
Hi,
Edit your code to add the line below.

pinMode(ROBOT,INPUT);

pinMode(ROBOT,INPUT_PULLUP);



So you declare the INPUT before you use pullup.

Tom... :)

Why 2 pinMode()s ?

UKHeliBob:
Why 2 pinMode()s ?

To be sure...
To be sure...
I know you don't have to but I prefer to see them like that..... :o :o :o

There are several people trying to help you here so if you would kindly post some of what has been requested like a proper Schematic maybe we can help.

Please get rid of that damn While Loop it's a waste of time.
The MicroSecond Delays are useless as well. They're far to short a period of time.
According to your first Schematic the Opto Isolator will only turn on when PUL is LOW.
If you are going to use a PULL_UP Resistor then your digitalReads will need to be inverted.
The input will HIGH when the button is released and LOW when pressed.

Lys_:
I put a picture of my wiring. To sum up :

I plugged my Input and the Ground from the robot to A1 and A2 on the converter.
Then I wire 14 NO (black wire) to Arduino on Pin 11 and 11 COM (green wire) on the Ground Pin.

The two other wires you can see are for the PUL OUTPUT (pin 8).

Hard to see where your wires are connected on that picture. Make sure your green wire didn’t ended up in AREF by mistake.

I made a hand-drawn circuit, hope this would be clearer.

About my code, I followed the instructions of the driver documentation (M542 Leadshine) that mentions the Microseconds delay. And when I delete these delays in my code, the motor isn’t spinning anymore.
digitalWrite(PUL,LOW); used alone doesn’t make the motor spin.

If the while loop is bothering you, what do you suggest ? Because I need a condition on the Robot Input to launch the motor. I tried “if” but it didn’t work. Can a do while loop work in this case ?

handdrawn_circuit.jpg

I finally solved my problem.
It appears that the motor works properly when I add a delay in my code :

int PUL = 8 ; //Pulse : HIGH 4-5V - LOW 0-0.5V
int ROBOT = 11 ; //input


void setup() {
  // put your setup code here, to run once:
  
  pinMode(PUL,OUTPUT);
  pinMode(ROBOT,INPUT);

}


void loop() {
  // put your main code here, to run repeatedly:


  delay(550);
  while (digitalRead(ROBOT)== HIGH) {
  
    digitalWrite(PUL,HIGH);
    delayMicroseconds(500);
    digitalWrite(PUL,LOW);
    delayMicroseconds(500);
 }
 
}