Intermediate 12: Putting it All Together
In the final lesson of the intermediate series, we’ll take a look at some example codes that utilize everything we’ve learned so far.

CoDrone and controller, outdoors

Summary

In the intermediate series, we’ve learned how to use the Serial Monitor, starting and resetting timers, controlling on-board and controller LEDs, void functions, return functions, while-loops, for-loop, arrays, arduino functions, and more.  Here, you will get the chance to follow some examples that utilize everything we’ve learned so far.  At the end of this lesson, you will be expected to create your own cool CoDrone program.

 

Extra Flow Control

The C++ language comes with a few special keywords that can help you control the flow of your programs.  We’ll discuss them here.

 

Break

The break  keyword is used to instantly exit a loop.  This works with both for-loops and while-loops.

This can especially come in handy if you are using a for-loop and need to include a kill-switch.

In this example, the break  allows you to exit the loop instantly.  Without a break, the THROTTLE command would continue to be sent until the loop was over.

 

Continue

Another special loop keyword is continue .  Instead of exiting a loop, the keyword continue  will skip the current iteration of the loop and continue with the next one.

This example will send the THROTTLE  command for the first 40 loops, then send a blank Control()  for loops 41-79, then send the THROTTLE  command again.

 

GoTo

The most powerful flow keyword we’ll learn about is goto .  This command will allow us to instantly go to any part in our program.

We mark our go to location with a label (can be named anything)

then we go to it using

goto gohere;

Here’s an example

While holding down sensor 18, the goto  will continue to jump to the label, preventing CoDrone.Buzz()  from executing.

It should be understood that using goto  is somewhat frowned upon and is rarely seen in actual programming.  This is because it is very easy to accidentally create infinite loops and undefined program flow using goto .  These issues are extremely difficult to debug in more complex codes and will completely ruin a program if not fixed.

 

 

Instant Replay

This program will record all of our CoDrone controller inputs as we’re flying and replay them at will.

An extra challenge for you is to create a new array that records your Landing and/or Stop commands as well.

 

Activity:  Arts and Crafts

Create your own advanced CoDrone code with everything we’ve learned so far.