Can I have some help with my code please?

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..

void setup() {
pinMode(7, INPUT);
pinMode(8, INPUT);
}

void loop() {
digitalRead(7); // blind up button 1
analogWrite(5, 255); // blind up output
digitalRead(8); // blind down button 1
analogWrite(10, 255); // blind down output
}

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

digitalRead(7);  // blind up button 1

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().

How are you wiring your buttons? Do you know about pinMode (pin, INPUT_PULLUP) ?

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().

Which is hardly pulsing them, right?

chrisjones95:

void loop() {

if (digitalRead(7) > 0){
    analogWrite(5, 255);
  }
  if (digitalRead(8) > 0){
    analogWrite(10, 255);
  }
}

Code tags please, like I used above.

Read this before posting a programming question

How to use this forum

What is the point of analogWrite (pin, 255)? That just turns it fully on. You may as well do digitalWrite (pin, HIGH).

 if (digitalRead(7) > 0)

digitalRead returns LOW or HIGH. You shouldn't be testing for "greater than zero".

Similar code used with servos.

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

Thanks again guys, how's this?

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 :slight_smile:

    analogWrite(5, HIGH);  //output to motor

If you just want the motor on, do:

    digitalWrite(5, HIGH);  //output to motor

However that shouldn't change the behaviour much.

  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?

Maybe:

digitalWrite (5, digitalRead(7));
digitalWrite (10, digitalRead (8));

However that would not stop both motors trying to turn at once if you hit both buttons.

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 :slight_smile:

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.