What are multi-dimensioned array and could i apply to servo motors

as stated above what are multi-dimensioned arrays and could i apply it to a servo motor in programming. Been trying to give the servos different angle position and is it possible to give it different function within the array at different speed control? Heres my script.

#include <Servo.h>


//Define the 10 servo motors
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;
Servo servo9;
Servo servo10;



unsigned long startTime1 = millis();
unsigned long startTime2 = millis();
unsigned long startTime3 = millis();
unsigned long startTime4 = millis();
unsigned long startTime5 = millis();
unsigned long startTime6 = millis();
unsigned long startTime7 = millis();
unsigned long startTime8 = millis();
unsigned long startTime9 = millis();
unsigned long startTime10 = millis();

unsigned long MOVING_TIME1 = 42100;
unsigned long MOVING_TIME2 = 42200;
unsigned long MOVING_TIME3 = 42300; //moving time is (x) seconds
unsigned long MOVING_TIME4 = 42400;
unsigned long MOVING_TIME5 = 42500;
unsigned long MOVING_TIME6 = 42600;
unsigned long MOVING_TIME7 = 42700;
unsigned long MOVING_TIME8 = 42800; //moving time is (x) seconds
unsigned long MOVING_TIME9 = 42900;
unsigned long MOVING_TIME10 = 42000;


// Lists of positions for each servo to cycle through 
byte servo1positions[] = {180,90,90,90,90,90,90,90,90,90,90};
byte servo2positions[] = {0,0,90,90,90,90,90,90,90,90,90};
byte servo3positions[] = {0,0,0,90,90,90,90,90,90,90,90};
byte servo4positions[] = {0,0,0,0,90,90,90,90,90,90,90};
byte servo5positions[] = {0,0,0,0,0,90,90,90,90,90,90};
byte servo6positions[] = {180,180,180,180,180,180,90,90,90,90,90};
byte servo7positions[] = {180,180,180,180,180,180,180,90,90,90,90};
byte servo8positions[] = {180,180,180,180,180,180,180,180,90,90,90};
byte servo9positions[] = {180,180,180,180,180,180,180,180,180,90,90};
byte servo10positions[] = {0,0,0,0,0,0,0,0,0,0,90};

// Current index into the list of position
byte servo1index = 0;
byte servo2index = 0;
byte servo3index = 0;
byte servo4index = 0;
byte servo5index = 0;
byte servo6index = 0;
byte servo7index = 0;
byte servo8index = 0;
byte servo9index = 0;
byte servo10index = 0;



unsigned long moveStartTime;

int startPos1 = 0;
int startPos2 = 45;
int startPos3 = 90;
int startPos4 = 135;
int startPos5 = 180;

int stopPos1 = 0;
int stopPos2 = 45;
int stopPos3 = 90;
int stopPos4 = 135;
int stopPos5 = 180;


int startAngle1 = 0;
int startAngle2 = 30;
int startAngle3 = 60;
int startAngle4 = 90;
int startAngle5 = 120;
int startAngle6 = 150;
int startAngle7 = 180;

int stopAngle1 = 0;
int stopAngle2 = 30;
int stopAngle3 = 60;
int stopAngle4 = 90;
int stopAngle5 = 120;
int stopAngle6 = 150;
int stopAngle7 = 180;

void setup()
{
  // define the servo signal pins
  servo1.attach(2);
  servo2.attach(3);
  servo3.attach(4);
  servo4.attach(5);
  servo5.attach(6);
  servo6.attach(8);
  servo7.attach(9);
  servo8.attach(10);
  servo9.attach(11);
  servo10.attach(12);
  moveStartTime = millis(); // start moving
}

void loop() {
  unsigned long progress = millis() - moveStartTime;
  unsigned long currentTime = millis();

  if (progress <= MOVING_TIME1) {
    long angle = map(progress, 0, MOVING_TIME1, startAngle1, stopAngle7);
    servo1.write(angle); 
  }

   if (progress <= MOVING_TIME2) {
    long angle = map(progress, 0, MOVING_TIME2, startAngle7, stopAngle1);
    servo2.write(angle); 
  }

   if (progress <= MOVING_TIME3) {
    long angle = map(progress, 0, MOVING_TIME3, startAngle7, stopAngle1);
    servo3.write(angle); 
  }

   if (progress <= MOVING_TIME4) {
    long angle = map(progress, 0, MOVING_TIME4, startAngle7, stopAngle1);
    servo4.write(angle); 
  }

   if (progress <= MOVING_TIME5) {
    long angle = map(progress, 0, MOVING_TIME5, startAngle7, stopAngle1);
    servo5.write(angle); 
  }

  if (progress <= MOVING_TIME6) {
    long angle = map(progress, 0, MOVING_TIME6, startAngle7, stopAngle1);
    servo6.write(angle); 
  }

   if (progress <= MOVING_TIME7) {
    long angle = map(progress, 0, MOVING_TIME7, startAngle1, stopAngle7);
    servo7.write(angle); 
  }

   if (progress <= MOVING_TIME8) {
    long angle = map(progress, 0, MOVING_TIME8, startAngle1, stopAngle7);
    servo8.write(angle); 
  }

   if (progress <= MOVING_TIME9) {
    long angle = map(progress, 0, MOVING_TIME9, startAngle1, stopAngle7);
    servo9.write(angle); 
  }

   if (progress <= MOVING_TIME10) {
    long angle = map(progress, 0, MOVING_TIME10, startAngle1, stopAngle7);
    servo10.write(angle); 
  }




 


  if (currentTime - startTime1 >= 1000)
  {
    startTime1 = currentTime;
    servo1index = (servo1index + 1) % sizeof servo1positions;
    servo1.write(servo1positions[servo1index]);
  }

  if (currentTime - startTime2 >= 2000)
  {
    startTime2 = currentTime;
    servo2index = (servo2index + 1) % sizeof servo2positions;
    servo2.write(servo2positions[servo2index]);
  }

  if (currentTime - startTime3 >= 3000)
  {
    startTime3 = currentTime;
    servo3index = (servo3index + 1) % sizeof servo3positions;
    servo3.write(servo3positions[servo3index]);
  }

  if (currentTime - startTime4 >= 4000)
  {
    startTime4 = currentTime;
    servo4index = (servo4index + 1) % sizeof servo4positions;
    servo4.write(servo4positions[servo4index]);
  }

  if (currentTime - startTime5 >= 5000)
  {
    startTime5 = currentTime;
    servo5index = (servo5index + 1) % sizeof servo5positions;
    servo5.write(servo5positions[servo5index]);
  }

if (currentTime - startTime6 >= 6000)
  {
    startTime6 = currentTime;
    servo6index = (servo6index + 1) % sizeof servo6positions;
    servo6.write(servo6positions[servo6index]);
  }

  if (currentTime - startTime7 >= 7000)
  {
    startTime7 = currentTime;
    servo7index = (servo7index + 1) % sizeof servo7positions;
    servo7.write(servo7positions[servo7index]);
  }

  if (currentTime - startTime8 >= 8000)
  {
    startTime8 = currentTime;
    servo8index = (servo8index + 1) % sizeof servo8positions;
    servo8.write(servo8positions[servo8index]);
  }

  if (currentTime - startTime9 >= 9000)
  {
    startTime9 = currentTime;
    servo9index = (servo9index + 1) % sizeof servo9positions;
    servo9.write(servo9positions[servo9index]);
  }

  if (currentTime - startTime10 >= 10000)
  {
    startTime10 = currentTime;
    servo10index = (servo10index + 1) % sizeof servo10positions;
    servo10.write(servo10positions[servo10index]);

    
  }





  
}
1 Like

Words in in internet search thingy "c++ multi dimensional arrays" makes results that you can select and read.

Gets things like:

Can you use them will bepend on your abilities.

I think that you need a 1-dimensional array (or just "array") for the servo motors.

Servo myServos[10];

The servo positions can be put together.
Are those 11 positions for 10 servo motors ? A multidimensional array starts with the most global one (10 servo motors) and ends with the details (11 positions).

byte servoPositions[10][11] = 
{
  {180,90,90,90,90,90,90,90,90,90,90},
  {  0, 0,90,90,90,90,90,90,90,90,90},
  ...
};

Why stop there ? You make arrays of the start times, the index, the start and stop angle, and so on.
The best solution is to create an array of 'struct' or even a 'class' that has everything in it. Then all the information of a servo motor will be together.

To control servo motors independent of each other, the position of all the servo motors are updated every 10ms or 20 ms. Here is an example, click on the start button to start the simulation: ServoShowcase.ino - Wokwi Arduino and ESP32 Simulator

1 Like

i think you're looking for more than simply a multidimensional array

consider

#define MyHW
#ifdef MyHW
struct Servo {
    byte pin;
    Servo (void) { }
    void attach (int p)  { pin = p; }
    void write (int pos)  {
        Serial.print ("write: ");
        Serial.print (pin);
        Serial.print (" ");
        Serial.println (pos);
    }
};

#else
# include <Servo.h>
#endif

// -----------------------------------------------------------------------------
Servo servo1;
Servo servo2;

struct MyServo {
    Servo           servo;
    byte            pin;
    unsigned long   msecPeriod;
#define MAX_POSITIONS   10
    int             positions [MAX_POSITIONS];

    int             idx;
    int             pos;
    unsigned long   msecLst;
};


MyServo servo [] = {
    { servo1, 2, 400, { 10, 40, 80, 120, 170, -1 }},
    { servo2, 3, 600, { 20, 100, -1 }},
};
const int N_Servo = sizeof(servo) / sizeof(MyServo);

char str [80];

// -----------------------------------------------------------------------------
void loop (void)
{
    unsigned long msec = millis ();

    MyServo *s = servo;
    for (unsigned n = 0; n < N_Servo; n++, s++)  {
        if (msec - s->msecLst > s->msecPeriod)  {
            s->msecLst = msec;
            s->servo.write (s->positions [s->idx++]);
            if (-1 == s->positions [s->idx])
                s->idx = 0;
        }
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (9600);

    MyServo *s = servo;
    for (unsigned n = 0; n < N_Servo; n++)  {
        s->servo.attach (s->pin);
        s->servo.write (s->positions [s->idx++]);
    }
}
3 Likes

what is a 'struct' or a 'class' havent heard either of them and what would be a good example for servo motors.

whoa..... it'll take a bit for me to understand that lol still somewhat new..

Words like "C++ structure" and "c++ class" in a internet search engine can answer those questions.

a struct/class is a user defined variable that is usually composed other primitive variables

rather than have multiple separate arrays where the nth element of each array are related to one another, a struct relates all the elements together and there can be a single array of structs

the different elements of a struct, s, would be accessed using the dot(?) nomenclature (e.g. s.time, s.index), and in an array, s[n].time. but pointers can also be used, s->time

the idea is to make what you're doing more intuitive and customize the code for the problem

1 Like

yes its 11 position for 10 servo motors

Is there a way I could get out of the function stop for a min and create and add a new function

I’ve think it’s called a while loop? I’m new to it and I’ve been trying to return the function right from the beginning and add a different function to it

You can see How to use an array of servo motor. That link shows an example for one dimension array of servo motor, but you can modify it to two dimension array

It’s completely off the topic but let’s say if I want to run the arrays of all 10 servos 11 positions it’s running would I “let’s say run it 3 times “ in cycle and break out of that loop and run a different program. Is that possible.

I’d guess the question I’ve might need to look into it is I want to be able to run 10 servos at the same time each of them running within those times individually different interations (x) amount of times as seen above but then exit that array function and enter another array .Could I have multiple arrays and do that instead of guess someone said I would need to have a struc……

Before going any deeper into the software, how are you intending to power the servos ?

Can you describe what the servo motors should do ?
Can you also tell about your project, give us a broader view.

A 'struct' or 'class' is something for later. You can start by putting the servo motors in a array. Keep your old sketch as it is, and make a new sketch and slowly migrate to arrays.

To exit an array and enter an array is not how look at arrays. Do you mean an array with a sequence for the servo motors ?

this sounds similar to controlling a robotic quadruped (e.g. dog). the servos would all have to be driven in sequence, to different positions, some forward, some back.

there could be a table of commands, each set of command executed periodically (e.g. 20 msec). there could be multiple tables describing sequences of commands for the robot to move forward, left, right, sit ...

each command could specify a servo and position or be a delay to the next command as well as an end. at the end of a cmd, it could be repeated or a different command sequence executed

the problem is determining the coordinated set of servo positions for each movement

2 Likes

Yeah I’m hearing towards that in a another project aside from the robotic hand just getting some basic understanding of the servos in general then work on getting the program to have all the servos motor under a set command. Controlling speed within a millis function and have it do different postures. Same with the robotic hand

1 Like

My biggest struggle isn’t the idea but getting into code lol :joy:

1 Like

many optimize the code, which may save a few lines. optimizing the architecture can say 100xs more

i favor data driven, rather than logic approaches. the same logic can be applied to many data. but you need to understand how to describe what you're doing.

"insect" rather than artificial intelligence