Intermediate Series Lesson 4: On-Board LEDs
In this lesson we will learn how to change the color and patterns of our CoDrone’s LED lights

IMG_1600

Summary

Changing the LEDs on your CoDrone without affecting the performance can be surprisingly tricky.  Here we will explore how to properly set LED colors and how to avoid command overload.

 

Initial Color Settings

The easiest way to change your LED colors is to do it in setup() .  This will change the colors once before your actual program starts.   To control the CoDrone’s on-baord LEDs, we use the function LedColor()

LedColor();

LedColor(mode, color, interval);

mode – The mode determines which LEDs to control and the style of control.

color – The color setting allows the user to select the preset color of the LED.

interval  – The interval determines the interval of the selected mode.  For example, if flicker is selected the rate of flickering will be determined by the interval. A larger number means a longer delay between blinks.  This number can range from 0 to 255.

LedColor(mode, R, G, B, interval);

mode – The mode determines which LEDs to control and the style of control.

R, G, B –  The R, G, B fields allow the user to create their own color by setting the intensity of each individual color.

R – Red.  Allows the user to set how much red light is emitted.

G – Green. Allows the user to set how much green light is emitted.

B – Blue. Allows the user to set how much blue light is emitted.

interval  – The interval determines the interval of the selected mode. If flicker is selected, the rate of flickering will be determined by the interval with a larger number meaning a longer delay between blinks.  This number can range from 0 to 255.

 

 

You can select from these preset mode and color options.

EyeNone, EyeHold, EyeMix, EyeFlicker, EyeFlickerDouble, EyeDimming

ArmNone, ArmHold, ArmMix, ArmFlicker, ArmFlickerDouble, ArmDimming, ArmFlow, ArmFlowReverse

AliceBlue, AntiqueWhite, Aqua,  Aquamarine, Azure, Beige,  Bisque, Black,

BlanchedAlmond,  Blue, BlueViolet, Brown,BurlyWood, CadetBlue, Chartreuse,

Chocolate, Coral, CornflowerBlue,  Cornsilk, Crimson, Cyan, DarkBlue, DarkCyan,

DarkGoldenRod,  DarkGray, DarkGreen, DarkKhaki,  DarkMagenta, DarkOliveGreen,

DarkOrange, DarkOrchid, DarkRed,DarkSalmon,  DarkSeaGreen, DarkSlateBlue,

Darkslategray, darkturquois to, darkviolet, deepp Link, deepskyblu to,dimgray,

DodgerBlue,FireBrick, FloralWhite, ForestGreen,  Fuchsia, Gainsboro, GhostWhite,

Gold, GoldenRod, Gray, Green,GreenYellow, HoneyDew,  HotPink, IndianRed, Indigo,

Ivory, Khaki, Lavender,  LavenderBlush, LawnGreen, LemonChiffon,LightBlue,

LightCoral, LightCyan, LightGoldenRodYellow, LightGray, LightGreen,  LightPink,

LightSalmon, LightSeaGreen,LightSkyBlue, LightSlateGray, LightSteelBlue, LightYellow,

Lime, LimeGreen, Linen, Magenta, Maroon, MediumAquaMarine,MediumBlue,

MediumOrchid, MediumPurple, MediumSeaGreen, MediumSlateBlue,MediumSpringGreen,

MediumTurquoise,MediumVioletRed,  MidnightBlue, MintCream,MistyRose, Moccasin,

NavajoWhite, Navy, OldLace, Olive, OliveDrab, Orange,OrangeRed,Orchid,PaleGoldenRod,

PaleGreen, PaleTurquoise, PaleVioletRed, PapayaWhip, PeachPuff,Peru, Pink, Plum,

PowderBlue, Purple, RebeccaPurple,Red, RosyBrown, RoyalBlue,SaddleBrown, Salmon,

SandyBrown, SeaGreen, SeaShell,Sienna, Silver, SkyBlue,SlateBlue, SlateGray,Snow,

SpringGreen, SteelBlue, Tan, Teal, Thistle, Tomato, Turquoise, Violet,Wheat, White,

WhiteSmoke, Yellow, YellowGreen

 

 

Example 1: Initial Arm LED Colors to Aqua

Using ArmHold will change the LEDs to a solid color. The interval number doesn’t matter in this case.

 

Example 2: Add Initial Eye LED Colors to Blinking Purple

EyeFlicker at a rate of 30 provides a steady strobe of Magenta light.

 

Command Spacing

You might be wondering why we include delays between LED commands

If you try uploading this command without the delay, you’ll notice that the second command  CoDrone.LedColor(EyeFlicker, Magenta, 30) doesn’t execute.

The CoDrone can only handle a set number of commands at a time.  If we send commands too quickly, some commands could either be lost or overflow into each other and cause a disconnect.  The way the CoDrone library handles this problem is to set coded restrictions on how many commands can be send to the CoDrone at a time.

By default, the CoDrone can only receive one LED command every 60 milliseconds and one Control command every 50 milliseconds.  The shortest allowed interval between any combination of commands is 10 milliseconds.

 

led_ignored

LED commands also have a special characteristic.  Once an LED color is set, that color will stay with the CoDrone until overridden by another LED command.  To be efficient with our code, we will learn how to use this to our advantage and only send LED commands when necessary.

 

Using On-Board LEDs

There are a few different methods for using On-Board LEDs.  We’ll talk about a few here

 

Mode Indicators

One way to use the CoDrone’s LEDs are are as mode indicators.  You might recall from our beginners series lesson on flags that a mode is just a variable with conditional statements attached to it.

Example 3.1: Setting Modes

Here’s a simple code that allows you to switch between Mode = 1  and Mode = 2  by using the sensor bt1

This code uses the CoDrone.Buzz() command to create a buzzer noise when the mode is switched.  This is helpful, but will only emit when switching modes.  Using LEDs, we could add a constant indicator of the current mode.

There are two ways to do this:

Example 3.2: Adding LEDs the Bad Way

This method will work, but it isn’t the proper way to do it.  The reason is because of how many times the LED commands are sending.

Since they are encompassed in the if-statements  if (Mode == 0) { and  else if (Mode == 1) { , and our Mode is always either Mode = 0  or Mode = 1 , the LED commands are constantly sending.

We really only need to send the commands once per mode change.

Example 3.3: Adding LEDs the Good Way

Now our LED commands are controlled by the if-statement   if (bt1) { , and thus only send once whenever the sensor bt1  is triggered.   This provides the same results, but much more efficiently without sending unnecessary commands.

 

Timer Based LEDs

Another cool way to use LEDs on your CoDrone is to add the LEDs on a timer.

Again, there is a good way and a bad way to do this.  For this example, we will use the Serial Monitor to visualize our commands.

Example 4.1: Timer LEDs the Bad Way

serial-arm-dimmingOutput

We clearly have the same problem as in the previous example.  Instead of sending the LED command once every second, we are constantly sending the command.

The smarter way to do this is to utilize flags

Example 4.2: Timer LEDs the Good Way

serial-arm-dimming-timer

Output

This way we only send the command exactly once per interval.  Translating this into actual CoDrone code:

 

Implementing LEDs into Flight Codes

You can directly insert any of these methods into CoDrone flight codes to make your programs more flashy.   Here’s an example using autonomous timer flight pathing with changing LED colors every second.

Example 5: Autonomous Flight with Changing LED Colors

You can see that it’s just an autonomous flight code with the LED code attached to the end.

 

 

Activity: Double Rainbow

Scene_01

 

Write a CoDrone controller flight code with additional LED commands:

  1. Make the eyes cycle through 3 different colors.  Change colors every second
  2. Make the arms switch between 2 colors every 1.2 seconds.
  3. Make sure that you can still control your CoDrone normally with your controller

Include a kill switch and make sure that you can Stop your CoDrone at any time.  Remember not to send the commands more often than you need to!