Pages: [1]   Go Down
Author Topic: Arduino motors with buttons  (Read 584 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi! I've got a problem.
I created this circuit with Arduino which allowed me to control a motor counterclockwise and clockwise.

But i would like to create a little car so i would like to create a button, and when i press it the motors start running counterclockwise. How can i do?
This is my code but it doesn't works. Can you help me? thanks smiley-grin


int motorpin1=1; //motore1
int motorpin2=2; //motore di destra
int motorpin3=3;
int motorpin4=4;

int button1=9;

int val1=0;


void setup()  {
  pinMode(motorpin1, OUTPUT);
  pinMode(motorpin2, OUTPUT);
  pinMode(motorpin3, OUTPUT);
  pinMode(motorpin4, OUTPUT);

  pinMode(button1, INPUT);

}

void loop(){
  val1=digitalRead(button1);

  if (val1==HIGH){
    digitalWrite(motorpin1, HIGH); //senso orario
  digitalWrite(motorpin2, LOW);
  digitalWrite(motorpin3, LOW);
  digitalWrite(motorpin4, HIGH);

    }

  }
« Last Edit: December 20, 2013, 01:07:48 pm by fabioriga » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50423
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Generally, switches work better than buttons, unless you are trying to keep your shirt on.

It's impossible to tell you why your code does not work, since you have not told us how the switch is wired, what the code actually does, or what you want it to do.
Logged

Victoria, Australia
Offline Offline
Edison Member
*
Karma: 46
Posts: 1619
Electronics Engineer/Industrial Control
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, your fritz shows 2,3,4,5 are used as motor control output, yet you declare in your code:

Code:
int motorpin1=1; //motore1
int motorpin2=2; //motore di destra
int motorpin3=3;
int motorpin4=4;

int button1=9;

int val1=0;


void setup()  {
  pinMode(motorpin1, OUTPUT);
  pinMode(motorpin2, OUTPUT);
  pinMode(motorpin3, OUTPUT);
  pinMode(motorpin4, OUTPUT);

  pinMode(button1, INPUT);

}

pin 1,2,3,4 as output.

Also I don't regard a fritz as a circuit diagram, so can you post a schematic and a picture of your project.
I have doubts about the transistor configuration.
Tom.... smiley
« Last Edit: December 21, 2013, 03:04:39 am by TomGeorge » Logged

Everything runs on smoke, let the smoke out, it stops running......VK3DMK

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9698
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some simple servo/button code. You might modify to incorporate your PWM code and a third button so you would have forward/stop/reverse buttons.

Code:
//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#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);
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: