Let's Make Robots!

Different buttons for different loops, building wired control

Hi everyone.
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 I only need this to work for 4 buttons to begin with.

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.

Thanks for a great forum!

 

#include <Servo.h>

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

That doesnt look to complex. And to answer your question, yes i have done some research now and i now know how to connect buttons. So this is what i needed to be able to continue. Just one more question. Is it possible to put a delay in  a certain routine to make the servo go to different positions in the same routine? See my example below         voidturnleft(){

servoleft.write(180);

delay(100);

servoleft.write(65)

i need to use the same servo several times in the same routine but in different positions. Is this even possible?

 

why don't you jus try it out and see what happens?

You just need some input buttons and some "If's".

First off, the input buttons. When you did a search for arduino and button, what came up? 

Now, in terms of buttons and routines. In your main loop, you simply need to check if any of your buttons have been pushed (using a digitalRead command) and if they are, zap down to the corresponding routine.

void loop()

{
   if (digitalRead(whatever)==high or low (depending on how its wired)
   {
       forward();
   } 
   if (digitalRead(whatever else)==high or low (depending on how its wired)
   {
      reverse();
   }  
   if (digitalRead(some other pin)==high or low (depending on how its wired)
   {
       turn or something();
   }   

}