Using a button to stop a motor

Hello, this code is meant to make a motor spin, and when a button is pressed, should make it stop, until lifted. I have no idea why the attached code doesn't work :confused: , perhaps it is just a wiring issue, but I do not think so. Thanks for your help. :slight_smile:

void setup()
{
#define SKILL_STOP A0 //For stoping the tray
#define MOTOR_SPEED 3 //PWM output for MOTOR
#define MOTOR_DIRECTION_A 4 //Motor Direction Pin A
#define MOTOR_DIRECTION_B 5 //Motor Direction Pin B
}

void loop()
{

if (digitalRead(SKILL_STOP == LOW)) //checks button
{
   while (digitalRead(SKILL_STOP == LOW)); //waits for button release
   {
   digitalWrite (MOTOR_DIRECTION_A, HIGH);//confirms motor direction
   digitalWrite (MOTOR_DIRECTION_B, LOW); //confirms motor direction
   analogWrite (MOTOR_SPEED, 0); //set the speed when skill stop is pressed
   delay(100); //pauses a .1 of a second
   }
}
else
 {
  digitalWrite (MOTOR_DIRECTION_A, HIGH); //confirms motor direction
  digitalWrite (MOTOR_DIRECTION_B, LOW); //confirms motor direction
  analogWrite (MOTOR_SPEED, 255); //sets the speed when skill stop is not pressed (1 - lowest, 255 - highest)
 }

}

What happens when you press SKILL_STOP ? And when you release it?

I don't see any reference to how the switches are wired in your sketch. It is very important that your sketch is aware of the way you actually did wire them... PULLUP, or PULLDOWN? It is usually in the setup() section of your sketch that you do that.

#define xxx is done before the setup() section.

EDIT: if you have your DC motor just connected between pin 3 and ground, stop right now. The motor is capable of destroying you Arduino. Easily.

This

if (digitalRead(SKILL_STOP == LOW))

is wrong, and similar constructs appear also.

This should be

if (digitalRead(SKILL_STOP) == LOW)

It makes a difference. Have you seen anything like the original code in the examples?
Of course, we know nothing about the wiring so perhaps this will not help. The setup() function does not turn on pullup resistors so there had better be resistors in the wiring.
Only memstar knows.

to wire a button to the arduino you need to connect the first pin to 5V and the other to any pin you like except the Analog IN pins they're are used to read analog signal from sensors also you need to add a pulldown resistor
from the button to ground usually 10k ohm resistor and you need to debounce the button (read the current button state if it's deffrent from the last button state use a delay of 5 ms and read it again just google it if you don't understand)

jbellavance:
What happens when you press SKILL_STOP ? And when you release it?

I don't see any reference to how the switches are wired in your sketch. It is very important that your sketch is aware of the way you actually did wire them... PULLUP, or PULLDOWN? It is usually in the setup() section of your sketch that you do that.

#define xxx is done before the setup() section.

EDIT: if you have your DC motor just connected between pin 3 and ground, stop right now. The motor is capable of destroying you Arduino. Easily.

Sorry for not making myself clear enough. I don't just have the motor connected to pin 3 and ground. It is wired through an L298N board. Second, I have #define in void setup() because otherwise, it gave me errors when compiling (note: this was something I saw someone else do to fix their issue, but it could only be a temporary/incorrect fix for mine, I am not sure). I am using a microswitch that is connected on to ground on the Com tab and on the normally open tab is connected back to pin A0. Pins 4 and 5 are connected to In3 and In4 on the L298N board, to set the motor directions. 24V is also going into that board to power the motor. Last thing, the goal of SKILL_STOP is to make the motor stop spinning once the button is pressed and to make it start working again once released. Sorry for my bad original post, hopefully, this helps, if it doesn't I could perhaps send an image of the whole thing.

Your micro switch needs a pullup resistor, or better still a pullup resistor should be enabled in the setup() function.

Otherwise when the micro switch is open, the input may float low and high randomly.

The #define statements are usually placed ahead of the setup() function. If you had them after the loop() function (you didn't say) then there would have been compiler errors.

vaj4088:
The #define statements are usually placed ahead of the setup() function. If you had them after the loop() function (you didn't say) then there would have been compiler errors.

I think that the compiler errors were from having a blank setup(), also, how would I go about installing that pullup resistor in the setup()? Thanks for your help.

The setup() function can be blank as long as the curly brackets {} are still present.

The line

  pinMode(SKILL_STOP, INPUT_PULLUP) ;

should be placed in the setup() function immediately after the last #define statement but before the closing curly bracket } .

jbellavance:
It is very important that your sketch is aware of the way you actually did wire them... PULLUP, or PULLDOWN? It is usually in the setup() section of your sketch that you do that.

How do you make the sketch aware that there's a pulldown resistor?

kenwood120s:
How do you make the sketch aware that there's a pulldown resistor?

I think that this answers your question. (It is from the post one above you.)

vaj4088:
The setup() function can be blank as long as the curly brackets {} are still present.

The line

  pinMode(SKILL_STOP, INPUT_PULLUP) ;

should be placed in the setup() function immediately after the last #define statement but before the closing curly bracket } .

memstar:
I think that this answers your question. (It is from the post one above you.)

No it doesn't: pinMode(x, INPUT_PULLUP) tells it to use the bulilt-in pullUP. jbellavance indicates there's a way to tell it there's a pullDOWN: I'm keen to see how he does that.

Hey Vaj4088, I returned back to my project this morning and I added the one line of code yet it still doesn't seem to work, I don't get the motor to spin at all. Do you have any more ideas why this would be?

No it doesn't: pinMode(x, INPUT_PULLUP) tells it to use the bulilt-in pullUP. jbellavance indicates there's a way to tell it there's a pullDOWN: I'm keen to see how he does that.

pinMode(x, INPUT);

jbellavance:
pinMode(x, INPUT);

Nonsense: I thought that's what you might say.

Just because you don't invoke a pullUP, that doesn't mean there's a pullDOWN in play.

If there was such a thing as a builtin pullDOWN resistor- there isn't- it would surely need explicit invocation with a pinMode(x, INPUT_PULLDOWN).

Ok, it is nonsense.

Please show us the current code. Also show us the wiring, including power supplies, grounds, etc.

The difference between a pullup resistor (internal or external) and an external pulldown resistor is how the code is written. The function of the switch is inverted between pullup and pulldown; the code must be written to take this into account.

Here is my code so far:

void setup()
{
#define SKILL_STOP A0 //For stoping the tray
#define MOTOR_SPEED 3 //PWM output for MOTOR
#define MOTOR_DIRECTION_A 4 //Motor Direction Pin A
#define MOTOR_DIRECTION_B 5 //Motor Direction Pin B
pinMode(SKILL_STOP, INPUT_PULLUP) ;
}

void loop()
{

if (digitalRead(SKILL_STOP == LOW)) //checks button
{
   while (digitalRead(SKILL_STOP == LOW)); //waits for button release
   {
   digitalWrite (MOTOR_DIRECTION_A, HIGH);//confirms motor direction
   digitalWrite (MOTOR_DIRECTION_B, LOW); //confirms motor direction
   analogWrite (MOTOR_SPEED, 0); //set the speed when skill stop is pressed
   delay(100); //pauses a .1 of a second
   }
}
else
 {
  digitalWrite (MOTOR_DIRECTION_A, HIGH); //confirms motor direction
  digitalWrite (MOTOR_DIRECTION_B, LOW); //confirms motor direction
  analogWrite (MOTOR_SPEED, 255); //sets the speed when skill stop is not pressed (1 - lowest, 255 - highest)
 }

}

Here is a quick Fritzing sketch (attached), I hope that it helps. Thanks for your continued help.

I am currently limited to mobile devices that cannot view images, but Fritzing files are usually unsatisfactory. However...

I noticed that these lines are still present:

If (digitalRead(SKILL_STOP == LOW)) //checks button
{
   while (digitalRead(SKILL_STOP == LOW)); //waits for button release

I pointed out earlier that these and any like them are bad. I already provided the correction. It is no wonder that your project does not work.

Sorry vaj, I must have missed your post. I'll be sure to fix those lines of code.