Intermediate 11: Arduino Functions
Here we will take a look at some built-in Arduino functions.

Flying CoDrone with VR goggles

Summary

Arduino comes with some useful, built-in functions that we can take advantage of.  A few of the functions we will go over today are:

  • max(), min()
  • constrain(), map()
  • abs(), pow(), sqrt()
  • random()

We will also take a look at some extra control statements to help us better direct the flow of our programs.

 

Arduino Functions

All of the functions we will talk about today are return functions.

 

max(), min()

The function max()  takes two numbers as input and returns the larger of the two.

In this case, x = 10.

The function min()  takes two numbers as input and returns the smaller of the two.

In this case, x = 5.

These functions can be used to easily constrain joystick values.  For example, if you wanted to limit your throttle to a max of 75

This would set THROTTLE  to the lower value between 75 and the joystick input.  If the joystick ever goes above 75, the output will be 75.

 

constrain()

If you want to limit both the minimum and maximum value of a number, you can use the function constrain() .

value  is the number to be constrained

lower  is the lower bound

upper  is the upper bound

For example, if we wanted to constrain our throttle to be between 30 and 75

 

map()

The mapping function is useful for performing scale changes.

value  is the number to be remapped

fromLow  is the current lower bound of the number

fromHigh  is the current upper bound of the number

toLow  is the new lower bound of the number

toHigh  is the new upper bound of the number

The function CoDrone.AnalogScaleChange() remaps our joystick’s analog values (which range from 0 to 1023) to inputs our CoDrone understands (-100 to 100).

This is the line directly from the ScaleChange function.

 

abs(), pow(), sqrt()

These three functions perform mathematical operations.

abs(x)  will return the absolute value of x.

pow(x,y)  will return x to the power of y.

sqrt(x)  will return the square root of x.

 

random()

The function  random()  generates a random number within the given range.

lower  the lower bound of the generated number

upper  the upper bound +1 of the generated number.  The generated number will never reach this upper bound.

For example, if I wanted to generate a random number between 0 and 10

Notice that we don’t end on 10.  That’s because the upper bound for random generation is never reached.

 

 

Fun with Random

Being able to generate a random number opens up a lot of fun possibilities for the CoDrone.

 

Random Roaming

One easy example of a random CoDrone program is a random roaming program.  This code will tell the CoDrone to move in a random direction every so often.

We begin by generating a random number.  Since we’ll be working with four different directions (pitch forward, pitch backward, roll right, roll left), we want to have four possible options.

Then it’s just a matter of assigning a direction to each possible number.

So if our random number happens to be 2, our CoDrone will roll right at a speed of 40 for 500 milliseconds.

 

 

Roulette

Let’s make a game called Roulette:

  • Every 200 milliseconds, the program will cycle through one of four random numbers
  • When the right IR sensor is triggered, the current random number is selected
  • If the random number is 0, the CoDrone will pitch forward
  • If the random number is 1, the CoDrone will roll right
  • If the random number is 2, the CoDrone will pitch backward
  • If the random number is 3, the CoDrone will roll left

The goal of this game is to try to fly the CoDrone from one point to the next by quickly selecting the correct direction when it appears in the random cycle.

To start, we create a timer that will generate a random number (0-3) every 200 milliseconds

Now we have the variable Rand  that will be either 0, 1, 2, or 3.

We then set the IR sensor 18 to trigger a different maneuver based on the value of Rand .

Each of these maneuvers will occur for 1 second ( delay(1000) ).

To make it easier for our users, we will add an indicator function that gives visual and audio cues depending on the value of Rand .

Each value of Rand  produces a unique beep and controller LED combination.

Now all we need to add is the throttle control and kill switch and we’re good to go!

Play with a friend and see who can maneuver their CoDrone better.  It’s all about reaction time.

If it’s too easy or too hard, change the interval time for the Rand  variable.