Different buttons for different loops, building a wired control.

Hi everyone. (Please move this thread if i posted it wrong) Im new into this arduino programing and i totaly love it! Im building a realy simple 3 servo hexapod walker. All the building is completed and im able to program it to do different thing for example walk forward, or turn. But the thing is that i want to build so i can control it with a wired control. For example, when i push "the forward button" on my little wired control, the arduino is supposed run the written "forward loop" to make all the servos do what i have programed them for. And when i push the turn right button, its supposed to run the "turn right" loop.

So i need some help with a script where i can have more then one loop, and each of them are not supposed to run until its button i pushed. I also need help with where to connect the buttons on the board and how to make the arduino read them. Just simple push and release buttons. I dont need any superfancy scripts, just something to make it work. But if its not to hard to write, it would be awsome if the loop would stop when the button is released. It gives a bit more control. But thats not necesary. Its my first bot right :P And also, i only need 4 buttons to start with, so i think the inputs is enough on the arduino uno.

Here is the "base" script that im using to set up a single loop to make the robot do something. I dont need the whole script to be written here, i just need an example of a 2-button-2-loop script and i will probably understand the deal and be able to add more loops myself. It would be great if the new script could be built around this one so i can recognize some parts of it. Makes it alot easier to understand. Im using a arduino uno.

Thanks for a great forum!

#include 

Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo

void setup() { 
  servoLeft.attach(13);  // Set left servo to digital pin 13
  servoRight.attach(12);  // Set right servo to digital pin 12
                          // This is an early script, thats why the 3rd servo isnt added yet
} 

void loop() {    // I want to have more loops.And they are supposed to starts when i push a defined button for example "forward loop"        
  stopRobot();   // What routine to run
  delay(4000);   // How long it runs that routine
  turnLeft();
  delay(6000);
  forward();
  delay(2000);
  reverse();
  delay(2000);
}

// Motion routines for forward, reverse, turns, and stop. The motion routines dont need any modifications to do this? Am i right?
// I want to use the same routines but in different orders in the different loops
void reverse() {
  servoLeft.write(0);
  servoRight.write(180);
}

void forward() {
  servoLeft.write(180);
  servoRight.write(0);
}

void turnRight() {
  servoLeft.write(0);
  servoRight.write(80);
}
void turnLeft() {
  servoLeft.write(180);
  servoRight.write(79);
}

void stopRobot() {
  servoLeft.write(80);
  servoRight.write(80);
}
const int LeftButtonPin = 2;
const int RightButtonPin = 3;
const int ForwardButtonPin = 4;
const int BackwardButtonPin = 5;

bool goLeft() {return digitalRead(LeftButtonPin);}
bool goRight() {return digitalRead(RightButtonPin);}
bool goForward() {return digitalRead(ForwardButtonPin);}
bool goBackward() {return digitalRead(BackwardButtonPin);}


void loop()
   {
   if (goLeft())
        turnLeft();
    else
    if (goRight()) 
        turnRight();
    else
    if (goForward())
        forward();
    else
    if (goBackward())
        reverse();
    else
       stopRobot();
    }

You can even add combinations like (if(goLeft() && goForward()) to circle left instead of turning in place.

bool goLeft() {return digitalRead(LeftButtonPin);}
bool goRight() {return digitalRead(RightButtonPin);}
bool goForward() {return digitalRead(ForwardButtonPin);}
bool goBackward() {return digitalRead(BackwardButtonPin);}

Please do NOT use these names!

A function name should indicate what the function does. The names indicates to be that the function should make something go left, right, forward or backward. These functions do not do that. Names like shouldGoLeft(), shouldGoRight(), etc. are better, in my opinion.

You got a point there, thanks. This will help ALOT. Is it possible to make one specifik servo move to different positions in the same routine? Dont see how i am supposed to make the walker walk without using a whole loop with servopositions. So if its possible to store several positions in a specifik order in a routine it would have been great? Is it possible to use the delay function in the routine to do that? Or can i just insert another row for that servo in the routine? As the script is now, it would work great with a continously rotating servo, but thats not my case. Does this work for an example? Voidforwars(){ ServoLeft(180); ServoLeft(36); Servoright(65);

Dont see how i am supposed to make the walker walk without using a whole loop with servopositions.

Terminology is important. What do you mean by “a whole loop of servo positions”? You can store the necessary positions for taking a step in an array (2D?), and iterate through the array(s), making each servo move as needed.

So if its possible to store several positions in a specifik order in a routine it would have been great?

Would have? You still can. Is this a question somehow?

Is it possible to use the delay function in the routine to do that?

In what routine (function)?

Thank you so much everyone! Ive got it all working with your help. This is my final code and its working like a charm. Its a realy simple way to run different movementpatterns for my bot :) Thanks again!

#include 

Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo
Servo servoMiddle;        // Define right servo
const int LeftButtonPin = 7;
const int RightButtonPin = 6;
const int ForwardButtonPin = 4;
const int BackwardButtonPin = 5;

bool goLeft() {return digitalRead(LeftButtonPin);}
bool goRight() {return digitalRead(RightButtonPin);}
bool goForward() {return digitalRead(ForwardButtonPin);}
bool goBackward() {return digitalRead(BackwardButtonPin);}

void setup() { 
  servoLeft.attach(13);  // Set left servo to digital pin 13
  servoRight.attach(12);  // Set right servo to digital pin 12
  servoMiddle.attach(11);  // Set right servo to digital pin 11
                          // This is an early script, thats why the 3rd servo isnt added yet
} 

void loop() {    // I want to have more loops.And they are supposed to starts when i push a defined button for example "forward loop"        
 if (goLeft())
        turnLeft();
    else
    if (goRight()) 
        turnRight();
    else
    if (goForward())
        forward();
    else
    if (goBackward())
        reverse();
    else
       stopRobot();


}

// Motion routines for forward, reverse, turns, and stop. The motion routines dont need any modifications to do this? Am i right?
// I want to use the same routines but in different orders in the different loops
void reverse() {
  servoMiddle.write(80);
  delay(500);
  servoLeft.write(135);
  servoRight.write(135);
  delay(500); 
  servoMiddle.write(101);
  delay(500);
  servoRight.write(75);
  servoLeft.write(75);
  delay(500);
}

void forward() {
  servoMiddle.write(80);
  delay(500);
  servoLeft.write(75);
  servoRight.write(75);
  delay(500); 
  servoMiddle.write(101);
  delay(500);
  servoRight.write(135);
  servoLeft.write(135);
  delay(500);
}

void turnLeft() {
  servoMiddle.write(100);
  delay(500);
  servoLeft.write(135);
  delay(500); 
  servoMiddle.write(80);
  delay(500);
  servoLeft.write(75);
  delay(500); 
}
void turnRight() {
  servoMiddle.write(80);
  delay(500);
  servoRight.write(75);
  delay(500); 
  servoMiddle.write(100);
  delay(500);
  servoRight.write(135);
  delay(500); 
}

void stopRobot() {
  servoLeft.write(105);
  servoRight.write(105);
  servoMiddle.write(90);
}