PLEASE HELP, problem stepper & driver Toshiba HY-DIV268N-5a with d50nk proximity

Excuse me, best regards to the Arduino masters. I'm having trouble with a stepper motor and a Toshiba HY-DIV268N-5a motor driver with a proximity sensor d50nk, using an Arduino Mega. I want to rotate a few steps of the stepper motor under the supervision of the d50nk sensor (the stepper motor stops when the sensor runs or blocks an obstacle). Please help me the Arduino masters :slight_smile:

This is my code :

#define stepPin 9
#define dirPin 8
#define sensor1 2
#define stepsPerRevolution 1600

void setup()
{
Serial.begin(9600);
Serial.print("Go");

pinMode(stepPin , OUTPUT);
pinMode(dirPin , OUTPUT);

pinMode(sensor1, INPUT);

digitalWrite(dirPin , HIGH);
}



void loop()
{

  // Set the spinning direction clockwise:
  digitalWrite(dirPin, HIGH);
  // Spin the stepper motor 5 revolutions fast:
  for (int i = 0; i < 5 * stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }

  delay(1000);
{  
if (digitalRead(sensor1) == HIGH) 
{
digitalWrite(stepPin , LOW);
delayMicroseconds(1000);
digitalWrite(stepPin , HIGH);
delayMicroseconds(1000);
}
}
}

The code only spins five times but the d50nk sensor doesn't stop it. I want to spins five times the stepper but if stop example stop on 2 times, 3 times spin after still available. Please help me,thx.

If you wrote the code you can do it yourself.

It looks as if your code is designed to move the motor 5 steps, check the sensor and if it is HIGH move one more step. The repeat that over and over.

Is that what it actually does?

What, exactly do you want it to do?

By the way if you use the AutoFormat tool to indent your code consistently it will make your code much easier to read and much easier for people trying to help you.

...R

Robin2
+Is that what it actually does?
-No, i want to spins five times the stepper but if sensor on / HIGH, it paused no stopped i meaned. Example paused on 2 times/rotate, 3 times spin after still available. I tried a lot of experiments but the code was wrong.

The stepper motor is Nema 23. I searched the arduino forum but nothing is the same as me the case. Please help me the newbie this :confused:

rendezvous:
Robin2
+Is that what it actually does?
-No, i want to spins five times the stepper but if sensor on / HIGH, it paused no stopped i meaned. Example paused on 2 times/rotate, 3 times spin after still available. I tried a lot of experiments but the code was wrong.

This is very confusing. Please re-read it and see if you think someone who knows nothing about your project can understand it.

Also, you have quoted my question but you seem to say my description is wrong without telling us what it actually does.

Unlike the Irishman who was asked for directions to Cork city and said "well, I wouldn't start from here" we are obliged to start from "here" - meaning from a clear description of what actually happens.

...R

@Robin2

Before it, i was doing the code arduino this :

#define stepPin 9
#define dirPin 8
#define sensor1 2


void setup()
{
  Serial.begin(9600);
  Serial.print("Counter Start");

  pinMode(stepPin , OUTPUT);
  pinMode(dirPin , OUTPUT);

  pinMode(sensor1, INPUT);

  digitalWrite(dirPin , HIGH);
}



void loop()
{
  if (digitalRead(sensor1) == HIGH)
  {
    digitalWrite(stepPin , LOW);
    delayMicroseconds(700);
    digitalWrite(stepPin , HIGH);
    delayMicroseconds(700);
  }
}

This was a simple code. it means the stepper work and if sensor proximity on / HIGH, the stepper was going off. If sensor proximity off / LOW, the stepper was going on again. No for how many steps do you wanted. It`s stepping/rotating continous.

I want the code which is the stepper spins five times but if sensor on / HIGH, it paused, no stopped i meaned. Example paused on 2 times/rotate, 3 times spin after still available. The first code was wrong. Please help and correct me how the true code that i wanted. The masters of arduino are surely having great soul for me the newbie this :). Thanks.

rendezvous:
This was a simple code. it means the stepper work and if sensor proximity on / HIGH, the stepper was going off. If sensor proximity off / LOW, the stepper was going on again. No for how many steps do you wanted. It`s stepping/rotating continous.

I'm guessing that English is not your first language. I don't know what that is and I have no doubt that your English is better than any attempt I might make at your language - but, unfortunately, I find your comments very difficult to understand. Maybe one the Forum's language sections would be more convenient for you?

This code in Reply #5

void loop()
{
  if (digitalRead(sensor1) == HIGH)
  {
    digitalWrite(stepPin , LOW);
    delayMicroseconds(700);
    digitalWrite(stepPin , HIGH);
    delayMicroseconds(700);
  }
}

will make the motor move when the sensor reads HIGH whereas your description seems to say it stops when the sensor is high.

I'm also don't understand what you want the program to do. I'm guessing that you want the motor to move 5 steps if the sensor is HIGH and to do nothing if the sensor is LOW. If so, this should do what you want

void loop() {
    if (digitalRead(sensor1) == HIGH) {
        for (byte n = 0; n < 5; n++) {
            digitalWrite(stepPin , LOW);
            delayMicroseconds(10);       // a short pulse is sufficient
            digitalWrite(stepPin , HIGH);
            delayMicroseconds(1400);   // only this needs to be changed to alter the speed
        }
    }
}

...R

@Robin2
Thats right. Im sorry im Indonesian. English in here, are street language used :slight_smile:
Thanks for reply but the code become like this :

#define stepPin 9
#define dirPin 8
#define sensor1 2


void setup()
{
  Serial.begin(9600);
  Serial.print("Counter Start");

  pinMode(stepPin , OUTPUT);
  pinMode(dirPin , OUTPUT);

  pinMode(sensor1, INPUT);

  digitalWrite(dirPin , HIGH);
}



void loop()

{
  if (digitalRead(sensor1) == HIGH)
  { for (byte n = 0; n < 5; n++) {
      digitalWrite(stepPin , LOW);
      delayMicroseconds(10);       // a short pulse is sufficient
      digitalWrite(stepPin , HIGH);
      delayMicroseconds(700);   // only this needs to be changed to alter the speed
    }
  }
}

But it not works, sorry for twice. Motor is not moved 5 times. Next, i added on above #define stepsPerRevolution 1600 and for (byte n = 0; n < 5 * stepsPerRevolution; n++). Also not works again.

I explored like this. Not works too :

#define stepPin 9
#define dirPin 8
#define sensor1 2
#define stepsPerRevolution 1600

void setup()
{
  Serial.begin(9600);
  Serial.print("Counter Start");

  pinMode(stepPin , OUTPUT);
  pinMode(dirPin , OUTPUT);

  pinMode(sensor1, INPUT);

  digitalWrite(dirPin , HIGH);
}



void loop()
{

  // Set the spinning direction clockwise:
  digitalWrite(dirPin, HIGH);
  // Spin the stepper motor 5 revolutions fast:
  for (int n = 0; n < 5 * stepsPerRevolution; n++)
  {
    if (digitalRead(sensor1) == HIGH)
    { for (byte n = 0; n < 5 * stepsPerRevolution; n++) {
        digitalWrite(stepPin , LOW);
        delayMicroseconds(20);       // a short pulse is sufficient
        digitalWrite(stepPin , HIGH);
        delayMicroseconds(500);   // only this needs to be changed to alter the speed
      }
    }
  }

  if (digitalRead(sensor1) == LOW)
  { for (byte n = 0; n < 5 * stepsPerRevolution; n++) {
      digitalWrite(stepPin , HIGH);
      delayMicroseconds(20);       // a short pulse is sufficient
      digitalWrite(stepPin , LOW);
      delayMicroseconds(500);   // only this needs to be changed to alter the speed
    }
  }
}

On your statement this :
I'm also don't understand what you want the program to do. I'm guessing that you want the motor to move 5 steps if the sensor is HIGH and to do nothing if the sensor is LOW. If so, this should do what you want

Me : The opposite your statement. I want the motor stepper works five steps if the sensor is LOW. And to do nothing / pause / temporary stop if the sensor is HIGH. Please explain it to me, thanks.

rendezvous:
@Robin2
Thats right. Im sorry im Indonesian. English in here, are street language used :slight_smile:
Thanks for reply but the code become like this :

But it not works, sorry for twice.

The words "not works" do not give us any information from which to help you. What actually happens when you try the program?

Me : The opposite your statement. I want the motor stepper works five steps if the sensor is LOW. And to do nothing / pause / temporary stop if the sensor is HIGH. Please explain it to me, thanks.

Then just change HIGH to LOW in the IF test

I notice in your second program that you have change the pulse width from 10 to 20 - why?

Also I notice that you have increased the motor speed by reducing 700 to 500. In general when you have problems with a stepper motor start by getting it to work at a very slow speed - try 7000, for example, or even 70000L (use the L to make sure the compiler treats it as a long integer - the max for a regular integer is 32767

...R

[color=#222222]@robin someone suggested me in the facebook group to use ʻinterrupt` on arduino. Please guide me so I can get lots of references.

The point is to
rotating several steps of the stepper motor under the supervision of the d50nk sensor (the stepper motor pauses while the sensor is working or blocks an obstacle, not absolute stopped). For example, the stepper moves five steps, but in the third step there is an object obstruction, the d50nk `HIGH` sensor then stops immediately (paused). After the obstacle is absent or the d50nk `LOW` sensor, the stepper motor continues two more steps as I wish.[/color]

Text from Reply #9 so we can read it

@robin someone suggested me in the facebook group to use ʻinterrupt` on arduino. Please guide me so I can get lots of references.

The point is to
rotating several steps of the stepper motor under the supervision of the d50nk sensor (the stepper motor pauses while the sensor is working or blocks an obstacle, not absolute stopped). For example, the stepper moves five steps, but in the third step there is an object obstruction, the d50nk HIGH sensor then stops immediately (paused). After the obstacle is absent or the d50nk LOW sensor, the stepper motor continues two more steps as I wish.

You don't need an interrupt for this. That was a silly suggestion.

That is a clearer explanation.

It's a long time since your last Post so please post the latest version of your program and tell us exactly what it does when you run it and what you want it to do that is different.

Do you really mean 5 steps of a stepper motor - that is a very short distance?

What should happen when the 5 steps have been completed?

By the way it is much easier to help if you respond quickly (the same day or the next day). After a month I have completely forgotten about your project.

...R

Do you want five individual steps or five full rotations of the stepper. Since I see this in your code:

for (byte n = 0; n < 5 * stepsPerRevolution; n++)

I assume it's five full rotations.

Note that you can't use a byte there: 5*1600 won't fit. Make it an int like you did earlier in the code.

Note also that loop is called repeatedly - you have nothing to stop the system after five rotations.