Go Down

Topic: Arduino motors with buttons (Read 614 times) previous topic - next topic

fabioriga

Dec 20, 2013, 06:55 pm Last Edit: Dec 20, 2013, 07:07 pm by fabioriga Reason: 1
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 :D


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

   }

 }

PaulS

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.

TomGeorge

#2
Dec 21, 2013, 09:00 am Last Edit: Dec 21, 2013, 09:04 am by TomGeorge Reason: 1
Hi, your fritz shows 2,3,4,5 are used as motor control output, yet you declare in your code:

Code: [Select]
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.... :)
Everything runs on smoke, let the smoke out, it stops running......VK3DMK

zoomkat

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: [Select]

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

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

Go Up