Hey guys, I'm trying to write a code for my Arduino Uno that will let me control a motor when there is an input from a button. One button will make the motor turn one way, and the other button will turn the motor the other way and as you can tell from my notes in the code it is for controlling my window blind up and down. My problem is that I can't figure out what I need to add to make the output occur only when the button is pressed for the digital input. I have the input and output numbers set so that the wiring will be neater. This is only my second time coding the Arduino so be nice haha.
Here is what I have so far, I know it isn't much, but it's the basics, any help will be greatly appreciated..
AnalogWrite is for the digital pins with PWM (3, 5, 6, 9, 10, 11) output. AnalogWrite won't work with the analog input pins (A0-A5) even if configured as digital outputs as I understand it. Move your motor control wire to two PWM pins and set their pinModes to OUTPUT. Then use analogWrite().
To be of much more help we need to see how your project is wired. You will need an h-bridge of some sort if you want a motor to run forward and reverse.
There's a magical construct called "if" which only runs blocks of code when a certain condition is true. I suggest you research it: if - Arduino Reference
Read the input pin 7, and do nothing with the result.
(if the compiler hasn't already recognised that it effectively does nothing, and removed the whole construct for you)
groundfungus:
AnalogWrite is for the digital pins with PWM (3, 5, 6, 9, 10, 11) output. AnalogWrite won't work with the analog input pins (A0-A5) even if configured as digital outputs as I understand it.
Not strictly true. All the other pins do have the facility to use analogWrite() - however, it does 1-bit PWM. I.e., a value < 128 turns the pin off, a value >= 128 turns the pin on. Kind of a heavy-weight digitalWrite().
hey guys, thanks for the quick responses, sorry it took me a while to get back to you guys. I've had a look into the 'if' function and got my head round it, I think I've got the 'if' function part sorted now, apart from the input. I have put it as if it is greater than 0, because as digital it can only read as high or low, ie 1 or 0, am I right in thinking that or have I done it wrong?
here is how it looks now;
void setup() {
pinMode(7, INPUT); //blind up button 1
pinMode(8, INPUT); //blind down button 1
pinMode(5, OUTPUT); //blind up output
pinMode(10, OUTPUT); //blind down output
}
void loop() {
if (digitalRead(7) > 0){
analogWrite(5, 255);
}
if (digitalRead(8) > 0){
analogWrite(10, 255);
}
}
I will get a model of my circuit made up on fritzing and put it in this thread in a bit.
majenko:
Not strictly true. All the other pins do have the facility to use analogWrite() - however, it does 1-bit PWM. I.e., a value < 128 turns the pin off, a value >= 128 turns the pin on. Kind of a heavy-weight digitalWrite().
void setup() {
pinMode(7, INPUT); //blind up button 1
pinMode(8, INPUT); //blind down button 1
pinMode(5, OUTPUT); //blind up output
pinMode(10, OUTPUT); //blind down output
}
void loop() {
if (digitalRead(7) == HIGH){ //input from button
analogWrite(5, HIGH); //output to motor
}
if (digitalRead(8) == HIGH){ //input from button
analogWrite(10, HIGH); //output to motor
}
}
I have attached my layout on a fritzing diagram. If I have done anything wrong on that can you please let me know because I couldn't see anything wrong with it. Cheers
if (digitalRead(7) == HIGH){ //input from button
analogWrite(5, HIGH); //output to motor
}
if (digitalRead(8) == HIGH){ //input from button
analogWrite(10, HIGH); //output to motor
}
Your code never turns the motors off, surely that is not intentional?
That is a very good point, I hadn't considered that.. how would I make the motors stop when the button is depressed? just put in another line of code that would stop the output when there is no input?
Also, how would I put in the code that it would only allow one output at once, ie when one button is pressed it would block the other one from creating an output?
chrisjones95:
I have attached my layout on a fritzing diagram. If I have done anything wrong on that can you please let me know because I couldn't see anything wrong with it. Cheers
Is that really how you have your motor wired up?!? Not only can thar never work, but it will kill your arduino.
2 outputs connected to ground. same 2 outputs connected to motor. what is that supposed to achieve other than 2 dead io ports?
I suggest you ask Google how to connect a motor to an arduino before you do anything else.
Oh, and those buttons are never going to work in a month of Sundays. One connecting a pin to +5V when you press it, the other connecting a pin to +3.3V when you press it...?
Another thing for you to google: how to wire a button to an Arduino.
I suggest you go right back to basics and work through a couple of the introductory tutorials. You seem a bit out of your depth here - best to get some of the basic concepts nailed before you try going further.
Oh, and you might want to buy yourself a new Arduino. If you have really wired your board up like that and actively tried using it you may find it's dead now - or at least somewhat handicapped.