Battery Checker

Now we will start learning how to receive information from the CoDrone. We will start by checking the battery level and have it set the eye color based off of the battery percentage. To do so we will start by creating a variable named Battery and setting it equal to the battery percentage. To get the battery percentage we will use the “Request” block from the CoDrone tab. There are many different options that can be used for it. All of the ones that start with STATE will give you information about the setup of the CoDrone. The Attitudes are the gyros, they will tell you the angle of the CoDrone. The trims will tell you what the trim value is. The motors will tell you how fast that motor is spinning. And most of the others are various sensors. Using the drop down menu we can select BATTERY PERCENT for our project.

Then the request will give back a number according to what was requested. We will save the value of the the request into a variable so that we can use it again without having to re-request the information.

Then we can use several if statements to see what percentage the battery is. I will divide it into four different percentage categories, less than 25%, 25%-50%, 50%-75%, and more than 75% achat viagra avec paypal. You can pick your own categories and colors if you wish, these are just a suggestion.

One other thing to note, is that we need to wait a little after we have requested the information before we can send anything to the CoDrone, so I will add in a wait 1 sec block, you should be fine with 0.25 seconds, but to be safe I will use 1 second.
I will start with the lowest level first, and have the LEDs turn red for a low battery.

Then we can add in the next level. To do so we will add in another if else statement inside of the else of this first if else.

At first you might think that we will need to have it check that the battery is greater than 25% before we set it to yellow, since 10% is still less than 50, so wouldn’t it set the LEDs to yellow? However since it is inside of the else of the first if, it will only happen if the if is false, which would be when the battery is not less than 25%, so we already know that the battery is greater than 25%, and now we are checking if it is also less than 50%.

We can now add in the next if else statement. I will use LIME since it is between green (the last color I am going to use and yellow), but it is a little hard to tell it from green, so I set the brightness to 10. Then for the last set (greater than 75%) we do not need another if statement, we can just use the last else, since the only way for it to get to there is if the battery is greater than 75%. If it is we will set the eyes to green.

Try it Out

 

Challenge: Add this code to your flight code, but change this program so that it will constantly be running and updating the LED colors. Also have it automatically land once the battery is below 15%.

Hint: To make it easier to test your program you can remove the request and just set Battery to what percentage you want to test at.