My function doesnt gett pass the delay (wont loop)

Hello everyone.
Ive been building on a quadropod for some time and i have got alot of help from this forum. But once again im stuck
This time its in the function part of the code.

Here is the function void, ive been using it in demo mode so i know it works.

void forward() {
  servo11.write(50); // Only runs
  servo12.write(50); // this part
  delay(2000);       // Doesnt get pass this
  servo11.write(100); // Never gets here :(
  servo12.write(100);
  delay(2000);
  servo11.write(30);
  servo12.write(30);
}

But i dont think the problem lies there. See, im using multiple buttons on one analog pin, and i think its something wrong with the code handling that part. Or maybe the loop void. Im totaly stuck on this part.

But the thing is that the buttons work, so there is nothing wrong with the wiring or the “base” code handling them.
Its something that prevents the function void to “loop” and not get pass the delay.

And also, when i increase the delay, its just like im constantly pushing the button for that amount of time, even if im not. I want it to run the function void only when im pushing the button.

Here is the whole code. Im not getting any error.

#include <Servo.h>

Servo servo11;          // Defines how many servos that are connected, also tells their names
Servo servo12; 
Servo servo13; 
Servo servo21; 
Servo servo22;
Servo servo23;
Servo servo31;   
Servo servo32;    
Servo servo33;  
Servo servo41;   
Servo servo42;
Servo servo43;

const int buttonPin = 0;      // the number of the pushbutton pin

const int BUTTON1 = 1;        // Creates a virtual button on pin 0
const int BUTTON2 = 2;     
const int BUTTON3 = 3;     
const int BUTTON4 = 4;     
const int BUTTON5 = 5;     
const int BUTTON6 = 6;     


const int BUTTON1LOW = 820;   //Tells low cap for button listening
const int BUTTON1HIGH = 860;  //Tells high cap for button listening
const int BUTTON2LOW = 970;   //Tells low cap for button listening
const int BUTTON2HIGH = 982; //Tells high cap for button listening
const int BUTTON3LOW = 925;   //Tells low cap for button listening
const int BUTTON3HIGH = 935; //Tells high cap for button listening
const int BUTTON4LOW = 690;   //Tells low cap for button listening
const int BUTTON4HIGH = 701;  //Tells high cap for button listening
const int BUTTON5HIGH = 1020; //Tells high cap for button listening
const int BUTTON5LOW = 1025;   //Tells low cap for button listening
const int BUTTON6HIGH = 1000; //Tells high cap for button listening
const int BUTTON6LOW = 1005;   //Tells low cap for button listening




void setup() 
{ 
  servo11.attach(13);          // Tells what pin the different servos are connected to
  servo12.attach(12); 
  servo13.attach(11); 
  servo21.attach(10);
  servo22.attach(9); 
  servo23.attach(8); 
  servo31.attach(7); 
  servo32.attach(6);
  servo33.attach(5); 
  servo41.attach(4); 
  servo42.attach(3); 
  servo43.attach(2);
  
  pinMode(buttonPin, INPUT); //Sets the buttonpin(0) to input
}

 void loop() {
   // read the state of the switch into a local variable:
   int reading = analogRead(buttonPin);   //tells the buttonpin to read analog input
   
  if(reading>BUTTON6LOW && reading<BUTTON6HIGH){//Read switch 6
     rotRight(); //execute the rotRight function
   }else if(reading>BUTTON5LOW && reading<BUTTON5HIGH){
     //Read switch 5
     rotLeft();
   }else if(reading>BUTTON4LOW && reading<BUTTON4HIGH){
     //Read switch 4
     turnRight();
   }else if(reading>BUTTON3LOW && reading<BUTTON3HIGH){
     //Read switch 3
     turnLeft();
   }else if(reading>BUTTON2LOW && reading<BUTTON2HIGH){
     //Read switch 2
     reverse();
   }else if(reading>BUTTON1LOW && reading<BUTTON1HIGH){
     //Read switch 1
     forward();
   }else{
     //No button is pressed;
     wait();
   }
 }
void wakeup() {
  servo11.write(90); 
  servo12.write(90); 
  servo13.write(90); 
  servo21.write(90);
  servo22.write(90); 
  servo23.write(90); 
  servo31.write(90); 
  servo32.write(90);
  servo33.write(90); 
  servo41.write(90); 
  servo42.write(90); 
  servo43.write(90);
}
void reverse() {
  servo31.write(50);
  servo32.write(50);
}

void forward() {
  servo11.write(50); // Only runs
  servo12.write(50); // this part
  delay(2000);       // Doesnt get pass this
  servo11.write(100); // Never gets here :(
  servo12.write(100);
  delay(2000);
  servo11.write(30);
  servo12.write(30);
}

void turnRight() {
  servo11.write(0);
  servo12.write(80);
}
void turnLeft() {
  servo11.write(180);
  servo12.write(0);
  delay(1000);
  servo11.write(0);
  servo12.write(180);
  delay(5000);

}
void rotRight() {
  servo11.write(180);
  servo12.write(0);
  delay(1000);
  servo11.write(0);
  servo12.write(180);
  delay(5000);

}  
  void rotLeft() {
  servo11.write(180);
  servo12.write(0);
  delay(1000);
  servo11.write(0);
  servo12.write(180);
  delay(5000);

}
  void stopRobot() {
  servo11.write(80);
  servo12.write(80);
}
void wait() {  
  servo11.write(90); 
  servo12.write(90); 
  servo13.write(90); 
  servo21.write(90);
  servo22.write(90); 
  servo23.write(90); 
  servo31.write(90); 
  servo32.write(90);
  servo33.write(90); 
  servo41.write(90); 
  servo42.write(90); 
  servo43.write(90);
}

Here is the function void

No, there is the function forward(), declared void because it doesn't return a value.

I suggest you put some debugging prints in, to confirm what the code is doing.

I don't think you should be using 'delay'. If nothing else, how are your servos going to get refresh() called on them every 50ms http://www.arduino.cc/playground/ComponentLib/Servo

Have a look at the 'blink without delay example' (use millis()).

Refresh?

I'm really not having a good day - I was looking at the Software Servo docs:

The Software Servo Library can drive servos on all of your pins simultaneously. The API is patterned after the wiring.org servo library but the code is different. You are not limited to 8 servos, but you must call the SoftwareServo::refresh() method at least once every 50ms or so to keep your servos updating.

Looks like that doesn't apply to the standard servo library.

I my defence I have 'man flu' and am trying to relieve the boredom. Think I'll just have a nap now!

Problem solved. I think.

void forward() {
  servo11.write(50); // Only runs
  servo12.write(50); // this part
  delay(2000);       // Doesnt get pass this
  servo11.write(100); // Never gets here :(
  servo12.write(100);
  delay(2000);
  servo11.write(30);
  servo12.write(30);
  delay(2000);            //  <==  Added this :)
}

Are the refresh necessary? Its seems to work just fine now :S

Think you should put the button handling in a function and use a switch to make the code a bit easier to read (imho). If you use another type of button it is easier to adapt the code.

Have a look (not tested)

void loop() 
{
   int command = readButton(0);

  switch(command)
  {
  case 6 : rotRight(); break;
  case 5 : rotLeft(); break;
  case 4 : turnRight(); break;
  case 3 : turnLeft(); break;
  case 2 : reverse(); break;
  case 1 : forward(); break;
  default: wait(); break;
  }
}

int readButton(int pin)
{
  // multiple readings for a more stable result
  int val = 0;
  for (int i=0; i<32; i++) val += analogRead(pin);
  val /= 32;

  if (BUTTON6LOW < val && val < BUTTON6HIGH) return 6;
  if (BUTTON5LOW < val && val < BUTTON5HIGH) return 5;
  if (BUTTON4LOW < val && val < BUTTON4HIGH) return 4;
  if (BUTTON3LOW < val && val < BUTTON3HIGH) return 3;
  if (BUTTON2LOW < val && val < BUTTON2HIGH) return 2;
  if (BUTTON1LOW < val && val < BUTTON1HIGH) return 1;

  return -1;
}

thanks robtillaart! Just tried out the code you suggested and it works great. Alot easier to read and understand. Maybe its just in my imagination but it feels like the bot works less jittery now.

Now im gonna tackle how to decrease the speed of all servos with as little code as possible :)