Beginner Series Lesson 12: Putting It All Together
In the final lesson of the beginner series we’ll take a look back at everything we’ve learned and figure out how to put it all together

146784347479767

So far we’ve learned about basic flight commands, variables, digital IR sensors, if-statements, controller programming, analog inputs, and event flags.  Let’s take a look at what we can do by combining our lessons.

Making Games

A fun thing you can do with the CoDrone is make mini-games.  These are fun ways to challenge your programming skills and play with your friends.

Tug of War

The first part of making your own game is to create your premise.  Think about what your goals for the game are.

For this game, two people will compete on one controller.  Each player will move one joystick back and forth.  Depending on how fast you wiggle the joystick, the CoDrone will either move left or right.  The first player to get the CoDrone to fly all the way to their side wins!

Lets break this down into what we need

  • A starting mechanism
  • Joystick inputs
  • Joystick back-and-forth detection
  • CoDrone resulting movement

 

Starting

To let the player know when they can start, you should always  include buzzers at the end of your setup.

 

Joystick Inputs

Since we’re using joystick inputs, we know we’re going to need the lines

We name them LeftH, LeftV, RightH, RightV to stand for “left stick, horizontal movement (left to right)”, “left stick, vertical movement (up to down)”

We don’t directly use

because we don’t want to control the YAW of the drone with our controller. We just want to use the joystick inputs as part of our game.

 

 

Starting Mechanism

You should also include a starting mechanism that the players have to activate in order to start the game.

Since each player controls a joystick in this game, we will have the starting mechanism involve both joysticks and two sensors

So to start the game, each player moves their joystick to the side and holds the sensor closest to them (
bt1  &
bt8 )

Once the starting mechanism is activated the buzzers count down, the CoDrone takes off and
StartFlag = 1 .

The rest of the game code can now be initiated with the condition
StartFlag = 1 .

 

Back-And-Forth Detection

This is really the main part of our game.   For tug-of-war, we want to be able to see which joystick is being wiggled the fastest.  By wiggle, we mean that the player must move the joystick all the way up, then all the way down.

This code ensures that the joystick must go back and forth.  You can’t just hold the stick down.

Each time an up-to-down combination is completed,
RightCounter  or
LeftCounter  is incremented by 1

Each time the counters are greater than or equal to 3, the
RollCount  will be either
10  (for the left joystick) or
+10  (for the right joystick).

So the players are competing to get
RollCount  in the negatives (for the left joystick) or the positives (for the right).

 

CoDrone Resulting Movement

Now we just translate
RollCount  to our CoDrone’s movement and we’re pretty much done

This limits the max/min rolls to 40 and -40.  This is to prevent the drone from flying full speed into a wall if someone is really winning.

 

Final Code

 

Player 1 will move the left joystick up and down as fast as possible, and player 2 will move the right joystick.   The CoDrone will move to the left or the right, depending on who is moving their joystick up and down faster.

Try it out and see if you can improve it or add your own features.

 

Battle

Another cool thing you can program is a CoDrone battle code.  This will allow you to play drone laser tag with another CoDrone.

We start with our regular setup code

And add a new function

CoDrone.BattleBegin()

CoDrone.BattleBegin(Team Select)

Team Select – The name of the team your CoDrone will be on.

Teams – TEAM_BLUE, TEAM_RED, TEAM_GREEN, TEAM_YELLOW, FREE_PLAY

Players on the same team will not take damage from each other.

Players on FREE_PLAY will take damage from all teams.

 

This command will initiate battle mode, allowing us to keep track of our “Hit Points” by the LED indicators on our controller.  You begin the battle with 8 hit points, represented by 4 lights.  The light is bright when it represents 2 hit points, and dim when it represents 1.

Once we have our team selected, we move onto
void loop() .

Here, we put our normal controller flight code since we want to be controlling our CoDrone manually.  Make sure to include a kill switch.

We can now control our CoDrone manually and have our CoDrone on a team.  The last two things we’re missing are:

  1. Shoot
  2. Receive

The shoot command will tell the CoDrone to shoot out an IR laser, and the receive command will tell it to check if it’s been shot.

To program the shoot command, we use the function

Whenever this function is read, the CoDrone shoots out a burst of IR light from its front.

We also use

This makes it so that you can’t just hold
bt8  down to rapid fire your laser.  You will need to keep tapping it.  You should always include this line to make the game fair.  Don’t cheat!

 

To receive IR signals, we use

This will tell your CoDrone to check if it has been hit.  We put this in loop so that we can constantly check if we’ve been shot.

Your final battle code should look like this:

Activity: I Want To Play A Game

Create your own CoDrone mini game!

Try to use everything you’ve learned so far, and be creative.  Remember to always have a kill-switch.