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