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
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;
}
...
}
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: