Pages: [1]   Go Down
Author Topic: how do i control my 2 servo motors?  (Read 461 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Currently I am working on a project which consists of two servo motors and two push buttons. Here is what I am trying to do, I want the 1st push button (input 9) to stop the motors from moving when pushed and then when I push it again they will go in a clockwise motion, I want this to repeat it self continuously. The 2nd push button(input8) will act as a bumper sensor(I am building a car robot) so the bumper sensor will be at the front of robot. When pushed I want the servos to move in an anti clockwise motion and eventually be able to turn left or right. But for the time being I just want it to go anticlockwise when the button is pressed. When pressed again I want it to go in a clockwise motion again. I have been working on some code but I just cant seem to get it working correctly, I am new to this so I was hoping could someone look at the code and tell me where I am going wrong?


#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
Servo myservo2; // create servo object to control a second servo
 
int button1 = 9; // Upper CW Button (UCW)
int button2 = 8; // Upper CCW Button (UCCW)


void setup()
{
  myservo.attach(3);                       // attaches the servo on pin 9 to the servo object
  myservo2.attach(11);                     // attaches the servo on pin 10 to the servo object
  pinMode (button1, INPUT);
  pinMode (button2, INPUT);
 
}
 
void loop()
{
  if (digitalRead(button1) == HIGH)       
  myservo.write(0);                       
  else if (digitalRead(button1) == LOW) 
  myservo.write(90);                                         
  if (digitalRead(button1) == HIGH)       
  myservo2.write(0);                       
  else if (digitalRead(button1) == LOW) 
  myservo2.write(90);                     
  if (digitalRead(button2) == HIGH)     
  myservo2.write(0);                       
  else if (digitalRead(button2) == LOW)   
  myservo2.write(90);                                           
if (digitalRead(button2) == HIGH)     
  myservo.write(0);                       
  else if (digitalRead(button2) == LOW)   
  myservo.write(90);                       
}
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3639
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have got your loop arranged so that your servos only move when a button is held down. From your description, what you really want is for a button press to toggle between moving and stopped. You need to detect a transition from not pressed to pressed and only then change the state of the motors. Search the programming forum for words like transition, debounce, prevbuttonstate, currbuttonstate; it should pull up some useful examples of using buttons in this way.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Servo toggle test code.

Code:
//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for the tips, I actually understand now about debouncing and what not ill try look at some examples and put it into my code!
Logged

Pages: [1]   Go Up
Jump to: