Calling an array in a if function

Sorry if this isn't enough information, I'm a beginner in c++ coding.

So what I want to do is to set end points at which my motor should turn directions.
int xdir= 1; //initial direction-->Forward
int count =0; //Starting step //no of steps
int endpoint [5]={100,220,300,410,500};
.
. //motor program

for (count=0; count<=600; count++)
{
//finding end point
{
if (count == endpoint[1]){

//change direction
xdir = xdir*(-1);
}

So the above program will only change direction at 220 because of [1], so how do I call all the elements in the array?
Any help would be appreciated.
Thank you
Last edited on
You don't "call" an array.

Array indexing also starts from [0], not [1].

xdir = -xdir is simpler than a multiply.

Assuming that your endpoint array is sorted then you can initialise the index of that (say e=0) and every time that count==endpoint[e] then you increment e as well as changinging direction. (But make sure that once you reach the end of the endpoint[] array you don't try to read beyond it.)
Add another int to keep track of which endpoint you are at:

[code]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int xdir= 1; //initial direction-->Forward
  int count =0; //Starting step //no of steps
  int endpoints []={100,220,300,410,500,-1};
  int current_endpoint = 0;
  .
  .//motor program

  for (count=0; count<=600; count++)
  {
    //finding end point
    if (count == endpoints[current_endpoint])
    {
      //change direction
      xdir = xdir*(-1);
      current_endpoint += 1;
    }
    ...
  }
[/code]

Good luck!
Last edited on
The question is: when exactly the direction is changed.

For all entries in the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
int entry = 0; // Note
for (count=0; count<=600; count++)
{
//finding end point
{
if(entry < 5) { // Note
if (count == endpoint[entry]){ // Note
++entry; // Note

//change direction
xdir = xdir*(-1);
}
}

For odd only:
1
2
3
4
5
6
7
8
9
10
11
12
13
int entry = 1; // Note
for (count=0; count<=600; count++)
{
//finding end point
{
if(entry < 5) { // Note
if (count == endpoint[entry]){ // Note
entry += 2; // Note

//change direction
xdir = xdir*(-1);
}
}


I feel like something is missing. If the loop does 601 iterations, "steps", then the motor "vehicle" should advance 601 "steps"? Say, the "step" is "inch".
1
2
3
4
for (count=0; count<=600; count++)
{
  // move one inch
}

At some points the direction changes:
1
2
3
4
5
for (count=0; count<=600; count++)
{
  // if turning point, then turn
  // move one inch
}

Now, with {100,220,300,410,500} this surely means:
Move forward 100 inches
Move backwards 120 inches
Move forward 80 inches
Move backwards 110 inches
Move forward 90 inches
Move backwards 101 inches

Or simply move backwards 61 inches from starting position (if you only care where you end up).

Anyway, there could be a different approach:
count = 0
for num in {100,220,300,410,500,601}
    advance num-count inches
    count = num
    turn
Last edited on
Registered users can post here. Sign in or register to post.