Why wont it stop! Fixed!

I have some very simple code that works well to run a very simple TB6560 motor controller. The issue is that it will not stop the motor when the pot is at zero, runs at a low speed. How do I stop the motor when the pot is at zero?

Thanks.

void setup() {



pinMode(13, OUTPUT);

}

void loop() {

digitalWrite(13, LOW);

delayMicroseconds(analogRead(A0));

digitalWrite(13, HIGH);

delayMicroseconds(analogRead(A0));


}

Lets see the code you have been using! and post it in code tags!

Mark

Because your code as shown doesn't ever stop giving out pulses. Even with the control at maximum, it just sends out slow pulses.

You should read the value and then decide if you want to issue pulses or not.

MorganS:
Because your code as shown doesn't ever stop giving out pulses. Even with the control at maximum, it just sends out slow pulses.

You should read the value and then decide if you want to issue pulses or not.

Sorry, maybe this is the wrong place to have put this, I haven't written c code in 20 years and this is my first arduino project. If the output is looking at the pot input how does it " never stop".

loop() never stops. It repeats forever. It takes a reading from the analog input and then uses that to control how fast to send out output pulses. That's what the lines with delayMicroseconds() in them are doing. It repeats thousands of times per second.

That's what the lines with delayMicroseconds() in them are doing.

But, we have no idea what that function does.

OP: Post ALL of your code.

Use Serial.print() to assure yourself that you are getting a 0 reading from the pin.

PaulS:
But, we have no idea what that function does.

OP: Post ALL of your code.

Use Serial.print() to assure yourself that you are getting a 0 reading from the pin.

That is all the code. All I'm trying to do is send a pulse to the stepper controller and have the pot control the speed from zero to full speed. It works from very slow to full speed right now. I know I'm missing something, I just don't know what that is.

I know I'm missing something, I just don't know what that is.

You call a function, delayMicroseconds(), that is NOT defined in the code you posted. How can you expect that to compile?

If, by some miracle it does, how can you be certain that you are sending a 0 to the function?

Hey Paul. delayMicroseconds is an Arduino function. The user doesn't need to define it.

Right now your pot controls the time between pulses. It can't go to zero because those lines of code themselves take some time. I think you need to rethink how you're going to write this code if you want the motor to stop. Maybe test the value you read from the pot in an if statement and only do the pulse thing if it is greater than some threshold.

Hi I just posted some code up under the heading of Rusty Programmer needs help or soemething like that. Its my first attempt at running a motor although not a stepper motor. May be worth having a look I had all sorts of issues.

That makes sense. I see where I went wrong. I guess I'll start over and add an if statement. Thanks delta.

PaulS:
You call a function, delayMicroseconds(), that is NOT defined in the code you posted. How can you expect that to compile?

If, by some miracle it does, how can you be certain that you are sending a 0 to the function?

Um...dude...

Where's the facepalm smiley when you need it?

oyader:
Sorry, maybe this is the wrong place to have put this, I haven't written c code in 20 years and this is my first arduino project. If the output is looking at the pot input how does it " never stop".

I'll flip that question back to you. Look at your code, and tell me in detail how you think the pot will stop it. Just "looking at the pot input" isn't enough, you also need to do the correct thing with the value. Because I can assure you (along with MorganS) that it will never stop the motor. If you think it will stop it, you are either not thinking it through enough or are horribly misunderstanding something. In either case, something needs to be cleared up.

What pin is #13 connected to on the motor controller?

EDIT: Hooray for taking too long to write my post!

Delta_G:
Hey Paul. delayMicroseconds is an Arduino function. The user doesn't need to define it.

I looked at that code and read writeMicroseconds(), in spite of what I wrote. Duh.

Ok after a lot of reading I came up with this and it works exactly how I wanted it to.
Thanks for the input.

int pulse = 13;
int pot = A0;
int nopulse = 998;

void setup() {
 
  pinMode(pulse, OUTPUT);
 
 
  
}
void loop() {
  int analogValue = analogRead (pot);
  if (analogValue > nopulse) {digitalWrite(pulse, LOW);}
  else{
   
  digitalWrite(pulse, HIGH);   
  delayMicroseconds  (analogRead(pot));              
  digitalWrite(pulse, LOW);    
  delayMicroseconds(analogRead(pot));     
      }}

Neat, simple, does the job. Well done.

Thanks for letting us know that you got it worked out. That program might be useful to many more people in the future.

Why do you need to read the potentiometer three times?

Why can't you put some spaces before the {, or put them on a new line, CONSISTENTLY?

Why can't you put EVERY } on its own line?

Why can't you properly indent your code?

Good for you for getting it working. Now consider improving it.

Improve the program layout as suggested by Paul to make it easier to read and understand and consider using variable types that appropriate for the values that they will hold. For instance, will the value of pulse ever be greater than 255 ? If not it could be a byte instead of an int. Will the value of pulse change ? If not then it could be made into a const

const byte pulse = 13;

With such a small program the saving in memory will not matter but it is a good idea to get into good habits as early as possible.