4 servo Arduino Mega 2560 REV3 button setup?

Hello everyone, let me start by saying that I've never used an Arduino product before but I do know my way around C++ and I'm hoping some of the awesome people here on the forums could help me with my little project.

I need four servos to be controlled by an Arduino board with 4 buttons. (I've heard that the Arduino Mega 2560 REV3 is the best board for this but please instruct me if I'm wrong.)

When I push button 1, servo 1 moves 180 degrees and stops. When I press button 1 again, it moves 180 degrees back to it's starting position.
When I push button 2, servo 2 moves 180 degrees and stops. When I press button 2 again, it moves 180 degrees back to it's starting position.
When I push button 3, servo 3 moves 180 degrees and stops. When I press button 3 again, it moves 180 degrees back to it's starting position.
When I push button 4, servo 4 moves 180 degrees and stops. When I press button 4 again, it moves 180 degrees back to it's starting position.

I've Googled this and came up with this example which is similar to what I need, but I just need this setup for 4 servos.

video: Arduino; push button to control a servo - YouTube

Can someone help me modify this code to accommodate 4 servos?

01 #include <Servo.h>
02
03 // Set digital pin numbers:
04 const int servoPin = 8;  // The number of the Servo pin
05 const int buttonPin = 9;  // The number of the Pushbutton pin
06
07 int buttonState = 0;  // Variable for reading the pushbutton status
08 int directionState = 0;  // Variable for reading direction of the servo
09
10 Servo myservo;  // Create servo object to control a servo
11
12 int pos = 0;  // Variable to store the servo position
13
14
15 void setup() {
16   myservo.attach(8);  // attaches the servo on pin 8 to the servo object
17   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
18 }
19
20 void loop(){
21   // read the state of the pushbutton value:
22   buttonState = digitalRead(buttonPin);
23
24   if (directionState == 0){
25     //The button is pushed
26     if (buttonState == HIGH) {
27       directionState = 1;// The direction for the servo is clockwise
28
29       // goes from 0 degrees to 180 degrees in steps of 1 degree
30       for(pos = 0; pos < 180; pos=pos+1)
31       {
32         myservo.write(pos);  // tell servo to go to position in variable ‘pos’
33         delay(15);  // waits 15ms for the servo to reach the position
34       }
35     }
36
37   } else if (directionState == 1) {
38     // The button is pushed
39     if (buttonState == HIGH) {
40       directionState = 0;  // The direction for the servo is anti-clockwise
41
42       // goes from 180 degrees to 0 degrees in steps of 1 degree
43       for(pos = 180; pos>=1; pos=pos-1)
44       {
45         myservo.write(pos);  // tell servo to go to position in variable ‘pos’
46         delay(15);  // waits 15ms for the servo to reach the position
47       }
48     }
49   }
50 }

Also, could someone please guide me on what I need to purchase for this project?

I know that I need a Arduino Mega 2560 (do I need the latest REV3 model)?
http://www.ebay.com/itm/130689067199?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1438.l2649

Arduino Push Button v2.0 module for Sensor Shield (do I need a sensor shield? It doesn't look like the person in the above video used one)
http://www.ebay.com/itm/270686390287?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1438.l2649

Mini Solderless Breadboard Bread Board 400 Contacts Available
http://www.ebay.com/itm/170740345947?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1438.l2649

Also, this is going to be a portable project so I have to figure out the best way to power the Arduino Mega 2560 and the 4 servos. Any suggestions?

Anything else that I need? Also, what servos do you recommend?

Thanks again! Mike

You don't need a Arduino Mega 2560 for this, any "standard" Arduino (like the UNO) will do too. You need 8 pins (4 for the buttons, 4 for the servos), so the UNO has more than enough.

In your sketch just replace servoPin, buttonPin, myservo and pos by an appropriate array and loop over them in loop().

pylon:
You don't need a Arduino Mega 2560 for this, any "standard" Arduino (like the UNO) will do too. You need 8 pins (4 for the buttons, 4 for the servos), so the UNO has more than enough.

In your sketch just replace servoPin, buttonPin, myservo and pos by an appropriate array and loop over them in loop().

Thanks for the quick reply. Please let me know if I'm understanding this correctly. The Arduino Uno is equipped with 14 digital I/O pins, six of which can be used as PWM outputs, 6 analog inputs. Are the servos considered PWM outputs and buttons considered analog inputs? So I could use up to 6 servos with this board?

Also, if I wanted to add an EasyVR Arduino Shield to replace the buttons and control the servos by voice commands, would this be feasible with the above code modified? And if I did use EasyVR Arduino Shield would this take up all of the 6 analog inputs? Thanks again for the help! I really appreciate it!

VeeaR - EasyVR Arduino Shield
http://domit.co.uk/en/arduino-veear-easyvr-shield.html

According to the Servo library documentation

you can use up to 12 servos on a standard Arduino (UNO and the like) but you must give up PWM output on two pins (9 and 10). Probably the library is using their PWM timer for it's handling. Pins A0 to A5 are analog inputs by default but can be GPIO (digital pins) too if you program them accordingly. Buttons are considered digital inputs so you can use any of the GPIO.

On the schematics of the EasyVR shield I cannot see any pin used except the serial connection. But I also cannot find the executing chip in it, so there might be other connections which are simply not in the schematics. The web page says that the shield offers 3 additional GPIO so it may be almost a zero sum game, using 2 pins (UART) and giving you 3 controlled by serial commands. I see no hint that the shield uses any of the analog inputs but as I said, I think it's (the schematics) incomplete.

Ok, I think I understand what your saying. I found this drawing by user: zoomkat

at this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1287331227/30

From reading that thread they came to the conclusion that 6v isn't enough to power multiple servos and suggested a larger battery.

Do you think an 20 Cell 11.1V 2100mAh 3S LiPo Battery would be sufficient for 4-6 servos?

I plan on using four 20kg Towerpro 9805bb servos
link: Radio Control Planes, Drones, Cars, FPV, Quadcopters and more - Hobbyking

http://www.amazon.com/Cell-11-1V-2100mAh-LiPo-Battery/dp/B000HKH02Y/ref=sr_1_2?ie=UTF8&qid=1336754349&sr=8-2

So what I plan on doing is powering my servos with the Lipo battery and powering my Arduino board with a standard 9v battery. (or would it be better to forget the 9v and power the board and the servos with Lipo battery with a voltage regulator for the board?)

Thanks for the help! I really really appreciate all the help!

I never seen a block battery with 6V, here they're always 9V. 9V should be enough for the 7805 to create a stable 5V (source should at least 2V more then target). I'm not a specialist with batteries but in my understanding a block battery isn't able to supply the current for more than perhaps one servo, especially such a powerful one you desire. May the LiPo has enough power but take care. LiPo batteries must be controlled by a special controller made for them else you take the danger of fire.
Don't you have a wall plug near to power such power-consuming servos?
And don't take a 7805 for that. The 6V are just burnt to heat, you would have to install a huge cooler.

pylon:
I never seen a block battery with 6V, here they're always 9V. 9V should be enough for the 7805 to create a stable 5V (source should at least 2V more then target). I'm not a specialist with batteries but in my understanding a block battery isn't able to supply the current for more than perhaps one servo, especially such a powerful one you desire. May the LiPo has enough power but take care. LiPo batteries must be controlled by a special controller made for them else you take the danger of fire.
Don't you have a wall plug near to power such power-consuming servos?
And don't take a 7805 for that. The 6V are just burnt to heat, you would have to install a huge cooler.

OK, I think I might start another thread asking what battery people would recommend for this project. I found this battery that I think will work but I don't know: ZIPPY Flightmax 2500mAh 6.6V 5C LiFePo4 Receiver Pack (DE Warehouse)

http://www.hobbyking.com/hobbyking/store/uh_viewItem.asp?idProduct=21220

With this battery I won't need the 7805 I believe. :~

Powering an Arduino from a battery always needs some kind of voltage regulation. A simple voltage regulator just holds it's output a fixed voltage level and the difference to the input voltage is "converted" to heat. The more current you are using from such a power source the more heat you generate.
A switching DC/DC converter can convert a bigger part of the input energy to the output (also holding a fixed voltage), so you get less heat when increasing the current. Such a thing was meant by "don't take a 7805". An Iteaduino (http://iteadstudio.com/store/index.php?main_page=product_info&products_id=467) has a built-in DC/DC converter capable of giving you up to 3A of current.

As for your batteries: They seem to be much safer to use than other Li types but as I already said, I'm not a specialist for batteries. Maybe someone with experience in this field may help you.