How reduce code in arduino

Hi everyone!

I am programing in arduino a control of a robotic arm with 4 motors, and I have the same code of movements for each motor, each one with his number variable.

For example, this part of the code corresponds to the 2 motor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    //ID2
  if (positionn2[l][i] < initial2)
  {
   while (positionn2[l][i] < initial2)
   {
    before_position_2 = initial2;
    dxlSetGoalPosition(SERVO_ID[1], initial2);
    initial2--;
    delayMicroseconds(400);

     if (initial2 > before_position_2 || initial2 < before_position_2)
     {
      if (initial2 == position2 + MX_MAX_POSITION_VALUE)
      {
        position2 = initial2;
        turns2++;
        Serial.print("Vueltas 2: ");
        Serial.println(turns2);
      }
      else if (initial2 == position2 - MX_MAX_POSITION_VALUE)
      {
        position2 = initial2;
        turns2--;
        Serial.print("Vueltas 2: ");
        Serial.println(turns2);
      }
    }
    else if (initial2 == before_position_2)
    {
      turns2 = turns2;
    }
   }
  }
  else if (positionn2[l][i] > initial2)
  {
   while (positionn2[l][i] > initial2)
   {
    before_position_2 = initial2;
    dxlSetGoalPosition(SERVO_ID[1], initial2);
    initial2++;
    delayMicroseconds(400);

    if (initial2 > before_position_2 || initial2 < before_position_2)
    {
      if (initial2 == position2 + MX_MAX_POSITION_VALUE)
      {
        position2 = initial2;
        turns2++;
        Serial.print("Vueltas 2: ");
        Serial.println(turns2);
      }
      else if (initial2 == position2 - MX_MAX_POSITION_VALUE)
      {
        position2 = initial2;
        turns2--;
        Serial.print("Vueltas 2: ");
        Serial.println(turns2);
      }
    }
    else if (initial2 == before_position_2)
    {
      turns2 = turns2;
    }
   }
  }


it is posible to reduce the code? that is, is posible instead of put the same code to each motor (changing the numbers) to put all together in one code "part" in order to clarify and become easier to read the program?

Thank you!

Yes, it is usually always possible to factor out similar code into a function. Factoring out similar functionality helps see the different logical parts of the code, and can further help you reason about what's going on or simplify the logic even more.

Looking at the different sections side-to-side can help see similarities.
Your main differences is less-than comparisons and decrements in one branch, and greater-than comparisons and increments in the other branch, with everything else being the same.

It's possible there's an even simpler solution, but one way to simplify this could be to define a delta value, either positive or negative, that can invert the operations, and then factor the common functionality into a function.

For completeness, you must pass every needed variable as a parameter into a common function.
This might not be necessary if you factor out variables that belong together into a struct/class.

________________________________________

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void common_function(int delta, int& initial2, int& turns2, int& position2,
                     ArrayType positionn2, int l, int i, {fill in what I forgot})
{
    before_position_2 = initial2;
    dxlSetGoalPosition(SERVO_ID[1], initial2);
    initial2 += delta;
    delayMicroseconds(400);

    if (initial2 > before_position_2 || initial2 < before_position_2)
    {
        if (initial2 == position2 + MX_MAX_POSITION_VALUE)
        {
            position2 = initial2;
            turns2++;
            Serial.print("Vueltas 2: ");
            Serial.println(turns2);
        }
        else if (initial2 == position2 - MX_MAX_POSITION_VALUE)
        {
            position2 = initial2;
            turns2--;
            Serial.print("Vueltas 2: ");
            Serial.println(turns2);
        }
    }
}

if (positionn2[l][i] < initial2)
{
    int delta = 1.0;
    while (positionn2[l][i] < initial2)
    {
        common_function(delta, initial2, turns2, position2, positionn2, int l, int i, ...);
    }
}
else if (positionn2[l][i] > initial2)
{
    int delta = -1.0;
    while (positionn2[l][i] > initial2)
    {
        common_function(delta, initial2, turns2, position2, positionn2, int l, int i, ...);
    }
}

________________________________________

However, there is more you can do beyond this.

if (initial2 > before_position_2 || initial2 < before_position_2)
What is the point of this complication? This can be simplified by just saying
if (initial2 != before_position_2)

Note,
1
2
3
4
    else if (initial2 == before_position_2)
    {
      turns2 = turns2;
    }

Is this a typo? This section of code doesn't change anything.

Last note,
if (initial2 == position2 + MX_MAX_POSITION_VALUE)
I hope those are integers you're comparing. If those are floating-point numbers, == is not an accurate way to compare them.

Edit: Last last note,
If you are in C and not C++, you'll need to pass the values you want to change within the function by pointer and not by value. Or use a struct and pass that by pointer.
Last edited on
Thank you very much for your help Ganado!!!
I'm going to try your advice now to see how it goes!

In the previous post I think I explained badly, I'm sorry but I still do not have a correct level of English.

What I want is to know if I can put the code below (the movements of the 4 engines) in a single function. I've been doing different tests these weeks and I'm not capable.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
 

 //ID1
  if (positionn1[l][i] < initial1)
  {
   while (positionn1[l][i] < initial1)
   {
    before_position_1 = initial1;
    dxlSetGoalPosition(SERVO_ID[0], initial1);
    initial1--;
    delayMicroseconds(400);

    if (initial1 > before_position_1 || initial1 < before_position_1)
    {
      if (initial1 == position1 + MX_MAX_POSITION_VALUE)
      {
        position1 = initial1;
        turns++;
        Serial.print("Turns: ");
        Serial.println(turns);
      }
      else if (initial1 == position1 - MX_MAX_POSITION_VALUE)
      {
        position1 = initial1;
        turns--;
        Serial.print("Turns: ");
        Serial.println(turns);
      }
    }
    else if (initial1 == before_position_1)
    {
      turns = turns;
    }
   }
  }
  else if (positionn1[l][i] > initial1)
  {
   while (positionn1[l][i] > initial1)
   {
    before_position_1 = initial1;
    dxlSetGoalPosition(SERVO_ID[0], initial1);
    initial1++;
    delayMicroseconds(400);
    if (initial1 > before_position_1 || initial1 < before_position_1)
    {
      if (initial1 == position1 + MX_MAX_POSITION_VALUE)
      {
        position1 = initial1;
        turns++;
        Serial.print("Turns: ");
        Serial.println(turns);
      }
      else if (initial1 == position1 - MX_MAX_POSITION_VALUE)
      {
        position1 = initial1;
        turns--;
        Serial.print("Turns: ");
        Serial.println(turns);
      }
    }
    else if (initial1 == before_position_1)
    {
      turns = turns;
    }
   }
  }

  //ID2
  if (positionn2[l][i] < initial2)
  {
   while (positionn2[l][i] < initial2)
   {
    before_position_2 = initial2;
    dxlSetGoalPosition(SERVO_ID[1], initial2);
    initial2--;
    delayMicroseconds(400);

     if (initial2 > before_position_2 || initial2 < before_position_2)
     {
      if (initial2 == position2 + MX_MAX_POSITION_VALUE)
      {
        position2 = initial2;
        turns2++;
        Serial.print("Vueltas 2: ");
        Serial.println(turns2);
      }
      else if (initial2 == position2 - MX_MAX_POSITION_VALUE)
      {
        position2 = initial2;
        turns2--;
        Serial.print("Vueltas 2: ");
        Serial.println(turns2);
      }
    }
    else if (initial2 == before_position_2)
    {
      turns2 = turns2;
    }
   }
  }
  else if (positionn2[l][i] > initial2)
  {
   while (positionn2[l][i] > initial2)
   {
    before_position_2 = initial2;
    dxlSetGoalPosition(SERVO_ID[1], initial2);
    initial2++;
    delayMicroseconds(400);

    if (initial2 > before_position_2 || initial2 < before_position_2)
    {
      if (initial2 == position2 + MX_MAX_POSITION_VALUE)
      {
        position2 = initial2;
        turns2++;
        Serial.print("Vueltas 2: ");
        Serial.println(turns2);
      }
      else if (initial2 == position2 - MX_MAX_POSITION_VALUE)
      {
        position2 = initial2;
        turns2--;
        Serial.print("Vueltas 2: ");
        Serial.println(turns2);
      }
    }
    else if (initial2 == before_position_2)
    {
      turns2 = turns2;
    }
   }
  }

  //ID3
  if (positionn3[l][i] < initial3)
  {
   while (positionn3[l][i] < initial3)
   {
    before_position_3 = initial3;
    dxlSetGoalPosition(SERVO_ID[2], initial3);
    initial3--;
    delayMicroseconds(400);

    if (initial3 > before_position_3 || initial3 < before_position_3)
    {
      if (initial3 == position3 + MX_MAX_POSITION_VALUE)
      {
        position3 = initial3;
        turns3++;
        Serial.print("Vueltas 3: ");
        Serial.println(turns3);
      }
      else if (initial3 == position3 - MX_MAX_POSITION_VALUE)
      {
        position3 = initial3;
        turns3--;
        Serial.print("Vueltas 3: ");
        Serial.println(turns3);
      }
    }
    else if (initial3 == before_position_3)
    {
      turns3 = turns3;
    }
   }
  }
  else if (positionn3[l][i] > initial3)
  {
   while (positionn3[l][i] > initial3)
   {
    before_position_3 = initial3;
    dxlSetGoalPosition(SERVO_ID[2], initial3);
    initial3++;
    delayMicroseconds(400);

    if (initial3 > before_position_3 || initial3 < before_position_3)
    {
      if (initial3 == position3 + MX_MAX_POSITION_VALUE)
      {
        position3 = initial3;
        turns3++;
        Serial.print("Vueltas 3: ");
        Serial.println(turns3);
      }
      else if (initial3 == position3 - MX_MAX_POSITION_VALUE)
      {
        position3 = initial3;
        turns3--;
        Serial.print("Vueltas 3: ");
        Serial.println(turns3);
      }
    }
    else if (initial3 == before_position_3)
    {
      turns3 = turns3;
    }
   }
  }

  //ID4
  if (positionn4[l][i] < initial4)
  {
   while (positionn4[l][i] < initial4)
   {
    before_position_4 = initial4;
    dxlSetGoalPosition(SERVO_ID[3], initial4);
    initial4--;
    delayMicroseconds(400);

    if (initial4 > before_position_4 || initial4 < before_position_4)
    {
      if (initial4 == position4 + MX_MAX_POSITION_VALUE)
      {
        position4 = initial4;
        turns4++;
        Serial.print("Vueltas 4: ");
        Serial.println(turns4);
      }
      else if (initial4 == position4 - MX_MAX_POSITION_VALUE)
      {
        position3 = initial4;
        turns4--;
        Serial.print("Vueltas 4: ");
        Serial.println(turns4);
      }
    }
    else if (initial4 == before_position_4)
    {
      turns4 = turns4;
    }
   }
  }
  else if (positionn4[l][i] > initial4)
  {
   while (positionn4[l][i] > initial4)
   {
    before_position_4 = initial4;
    dxlSetGoalPosition(SERVO_ID[3], initial4);
    initial4++;
    delayMicroseconds(400);

    if (initial4 > before_position_4 || initial4 < before_position_4)
    {
      if (initial4 == position4 + MX_MAX_POSITION_VALUE)
      {
        position4 = initial4;
        turns4++;
        Serial.print("Vueltas 4: ");
        Serial.println(turns4);
      }
      else if (initial4 == position4 - MX_MAX_POSITION_VALUE)
      {
        position4 = initial4;
        turns4--;
        Serial.print("Vueltas 4: ");
        Serial.println(turns4);
      }
    }
    else if (initial4 == before_position_4)
    {
      turns4 = turns4;
    }
   }
  }
 }
sure. make a function that does the identical code bit against its input parameters. Then call it 4 times with the different values.

like where you have initial1,initial2,initial3, initial 4 just make the parameter initial and pass in initial1, ... etc in each call.

looks like

void doenginething(double initial, double turns, double before_position) //may have other params
{
//redundant code
...blah
if(initial == before_position)
etc...

}

...
//in the code above replace redundant bit with...
doenginething(initial1, turns1, before_position1);
doenginething(initial2, turns2, before_position2);
/// etc 3, 4


Thank you for answering jonnin!

I´m trying to do what yo say:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

void repeat_sequence(double initial, double turns, double before_position, double positionn, double position);
    {
    repeat_sequence(initial1, turns1, before_position_1, positionn1, position1);
    repeat_sequence(initial2, turns2, before_position_2, positionn2, position2);
    repeat_sequence(initial3, turns3, before_position_3, positionn3, position3);
    repeat_sequence(initial4, turns4, before_position_4, positionn4, position4);
 
  if (positionn[l][i] < initial)
  {
   while (positionn[l][i] < initial)
   {
    before_position = initial;
    dxlSetGoalPosition(SERVO_ID[0], initial);
    initial--;
    delayMicroseconds(400);

    if (initial != before_position)
    {
      if (initial == position + MX_MAX_POSITION_VALUE)
      {
        position = initial;
        turns++;
        Serial.print("Turns: ");
        Serial.println(turns);
      }
      else if (initial == position - MX_MAX_POSITION_VALUE)
      {
        position = initial;
        turns--;
        Serial.print("Turns: ");
        Serial.println(turns);
      }
    }
   }
  }
  else if (positionn[l][i] > initial)
  {
   while (positionn[l][i] > initial)
   {
    before_position = initial;
    dxlSetGoalPosition(SERVO_ID[0], initial);
    initial++;
    delayMicroseconds(400);
    if (initial != before_position)
    {
      if (initial == position + MX_MAX_POSITION_VALUE)
      {
        position = initial;
        turns++;
        Serial.print("Turns: ");
        Serial.println(turns);
      }
      else if (initial == position - MX_MAX_POSITION_VALUE)
      {
        position = initial;
        turns--;
        Serial.print("Turns: ");
        Serial.println(turns);
      }
    }
   }
  }
 }


But the program gives some errors, I have one question, is it posible to pass Arrays into a function? Because this is the error that the program gives:

cannot convert 'int (*)[m]' to 'double' for argument '4' to 'void repeat_sequence(double, double, double, double, double)'.

Other question, do I have to declare turns, position, positionn, before_position...???

Topic archived. No new replies allowed.