Servo Controlled by two buttons

Hello,

I am doing a uni project that requires some Arduino work;

I need to have a servo which is controlled by two push buttons (one button to turn it right and the other to turn it left) and have the servos moving until I release the button.
I should also add that the final aim of this is to do it wirelessly although it isnt too large a selling point.
Is there anywhere I can find some sample code which I won't have to modify too much to make this possible as my coding is next to none. And couple people point me into the correct direction as to which modules I should buy?

Thanks a lot guys and gals!

StrathyStudent:
Is there anywhere I can find some sample code which I won't have to modify too much to make this possible as my coding is next to none.

Maybe, but spending a little bit of time understanding the half a dozen lines of example code for switches and servos would probably take less time then finding thing perfect piece of example code.

And couple people point me into the correct direction as to which modules I should buy?

A servo that matches the requirements for your project.

Two momentary switches.

... and a suitable power supply for the servo.

Not exactly what you want, but two button servo test code.

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

StrathyStudent:
Hello,

I am doing a uni project that requires some Arduino work;

I need to have a servo which is controlled by two push buttons (one button to turn it right and the other to turn it left) and have the servos moving until I release the button.

Sounds like you may need a "Continuous Rotation Servo". One which you tell it direction and 'speed' instead of referencing a particular angle to go to.

Thanks for the sample code, will play around with that and see what results I get.

Also thanks for the advice with the continuous rotation servo, found a sample online tho it looks a bit more complex so might have to stick with the 'normal' one! :smiley:

Look at this library Google Code Archive - Long-term storage for Google Code Project Hosting. , you can use single push, multi push & long push all with one button.

So I have been playing with the sample code above and changed it to add the second servo, not sure if it is correct tho. Does anyone mind taking a look at it and telling me whether I need to add anything or change it? Also with regards to the moving, in the code there is a 170 and 10, what do those mean? Is that the amount of pulses they are meant to move?? For the wireless would it just be pretty much the same but at the start putting in the code for the network configuration and then the same code for both the modules? Looking at this particular board http://www.amazon.co.uk/Arduino-A000065-Wireless-SD-Shield/dp/B0084ZOO6Y/ref=sr_1_1?ie=UTF8&qid=1373287753&sr=8-1&keywords=arduino+wireless+shield.

Please keep posting, appreciate any help I can get! :smiley:

#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;
int button3 = 6; //button pin, connect to ground to move servo
int press3 = 0;
int button4 = 7; //button pin, connect to ground to move servo
int press4 = 0;
Servo servo2;

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

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

I am doing a uni project that requires some Arduino work;

I need to have a servo which is controlled by two push buttons (one button to turn it right and the other to turn it left) and have the servos moving until I release the button.

I count 4 buttons.

The link i provided allows you to control a single servo with 1 or two buttons, and allows the servo to run until the button is depressed.

All dependent on how you implement the library for your uni project.

StrathyStudent:
Also with regards to the moving, in the code there is a 170 and 10, what do those mean? Is that the amount of pulses they are meant to move??

void loop()

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

Seriously?
You haven't read what the servo library commands do?

write(int)
Set the angle of the servo in degrees, 0 to 180.

1ChicagoDave:
Seriously?
You haven't read what the servo library commands do?

write(int)
Set the angle of the servo in degrees, 0 to 180.

I had looked for it but I had only ever found tutorials that talk about pulses rather than angles in degrees. I had assumed it was degrees but then assuming is just guessing really.

R/C servos displacement is proportional to the width of the PWM pulses they are supplied with.
Typical pulse widths are from around 1ms (1000us) to 2ms (2000us), with a frame rate of 20ms.
The Arduino maps these pulses times to nominal 0 to 180 angles, but this is just a convenience, and individual servos will vary.
You can if you wish use pulse lengths directly - any value written the Servo library "write" method above 544 will be interpreted as the pulse time in microseconds, or you can use the "writeMicroseconds" method directly.