user76704: I wanna write a program for rc-plane navigation
The plane must navigate to 2 waypoints waypoint_1 & waypoint_2
When it reaches to waypoint_1 (with miss-distance =< 5) then navigate to waypoint_2
But i have a problem.
First look at this :
void loop(){

    if (distance-to-waypoint_1 > 5 meters) {
        Navigate to waypoint_1
    if distace-to-waypoint_1 < 5 meters :
        Navigate-to-waypoint_2

If i use a code like this, plane will navigate to waypoint_1 and when it reaches somewhere that has  less than 5 meters distance to waypoint_1, then will navigate to waypoint_2, but if plane fly away and the distance between it's location and waypoint_1 goes greater than 5 meters, plane return to previous navigation and will navigate to waypoint_1 again and ignore navigation to waypoint_2
So, i need a form of programme to first do a loop for navigation to navigate the plane to waypoint_1 and when it reaches the waypoint_1 , do another loop to navigate the plane to waypoint_2
Thanks

user42015: I think you should use a different logic by using a variable to hold the current target waypoint and separate the code to reach a waypoint and to change it.
Something like this (in pseudocode):
declare variable target_waypoint and set it to waypoint1
loop
    Fly towards target waypoint
    if distance to target waypoint lower than 5
        change target_waypoint to waypoint 2

You can easily extent that for more waypoints by using an array of waypoints and a variable holding the index of the next waypoint:
declare waypoints as array
declare index variable, initialize with zero
set target_waypoint to waypoints[index]
loop
    Fly towards target waypoint
    if distance to target waypoint lower than 5
        increment index
        if index greater than max index
            Either set to zero to loop through all waypoints again
            or do whatever you want to do after the waypoints
        set target_waypoint to waypoints[index]

The code that moves you to the waypoint doesn't care about what waypoint is next. It's only doing this one job. And when you reached a waypoint you simply move the target of that code to a different position.
Much like you yourself would walk between different waypoints. Setting first waypoint as target, walking towards it until you reached it, setting the next waypoint as target and doing the walking again.

76704: <p>I wanna write a program for rc-plane navigation</p>
<p>The plane must navigate to 2 waypoints waypoint_1 &amp; waypoint_2
When it reaches to waypoint_1 (with miss-distance =&lt; 5) then navigate to waypoint_2</p>
<p>But i have a problem.</p>
<p>First look at this :</p>
<pre><code>void loop(){

    if (distance-to-waypoint_1 &gt; 5 meters) {
        Navigate to waypoint_1
    if distace-to-waypoint_1 &lt; 5 meters :
        Navigate-to-waypoint_2
</code></pre>
<p>If i use a code like this, plane will navigate to waypoint_1 and when it reaches somewhere that has  less than 5 meters distance to waypoint_1, then will navigate to waypoint_2, but if plane fly away and the distance between it's location and waypoint_1 goes greater than 5 meters, plane return to previous navigation and will navigate to waypoint_1 again and ignore navigation to waypoint_2</p>
<p>So, i need a form of programme to first do a loop for navigation to navigate the plane to waypoint_1 and when it reaches the waypoint_1 , do another loop to navigate the plane to waypoint_2</p>
<p>Thanks</p>
 42015: <p>I think you should use a different logic by using a variable to hold the current target waypoint and separate the code to reach a waypoint and to change it.</p>
<p>Something like this (in pseudocode):</p>
<pre><code>declare variable target_waypoint and set it to waypoint1
loop
    Fly towards target waypoint
    if distance to target waypoint lower than 5
        change target_waypoint to waypoint 2
</code></pre>
<p>You can easily extent that for more waypoints by using an array of waypoints and a variable holding the index of the next waypoint:</p>
<pre><code>declare waypoints as array
declare index variable, initialize with zero
set target_waypoint to waypoints[index]
loop
    Fly towards target waypoint
    if distance to target waypoint lower than 5
        increment index
        if index greater than max index
            Either set to zero to loop through all waypoints again
            or do whatever you want to do after the waypoints
        set target_waypoint to waypoints[index]
</code></pre>
<p>The code that moves you to the waypoint doesn't care about what waypoint is next. It's only doing this one job. And when you reached a waypoint you simply move the target of that code to a different position.</p>
<p>Much like you yourself would walk between different waypoints. Setting first waypoint as target, walking towards it until you reached it, setting the next waypoint as target and doing the walking again.</p>
