Hey, I'm a newbie at this, so bear with me... So I'm trying to create code to make it so the motor starts off, but turns on when the button is pressed. I've got the following code but it's not working. When I start the program the motor starts, so I know that's working. I've also tested the button with an LED so I know that's working as well, I just can't seem to get them together. Any help would be greatly appreciated!
const int buttonPin=13;
int buttonState=0;
const int motorPin=12;
void setup() {
// put your setup code here, to run once:
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW); //start at off
pinMode(buttonPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState=digitalRead(buttonPin);
//reads the state of the pushbutton value
if (buttonState == HIGH){
//start motor
digitalWrite(motorPin, HIGH);
delay(1000);
}
else{
}
}
Move the button off pin13. That's the pin the internal LED and it's resistor are connected to, and isn't any good as a button input.
Then, it's better to make it active-low rather than active-high, and enable the internal pullup resistor, rather than use an external pulldown resistor. (Connect your button between the pin and ground, rather than between the pin and 5V.)
pinMode(buttonPin,INPUT_PULLUP);
then:-
buttonState=digitalRead(buttonPin);
//reads the state of the pushbutton value
if (buttonState == LOW){ // *** LOW replaces HIGH
//start motor
digitalWrite(motorPin, HIGH);
delay(1000);
}
I assume you have plans for the 'else'?
I actually don't really have plans for 'else' at the moment.
So I connected the button to pin11 and ground instead but it's still not working...
Can you turn the motor on and off without a button?
One second on, one second off, etc.?
To do that I'd just put the following in a loop, right?
delay(1000);
digitalWrite(motorPin, HIGH);
delay(1000);
digitalWrite(motorPin, LOW)
It still didn't work for me -_-
Show us how you have the motor connected. (Schematic)
And double-check that it's driver is really connected properly to the 'motorPin' and ground, and that the Arduino ground and motor driver ground are connected.
Speaking of which, what motor driver are you using?
jw3times:
To do that I'd just put the following in a loop, right?
Yes, that should do it.
OldSteve:
Show us how you have the motor connected. (Schematic)
+1
How are you powering the motor?
Please don't say you're powering it directly from the Arduino's I/O pin.
DuaneDegn:
Please don't say you're powering it directly from the Arduino's I/O pin.
I was wondering this myself, that's why I was careful to include the words "motor driver". 
I'm not sure how to create a schematic on here, but I did exactly what's in the following link here. Only exception is I have wires connecting 5V to the + and GND to the -.
When I switch the motor to ground instead of 5V, it doesn't move at all.
Totally a newb question, but what do you mean by motor driver and I/O pin :o
jw3times:
I'm not sure how to create a schematic on here, but I did exactly what's in the following link here. Only exception is I have wires connecting 5V to the + and GND to the -.
Do you mean without a transistor?
Draw a schematic accurately by hand on paper and upload it. Words don't necessarily convey enough information. A photo would help, too.
Instead of "Quick Reply", click the "Reply" button, and you will see the option to add an attachment at the bottom of the edit window.
Edit: Even with a transistor, that Adafruit lesson is not a good one at all.
I'll draw one and upload it tomorrow if I still can't figure it out.. Thanks for all your help thus far though!
You didn't answer the transistor question. If you have things wired up without one, don't power it up again.
OldSteve:
Edit: Even with a transistor, that Adafruit lesson is not a good one at all.
I know it's not a good idea to power motors and servos from the Arduino's 5V line but I can sure understand why there are so many tutorials which use the Arduino's 5V line. It makes the whole setup so much easier than trying to find an alternate power source for the motor.
The Adafruit tutorial does point out the motor shouldn't draw much current.
jw3times:
Only exception is I have wires connecting 5V to the + and GND to the -.
When I switch the motor to ground instead of 5V, it doesn't move at all.
I don't understand the above description. Taking a photo of your setup is another alternative to showing how you connected things.
I suggest you don't continue to use the motor until we help determine you won't burn out your Arduino.
jw3times:
what do you mean by motor driver
A motor driver is a circuit to provide power to the motor. It can be a relay, transistor, MOSFET, h-bridge or some other circuit with allows the Arduino to control the motor without directly powering the motor.
I made a tutorial about how to use a h-bridge with a microcontroller. I used a different microcontroller but the principles also apply to the Arduino. (It seems like I've been linking to this tutorial several times everyday. I should update the tutorial to show an Arduino being used.)
jw3times:
and I/O pin
An I/O pin is an input/output pin. The numbered pins and the pins labeled with "A" for analog are I/O pins.
Move the button off pin13. That's the pin the internal LED and it's resistor are connected to, and isn't any good as a button input.
Incorrect - 13 connects to the input of an op-amp, which then drives the LED. 13 is free to be used like any other pin.
DuaneDegn:
I know it's not a good idea to power motors and servos from the Arduino's 5V line but I can sure understand why there are so many tutorials which use the Arduino's 5V line. It makes the whole setup so much easier than trying to find an alternate power source for the motor
Unfortunately, the main problem with this is that many beginners don't understand current at all, or the capabilities of an Arduino board. It is a little harder to do it properly, but it would avoid many silly mistakes. And save the lives of a few Arduino boards.
Anyone with a little more experience never powers motors or servos from the Arduino, because we know better.
So I still hold that those lessons are bad. 
Everyone wants to be a programmer and electronics designer, but hardly anyone wants to learn even the very basics of both disciplines first.
CrossRoads:
Incorrect - 13 connects to the input of an op-amp, which then drives the LED. 13 is free to be used like any other pin.
Fair enough, I stand corrected. I won't argue, because you know the subject better than I do, but I'm sure I've read that the LED was directly connected. Maybe on earlier boards? Or just on some Arduino models?
OK, I just found the reference that I read, indicating/recommending that pin13 shouldn't be used as an input.
It says:-
NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.
The page:-
Arduino Digital Pins
On earlier boards, yes, Not on Uno for several years now.
Can use as button input with 1K pullup to 'help' the internal pullup overcome the resistor/LED on boards that do not have the op amp.
CrossRoads:
On earlier boards, yes, Not on Uno for several years now.
Can use as button input with 1K pullup to 'help' the internal pullup overcome the resistor/LED on boards that do not have the op amp.
Looks like another case of an outdated reference. (And all this time I've been carefully avoiding using pin 13 for an input. We live and learn.
)
A question - only UNO, or other boards too?
I drew a picture, but honestly I think that this is clearer...
The red wire on the Board is connected to the 5V, and the yellow is connected to the ground.
Edit: The motor is starting, just not responding to the code.