Book Image

Arduino Essentials

Book Image

Arduino Essentials

Overview of this book

Table of Contents (17 chapters)
Arduino Essentials
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Analog control through code


The most valuable part of this example is not its physical part but its logical one, and I'll show you the associated functions to deal with analog outputs and a very important control structure in any programming language called a for loop in the code we will be using for this circuit.

The analogWrite() function

Dealing with analog outputs from the programming side is just a matter of using the analogWrite() function, which, as its name implies, writes an analog value through a pin.

It takes two parameters, similar to the digitalWrite() function:

  • The first one is the pin on which we want to operate (remember that it has to be one marked as PWM) on

  • The second parameter is the value we want to output, and that, as I told you previously, can take any value in the range of 0 to 255, 0 meaning 0V and 255 meaning 5V

The for loop

The other important concept we will come across in this example is a for loop, which is a very important control structure present in almost every...