I love these forums, I have a couple of different things going on, so I have a couple of different subjects in different categories, but I just thought of something. So I am getting my motor to work and its going to be running off of...lets say PMW 11. So I wanted to get implement a push button in my circuit and be on my way. However, could I theoretically instead plug a push button to PMW 10, then just tell arduino "When push button is down, then PIN 11 = HIGH"? Because I could then evolve the code from there to do what I want...
could I theoretically instead plug a push button to PMW 10, then just tell arduino "When push button is down, then PIN 11 = HIGH"?
It depends how you have the input wired as to how you test whether the push button is "down" but, yes, you could do that. In fact, I don't see what you think is the alternative.
If you are not going to use the PWM ability of pin 11 you could use any pin to drive the motor but I hope that you are not driving it directly because it will almost certainly draw too much current.
Generally, you cannot drive a motor directly with an arduino. Not only will it draw too much current, but it's an inductive load and it will (probably) fry your chip. There's a separate forum for driving motors and such, and you should go there.
But as for what you want: sure!
boolean currentlyOn = false;
void loop() {
boolean on = (digitalRead(PUSH_BUTTON_PIN) == LOW);
if(!on && currentlyOn) {
analogWrite(11, 0); // turn off
currentlyOn = false;
}
else if(on && !currentlyOn) {
analogWrite(11, 255); // turn on
currentlyOn = true;
}
}
The only issue, as I mentioned, is the electronics of what you connect to pin 11.
Ahh, Thank you guys. I spend a lot more of my time in the motors section, since every user has their own expertise. This was just a simple question and didn't want to throw off any of the programmer specialists with a bunch of motor talk.
Attached is the current schematic that I have. As you can see, im not plugging the motor directly into the board, but I was unsure of how to implement a push button into the mix. My assumption was in between the resistor and the transistor. My logic was that it would be connected directly to the PWM pin so that I could could program the button to control the motor. However, as I was looking at it, I was thinking about just programming the PIN itself and that's why I came to this section. Thanks.
Put the diode across the motor not the transistor.
PaulMurrayCbr:
Generally, you cannot drive a motor directly with an arduino. Not only will it draw too much current, but it's an inductive load and it will (probably) fry your chip. There's a separate forum for driving motors and such, and you should go there.But as for what you want: sure!
boolean currentlyOn = false;
void loop() {
boolean on = (digitalRead(PUSH_BUTTON_PIN) == LOW);
if(!on && currentlyOn) {
analogWrite(11, 0); // turn off
currentlyOn = false;
}
else if(on && !currentlyOn) {
analogWrite(11, 255); // turn on
currentlyOn = true;
}
}
The only issue, as I mentioned, is the electronics of what you connect to pin 11.
I am having a similar issue, so thank you for taking the time to code this out. My only question with this code is the analogWrite. I am still an Arduino newbie, but I have always associated the analog function with potentiometers. the (11, 0) and (11,255) im assuming is just the (pin, speed), but according to Resistorftw's picture, it is connected to the digital, I believe. If you have any questions, please let me know. Thank you.
-Theonegoku
Grumpy_Mike:
Put the diode across the motor not the transistor.
HA! believe it or not the people over in the motor section said the same thing. the picture is from an example on instructables and he said that it was a precautionary measure but also said the same about the capacity on the motor but I didn't use a capacitor. are you saying to put the diode where the cap is labeled across the motor? I might be misreading that because then there would be a current flow directly from one lead to another, and I don't think that is a good thing. side note, if I was planning on making the motor turn both ways in the future, wouldn't a diode prevent that, because if it was across the motor it would only allow the current to travel one way?
the picture is from an example on instructables
Instructables are the biggest repository of crap electronics on the net. Avoid it at all costs.
believe it or not the people over in the motor section said the same thing.
That is because it is the right thing to do. It does not take a specialist to know this, you just need to be not an idiot.
I might be misreading that because then there would be a current flow directly from one lead to another,
You are miss reading this. The diode is reverse biased across the motor, so it only conducts when the motor acts as a generator when it is spinning after the power has been removed.
f I was planning on making the motor turn both ways in the future, wouldn't a diode prevent that,
Yes when making the motor move both ways you need four diodes.
See
http://www.thebox.myzen.co.uk/Workshop/Motors_1.html
and
http://www.thebox.myzen.co.uk/Workshop/Motors_2.html