Go Down

Topic: how do i control my 2 servo motors? (Read 605 times) previous topic - next topic

hads123

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

wildbill

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.

zoomkat

Servo toggle test code.

Code: [Select]

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

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

hads123

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!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy