The Arduino Inventor's Guide

The Arduino Inventor's Guide by Unknown

Book: The Arduino Inventor's Guide by Unknown Read Free Book Online
Authors: Unknown
Ads: Link
topLeftDiagonal()
  {
     digitalWrite (13, HIGH );
     digitalWrite (12, LOW );
     digitalWrite (11, LOW );

     digitalWrite (10, LOW );
     digitalWrite (9, HIGH );
     digitalWrite (8, LOW );

     digitalWrite (7, LOW );
     digitalWrite (6, LOW );
     digitalWrite (5, HIGH );
  }

➌ void horizontalLine()
  {
     digitalWrite (13, LOW );
     digitalWrite (12, LOW );
     digitalWrite (11, LOW );

     digitalWrite (10, HIGH );
     digitalWrite (9, HIGH );
     digitalWrite (8, HIGH );

     digitalWrite (7, LOW );
     digitalWrite (6, LOW );
     digitalWrite (5, LOW );
  }

➍ void topRightDiagonal()
  {
     digitalWrite (13, LOW );
     digitalWrite (12, LOW );
     digitalWrite (11, HIGH );

     digitalWrite (10, LOW );
     digitalWrite (9, HIGH );
     digitalWrite (8, LOW );

     digitalWrite (7, HIGH );
     digitalWrite (6, LOW );
     digitalWrite (5, LOW );
  }
    The verticalLine() function ➊ shows the first image in Figure 3-23 , the topLeftDiagonal() function ➋ shows the second image, the horizontalLine() function ➌ shows the third, and the topRightDiagonal() function ➍ shows the last. As with your previous custom function, these custom functions have the void data type, since they won’t return a value.
    Custom functions can call other custom functions, too, so let’s call the four line functions inside a single spinningLine() function. Add the following code to your sketch, after the closing bracket in the topRightDiagonal() function.
    void spinningLine( int delayTime)
{
  verticalLine();
   delay (delayTime);

  topLeftDiagonal();
   delay (delayTime);

  horizontalLine();
   delay (delayTime);

  topRightDiagonal();
   delay (delayTime);
}
    This code shows a vertical line, a diagonal line, a horizontal line, and another diagonal line, with a delay after each line. Now, all you have to do is call spinningLine() inside the loop() function.
    NOTE
    You’ll find a complete listing of this code in the resource files at
https://nostarch.com/arduinoinventor/
.
Tweak Your loop() Function
    Add a call to your custom function inside your loop() function, as in Listing 3-13 . Remember that you still need to have all of those pinMode() commands in your setup() function.
    LISTING 3-13: Completed loop() function with the new custom function call spinningLine(200) ;
    void loop ()
{
  spinningLine(200);
}
    Once you add the function and pass it a delay time parameter (the code uses 200 ), upload your sketch to your Arduino. You’ll see a rotating line on your display. With this knowledge of sequencing LEDs and custom functions, you can make your own nine-pixel animation!
GOING FURTHER
    Custom functions will be useful when you want to reuse code later or organize your code. To take this project further, try designing more elaborate animations; you could even come up with your own alphabet and use your monitor to display a secret message.
Hack
    To take this project further, start by creating more elaborate animations. As you work through the next few projects, look for ways to incorporate your monitor—for example, maybe you could use a sensor to control an animation speed or display a sensor value in some interesting ways. Download a blank design template at
https://www.nostarch.com/arduinoinventor/
.
Modify
    You’ve learned how to control a number of electronic components by using digital pins and custom functions. Try replacing your individual LEDs with different components. We suggest a
seven-segment display
, as shown in Figure 3-24 .
    FIGURE 3-24: A single seven-segment display

    Each segment is an LED that you can control. There are seven individual segments (plus the decimal points), as shown in Figure 3-25 , and by turning specific segments on and off, you can make numbers and most letters of the English language.
    FIGURE 3-25: Illustration of the

Similar Books

Greetings from Nowhere

Barbara O'Connor

With Wings I Soar

Norah Simone

Born To Die

Lisa Jackson