Why .attach something to an object?

I am new to c++ but have done well with qbasic. I do not understand the purpose of making an object and attachings varibles to it in c++. I am building my first octapod robot. I want to have just two servos per leg(wth the option of adding in a third.) The basic idea I want to do is:

Initialize:
// will run a small code to center and align sensors//
// will only be needed at startup//
Main:
Input all sensors
//in qbasc i used a=input(889) and the other 4 status inputs//
If a=1 then gosub lowbattery
If b=1 then gosub wallinfront
If walkingmode=1 then gosub forward
//and any other "event"
goto main:
forward:
Kneea=up
hipa=forward
If walkingflag=1 then hipb=back
Kneea=down

Kneeb=up
hipb=forward
If walkingstate=1 then hipa=back //there are 2 sets of legs,4 legs in each set. I want each set to move "forward" together//
kneeb=down
walkingflag=1
return

I hope some of that you can understand and sorry for my ignorance.

Is there a gosub function n c++?
Can I make a walkforward function?

Thank you in advance for the help
Bob

Is there a gosub function n c++?
Can I make a walkforward function

There is no gosub - a call to a function is implicit in the name, but you must use ().
So, "Serial.print ("here");" call the "print" method (function) belonging to the object "Serial".

Yes, you can make your own functions.

I do not understand the purpose of making an object and attachings varibles to it in c++.

A servo object controls a servo, but it needs a hardware resource (a pin) to connect to the servo.
Tha "attach" method associates the pin with the object.

Can an object have more then one varible attached to it? Such as the "front right leg has two servos?" I am having trouble relating this.
Say I have two functions. One is walkforward and one is walkbackward.
I want the walkforward to run one subroutine and walkbackswards to run another.
Each subroutine will turn the servos in sequence then return to the main subroutine.
How does the leg relate to an object and the servos "attachng" to it.

Can an object have more then one varible attached to it? Such as the "front right leg has two servos?" I am having trouble relating this.

A Servo object can only control one servo (Well, it can control more than one by attaching multiple servos to the same pin, but they'd all be doing the same thing)
You could create a "Leg" class, that has two or more Servo object members.

So, I know my variables, I know some of my functions, but I am still not sure where a class would fit in.
I have some of my code started. I think it may be to much of a wall of text to post it all on here.

This is my "forward" function:

void forward() {
if (front_ping_sensor_hit==10) return;
leg_ra1_servo.write(leg_ra1_up);
delay(servo_timer_delay);
leg_la2_servo.write(leg_la2_up);
delay(servo_timer_delay);
leg_ra3_servo.write(leg_ra3_up); //////seta legs up
delay(servo_timer_delay);
leg_la4_servo.write(leg_la4_up);
delay(servo_timer_delay);
hip_ra1_servo.write(hip_ra1_forward);
delay(servo_timer_delay);
hip_la2_servo.write(hip_la2_forward);
delay(servo_timer_delay);
hip_ra3_servo.write(hip_ra3_forward); //////seta hips forward
delay(servo_timer_delay);
hip_la4_servo.write(hip_la4_forward);
delay(servo_timer_delay);
if (standing_state==0) hip_rb5_servo.write(hip_rb5_backward);
delay(servo_timer_delay);
if (standing_state==0) hip_lb6_servo.write(hip_lb6_backward);
delay(servo_timer_delay);
if (standing_state==0) hip_rb7_servo.write(hip_rb7_backward); //////setb hips back only if
delay(servo_timer_delay);
if (standing_state==0) hip_lb8_servo.write(hip_lb8_backward);
delay(servo_timer_delay);
leg_ra1_servo.write(leg_ra1_down);
delay(servo_timer_delay);
leg_la2_servo.write(leg_la2_down); ////////////seta leg down
delay(servo_timer_delay);
leg_ra3_servo.write(leg_ra3_down);
delay(servo_timer_delay);
leg_la4_servo.write(leg_la4_down);
delay(servo_timer_delay);
leg_rb5_servo.write(leg_rb5_up);
delay(servo_timer_delay);
leg_lb6_servo.write(leg_lb6_up);
delay(servo_timer_delay);
leg_rb7_servo.write(leg_rb7_up); //////setb legs up
delay(servo_timer_delay);
leg_lb8_servo.write(leg_lb8_up);
delay(servo_timer_delay);
hip_rb5_servo.write(hip_rb5_forward);
delay(servo_timer_delay);
hip_lb6_servo.write(hip_lb6_forward);
delay(servo_timer_delay);
hip_rb7_servo.write(hip_rb7_forward); //////setb hips forward
delay(servo_timer_delay);
hip_lb8_servo.write(hip_lb8_forward);
delay(servo_timer_delay);
if (standing_state==0) hip_ra1_servo.write(hip_ra1_backward);
delay(servo_timer_delay);
if (standing_state==0) hip_la2_servo.write(hip_la2_backward);
delay(servo_timer_delay);
if (standing_state==0) hip_ra3_servo.write(hip_ra3_backward); //////seta hips back only if
delay(servo_timer_delay);
if (standing_state==0) hip_la4_servo.write(hip_la4_backward);
delay(servo_timer_delay);
leg_rb5_servo.write(leg_rb5_down);
delay(servo_timer_delay);
leg_lb6_servo.write(leg_lb6_down); ////////////setb leg down
delay(servo_timer_delay);
leg_rb7_servo.write(leg_rb7_down);
delay(servo_timer_delay);
leg_lb8_servo.write(leg_lb7_down);
delay(servo_timer_delay);
standing_state = 1;
x=x=1;
}

Is there a better way to do this?

Thanks for the help.
Bob

Is there a better way to do this?

Almost certainly - an array of servo objects, and an array of actions would shorten the code, I'm sure.
Define your gait in those terms, and I would think your code would reduce drastically.

Or did you mean "is there a better way to post code?", in which case the answer is most definitely "yes".

Use the code "#" icon on the toolbar, not the quote.

x=x=1;

Is that deliberate?
Why?

I know i can use an array, but i am not sure how. I have 24 indivdual servos, they are all one of 8 knees , 8 hips or 8 legs. Each servo will be positioned in 1 of 3 points.(min,90,max) I have tried to work it out, but i am having trouble by just reading how to do it. I am not sure where to start, or how to find the order of them.

and no, It was not deliberate. and thanks.

It seems to me that the choice is between a limb-centric view, or a joint-centric view.
Whichever produces the kind of gait you want, I can't say (because I simply don't know).

However, those delays are going to mean that simultaneous movement of multiple joints is going to be difficult, and getting rid of them whilst retaining your current code structure is similarly tricky.

To be honest, I only really know the gait I want. A spider uses 2 sets of 4 for its gait. It picks the first 4 legs up, then moves those 4 forward, then moves second set of legs back, and so on then repeats the same but for the second set. That is the only part i am sure of. I think joint-centric would be best, as I plan on moving all 8 leg(joints) then all 8 knee(joints) and such.

How would I make this a starting point for a array:

move.forward.knee.6=90
or
move.turn_left.(hip)(3)=180
or
stand.stand.(hip)(3)=90

move stand are functions
forward turn_left are functions
knee and hip are objects in an array?
6 and 3 are the legs, in an array
90 in the position to move the servo

AWOL:
However, those delays are going to mean that simultaneous movement of multiple joints is going to be difficult, and getting rid of them whilst retaining your current code structure is similarly tricky.

=my code is terrible, sorry, but I am just learning.

I do not know of another way to do it with a sketch. Any suggestions?

but I am just learning

Breagle,

Think you should spend some time in the tutorial section and the reference section of Arduino.cc There are a lot of relative simple examples that tell much about how the arduino can be programmed. It's important to get the basics right.

I do mostly a sort of top down approach, define the program in large chunks with clear interfaces. Then the chunks become a mini project existing from smaller chunks. etc.

Ok, here is the code i have so far. I would like to break it down more with arrays. I am not sure how to apply them to my sketch.

void battery() {
  if ((battery_value)<low_battery_value){
    battery_low=1;
    digitalWrite(battery_low_led, HIGH);   // turn the battery low led on  
  }
  else {
    battery_low=0;
    digitalWrite(battery_low_led, LOW); // turn the battery low led off:   
  }
}
void front_scan() {
  long duration, cm_front;
  pinMode(front_ping_Pin, OUTPUT);
  digitalWrite(front_ping_Pin, LOW);
  delayMicroseconds(2);
  digitalWrite(front_ping_Pin, HIGH);
  delayMicroseconds(5);
  digitalWrite(front_ping_Pin, LOW);
  pinMode(front_ping_Pin, INPUT);
  duration = pulseIn(front_ping_Pin, HIGH);
  cm_front = microseconds_to_centimeters1(duration);
  front_distance=cm_front;
  if (cm_front<ping_max_front)front_ping_sensor_hit=1; 
  else front_ping_sensor_hit=0; 
  delay(ping_timer_delay);
}
long microseconds_to_centimeters1(long microseconds)
{
  return microseconds / 29 / 2;
}
void stand(){
  knee_ra1_servo.write(knee_ra1_stand); // puts servo in stand position
  knee_la2_servo.write(knee_la2_stand); // puts servo in stand position
  knee_ra3_servo.write(knee_ra3_stand); // puts servo in stand position
  knee_la4_servo.write(knee_la4_stand); // puts servo in stand position
  knee_rb5_servo.write(knee_rb5_stand); // puts servo in stand position
  knee_lb6_servo.write(knee_lb6_stand); // puts servo in stand position
  knee_rb7_servo.write(knee_rb7_stand); // puts servo in stand position
  knee_lb8_servo.write(knee_lb8_stand); // puts servo in stand position
  delay(servo_timer_delay);
  leg_ra1_servo.write(leg_ra1_stand); // puts servo in stand position
  leg_la2_servo.write(leg_la2_stand); // puts servo in stand position
  leg_ra3_servo.write(leg_ra3_stand); // puts servo in stand position
  leg_la4_servo.write(leg_la4_stand); // puts servo in stand position
  leg_rb5_servo.write(leg_rb5_stand); // puts servo in stand position
  leg_lb6_servo.write(leg_lb6_stand); // puts servo in stand position
  leg_rb7_servo.write(leg_rb7_stand); // puts servo in stand position
  leg_lb8_servo.write(leg_lb8_stand); // puts servo in stand position
  delay(servo_timer_delay);
  hip_ra1_servo.write(hip_ra1_stand); // puts servo in stand position
  hip_la2_servo.write(hip_la2_stand); // puts servo in stand position
  hip_ra3_servo.write(hip_ra3_stand); // puts servo in stand position
  hip_la4_servo.write(hip_la4_stand); // puts servo in stand position
  hip_rb5_servo.write(hip_rb5_stand); // puts servo in stand position
  hip_lb6_servo.write(hip_lb6_stand); // puts servo in stand position
  hip_rb7_servo.write(hip_rb7_stand); // puts servo in stand position
  hip_lb8_servo.write(hip_lb8_stand); // puts servo in stand position
  delay(servo_timer_delay);
  standing_state=1;
  x=x+1;
}
void forward() {
  if (front_ping_sensor_hit==1) return;
  leg_ra1_servo.write(leg_ra1_up);
  leg_la2_servo.write(leg_la2_up);
  leg_ra3_servo.write(leg_ra3_up);      //////seta legs up
  leg_la4_servo.write(leg_la4_up);
  delay(servo_timer_delay);
  hip_ra1_servo.write(hip_ra1_forward);
  hip_la2_servo.write(hip_la2_forward);
  hip_ra3_servo.write(hip_ra3_forward);    //////seta hips forward
  hip_la4_servo.write(hip_la4_forward);
  delay(servo_timer_delay);
  hip_rb5_servo.write(hip_rb5_backward);
  hip_lb6_servo.write(hip_lb6_backward);
  hip_rb7_servo.write(hip_rb7_backward);  //////setb hips back   
  hip_lb8_servo.write(hip_lb8_backward);
  delay(servo_timer_delay);
  leg_ra1_servo.write(leg_ra1_stand);
  leg_la2_servo.write(leg_la2_stand); ////////////seta leg down
  leg_ra3_servo.write(leg_ra3_stand);
  leg_la4_servo.write(leg_la4_stand);
  delay(servo_timer_delay);
  leg_rb5_servo.write(leg_rb5_up);
  leg_lb6_servo.write(leg_lb6_up);
  leg_rb7_servo.write(leg_rb7_up);      //////setb legs up
  leg_lb8_servo.write(leg_lb8_up);
  delay(servo_timer_delay);
  hip_rb5_servo.write(hip_rb5_stand);
  hip_lb6_servo.write(hip_lb6_stand);
  hip_rb7_servo.write(hip_rb7_stand);    //////setb hips forward
  hip_lb8_servo.write(hip_lb8_stand);
  delay(servo_timer_delay);
  hip_ra1_servo.write(hip_ra1_stand);
  hip_la2_servo.write(hip_la2_stand);
  hip_ra3_servo.write(hip_ra3_stand);  //////seta hips back only  
  hip_la4_servo.write(hip_la4_stand);
  delay(servo_timer_delay);
  leg_rb5_servo.write(leg_rb5_stand);
  leg_lb6_servo.write(leg_lb6_stand); ////////////setb leg down
  leg_rb7_servo.write(leg_rb7_stand);
  leg_lb8_servo.write(leg_lb8_stand);
  delay(servo_timer_delay);
  standing_state = 0;
  x=x+1;
}
void backward() {
  leg_ra1_servo.write(leg_ra1_up);
  leg_la2_servo.write(leg_la2_up);
  leg_ra3_servo.write(leg_ra3_up);      //////seta legs up
  leg_la4_servo.write(leg_la4_up);
  delay(servo_timer_delay);
  hip_ra1_servo.write(hip_ra1_backward);
  hip_la2_servo.write(hip_la2_backward);
  hip_ra3_servo.write(hip_ra3_backward);    //////seta hips backward
  hip_la4_servo.write(hip_la4_backward);
  delay(servo_timer_delay);
  hip_rb5_servo.write(hip_rb5_forward);
  hip_lb6_servo.write(hip_lb6_forward);
  hip_rb7_servo.write(hip_rb7_forward);  //////setb hips forward only  moving
  hip_lb8_servo.write(hip_lb8_forward);
  delay(servo_timer_delay);
  leg_ra1_servo.write(leg_ra1_stand);
  leg_la2_servo.write(leg_la2_stand); ////////////seta leg down
  leg_ra3_servo.write(leg_ra3_stand);
  leg_la4_servo.write(leg_la4_stand);
  delay(servo_timer_delay);
  leg_rb5_servo.write(leg_rb5_up);
  leg_lb6_servo.write(leg_lb6_up);
  leg_rb7_servo.write(leg_rb7_up);      //////setb legs up
  leg_lb8_servo.write(leg_lb8_up);
  delay(servo_timer_delay);
  hip_rb5_servo.write(hip_rb5_stand);
  hip_lb6_servo.write(hip_lb6_stand);
  hip_rb7_servo.write(hip_rb7_stand);    //////setb hips backward
  hip_lb8_servo.write(hip_lb8_stand);
  delay(servo_timer_delay);
  hip_ra1_servo.write(hip_ra1_stand);
  hip_la2_servo.write(hip_la2_stand);
  hip_ra3_servo.write(hip_ra3_stand);  //////seta hips forward only  moving
  hip_la4_servo.write(hip_la4_stand);
  delay(servo_timer_delay);
  leg_rb5_servo.write(leg_rb5_stand);
  leg_lb6_servo.write(leg_lb6_stand); ////////////setb leg down
  leg_rb7_servo.write(leg_rb7_stand);
  leg_lb8_servo.write(leg_lb8_stand);
  delay(servo_timer_delay);
  standing_state = 0;
  x=x-1;}
void check_status(){
  battery_low=0;
  front_ping_sensor_hit=0;
  battery();
  front_scan();
}
void loop()
{  
  check_status();
  forward();
  forward();
  forward();
  forward();
  stand();
  Serial.println(battery_value);
}

I guess I should narrow it down more. This is what I come up with. But i do not understand how to write to the servos in the loop.
I do not understand how to write to the individul servos in the loop.

int positions_to_stand_[3][8]={
  { 90,90,90,90,90,90,90,90} ,
  { 90,90,90,90,90,90,90,90} ,
  { 90,90,90,90,90,90,90,90}
};
void stand(){
  for (joint_number=0;joint_number<3;joint_number++){ ///joint [0] is the knees, joint[1] is the leg joint[2] is the hips
      for (leg_number=0;leg_number<8;leg_number++){ ///leg_number is each of the eight legs 
          int x=(positions_to_stand_)[joint_number][leg_number];
          knee_ra1_servo.write(knee_ra1_stand); // puts servo in stand position
          knee_la2_servo.write(knee_la2_stand); 
          knee_ra3_servo.write(knee_ra3_stand);
          knee_la4_servo.write(knee_la4_stand); 
          knee_rb5_servo.write(knee_rb5_stand); 
          knee_lb6_servo.write(knee_lb6_stand);
          knee_rb7_servo.write(knee_rb7_stand); 
          knee_lb8_servo.write(knee_lb8_stand); 
          leg_ra1_servo.write(leg_ra1_stand); // puts servo in stand position
          leg_la2_servo.write(leg_la2_stand); 
          leg_ra3_servo.write(leg_ra3_stand); // puts servo in stand position
          leg_la4_servo.write(leg_la4_stand); // puts servo in stand position
          leg_rb5_servo.write(leg_rb5_stand); // puts servo in stand position
          leg_lb6_servo.write(leg_lb6_stand); // puts servo in stand position
          leg_rb7_servo.write(leg_rb7_stand); // puts servo in stand position
          leg_lb8_servo.write(leg_lb8_stand); // puts servo in stand position
          hip_ra1_servo.write(hip_ra1_stand); // puts servo in stand position
          hip_la2_servo.write(hip_la2_stand); // puts servo in stand position
          hip_ra3_servo.write(hip_ra3_stand); // puts servo in stand position
          hip_la4_servo.write(hip_la4_stand); // puts servo in stand position
          hip_rb5_servo.write(hip_rb5_stand); // puts servo in stand position
          hip_lb6_servo.write(hip_lb6_stand); // puts servo in stand position
          hip_rb7_servo.write(hip_rb7_stand); // puts servo in stand position
          hip_lb8_servo.write(hip_lb8_stand); // puts servo in stand position
          standing_state=1;
          x=x+1;
 }
  };
}

Instead of:

knee_ra1_servo.write(knee_ra1_stand); // puts servo in stand position
          knee_la2_servo.write(knee_la2_stand); 
          knee_ra3_servo.write(knee_ra3_stand);
          knee_la4_servo.write(knee_la4_stand); 
          knee_rb5_servo.write(knee_rb5_stand); 
          knee_lb6_servo.write(knee_lb6_stand);
          knee_rb7_servo.write(knee_rb7_stand); 
          knee_lb8_servo.write(knee_lb8_stand);

why not:

const int nKnees = 9;
Servo kneeServo [nKnees];
const int kneeStand [nKnees] = {90, 90, 90 //whatever;
//...
//
for (int i = 0; i < nKnees; ++i) {
  kneeServo [i].write (kneeStand [i]); 
}
Thank you for the help on that. So, in getting back to the post questing. Is:

[code]

Servos[servo_number].attach( servo_pin_start +servo_number);[/code]

the attaching to an object called Servos?

Servos[servo_number].attach( servo_pin_start +servo_number);

"Servos" is an array of Servo objects.
"servo_number" is the index into that array.