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 , perhaps it is just a wiring issue, but I do not think so. Thanks for your help.
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.
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.
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.
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?
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.
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.
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.