ProgrammingArduinoGettingStartedWithSketches_djvu.txt

(208 KB) Pobierz
TA3 



Programming 

Arduino" 

Getting Started 
with Sketches 






Simon Monk 




Programming Arduino 


TM 


Getting Started with Sketches 


Simon Monk 


Me 

Graw 

Hill 


New York Chicago San Francisco Lisbon 
London Madrid Mexico City Milan New Delhi 
San Juan Seoul Singapore Sydney Toronto 



The McGraw-Hill Companies 


Copyright © 2012 by The McGraw-Hill Companies. All rights reserved. Except as permitted under 
the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed 
in any form or by any means, or stored in a database or retrieval system, without the prior written 
permission of the publisher. 

ISBN: 978-0-07-178423-8 

MHID: 0-07-178423-3 

The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-178422-1, 
MHID: 0-07-178422-5. 

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after 
every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit 
of the trademark owner, with no intention of infringement of the trademark. Where such designations 
appear in this book, they have been printed with initial caps. 

McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales 
promotions, or for use in corporate training programs. To contact a representative please e-mail us at 
bulksales@mcgraw-hill.com. 

All trademarks or copyrights mentioned herein are the possession of their respec-tive owners and 
McGraw-Hill makes no claim of ownership by the mention of products that contain these marks. 

“Arduino†is a trademark of the Arduino team. 

Information has been obtained by McGraw-Hill from sources believed to be reliable. However, 
because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, 
McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is 
not responsible for any errors or omissions or the results obtained from the use of such information. 

TERMS OF USE 

This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hillâ€) and its 
licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as 
permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, 
you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works 
based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it 
without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and 
personal use; any other use of the work is strictly prohibited. Your right to use the work may be 
terminated if you fail to comply with these terms. 


THE WORK IS PROVIDED “AS IS.†McGRAW-HILL AND ITS LICENSORS MAKE NO 
GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR 
COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, 


INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA 
HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its 
licensors do not warrant or guarantee that the functions contained in the work will meet your 
requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its 
licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of 
cause, in the work or for any damages resulting therefrom McGraw-Hill has no responsibility for the 
content of any information accessed through the work. Under no circumstances shall McGraw-Hill 
and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar 
damages that result from the use of or inability to use the work, even if any of them has been advised 
of the possibility of such damages. This limitation of liability shall apply to any claim or cause 
whatsoever whether such claim or cause arises in contract, tort or otherwise. 



To my boys, Stephen and Matthew, 
from a very proud Dad. 



About the Author 


Simon Monk has a bachelor’s degree in cybernetics and computer science and a doctorate in 
software engineering. He has been an active electronics hobbyist since his school days and is an 
occasional author in hobby electronics magazines. He is also author of 30 Arduino Projects for the 
Evil Genius and 15 Dangerously Mad Projects for the Evil Genius. 



CONTENTS 


Acknowledgments 

Introduction 

1 This Is Arduino 

Microcontrollers 

Development Boards 

A Tour of an Arduino Board 
Power Supply 
Power Connections 
Analog Inputs 
Digital Connections 
Microcontroller 
Other Components 

The Origins of Arduino 

The Arduino Family 

Uno. Duemilanove, and Diecimila 

Mega 

Nano 

Bluetooth 

Lilvpad 

Other “Official†Boards 


Arduino Clones and Variants 


Conclusion 
2 Getting Started 
Powering Up 
Installing the Software 
Uploading Your First Sketch 
The Arduino Application 
Conclusion 


3 C Language Basics 
Programming 

What Is a Programming Language? 

Blink — Again! 

Variables 

Experiments in C 

Numeric Variables and Arithmetic 

Commands 

if 

for 

while 

The #define Directive 
Conclusion 

4 Functions 

What Is a Function? 

Parameters 


Global. Focal, and Static Variables 
Return Values 


Other Variable Types 
floats 
boolean 

Other Data Types 

Coding Style 
Indentation 
Opening Braces 
Whitespace 
Comments 


Conclusion 
5 Arrays and Strings 


Arrays 

Morse Code SOS Using Arrays 

String Arrays 

String Literals 
String Variables 

A Morse Code Translator 
Data 

Globals and Setup 
The loop function 
Th e flashSequence Function 
The fl ashPotOr Pash Function 
Putting It All Together 

Conclusion 

6 Input and Output 

Digital Outputs 

Digital Inputs 

Pull-up Resistors 
Internal Pull-up Resistors 
Debouncing 

Analog Outputs 

Analog Input 

Conclusion 

7 The Standard Arduino Library 
Random Numbers 


Math Functions 


Bit Manipulation 

Advanced I/O 

Generating Tones 
Feeding Shift Registers 

Interrupts 

Conclusion 


8 Data Storage 
Constants 

The PROGMEM Directive 
EEPROM 

Storing an int in EEPROM 
Storing a float in EEPROM (Unions) 
Storing a String in EEPROM 
Clearing the Contents of EEPROM 

Compression 

Range Compression 

Conclusion 

9 LCD Displays 

A USB Message Board 
Using the Display 
Other LCD Library Functions 
Conclusion 

10 Arduino Ethernet Programming 
Ethernet Shields 

Communicating with Web Servers 
HTTP 
HTML 

Arduino as a Web Server 

Setting Arduino Pins over the Network 

Conclusion 

11 C++ and Libraries 

Object Orientation 

Classes and Methods 

Built-in Library Example 


Writing Libraries 
The Header File 
The Implementation File 
Completing Your Library 

Conclusion 


Index 


ACKNOWLEDGMENTS 


I thank Linda for giving me the time, space, and support to write this book and for putting up with the 
various messes my projects create around the house. 

I also thank Stephen and Matthew Monk for taking an interest in what their Dad is up to and their 
general assistance with project work. 

Finally, I would like to thank Roger Stewart, Sapna Rastogi, and everyone involved in the 
production of this book. It’s a pleasure to work with such a great team 



INTRODUCTION 


Arduino interface boards provide a low-cost, easy-to-use technology to create microcontroller- 
based projects. With a little electronics, you can make your Arduino do all sorts of things, from 
controlling lights in an art installation to managing the power on a solar energy system. 

There are many project-based books that show you how to connect things to your Arduino, 
including 30 Arduino Projects for the Evil Genius by this author. However, the focus of this book is 
on programming the Arduino. 

This book will explain how to make programming the Arduino simple and enjoyable, avoiding the 
difficulties of uncooperative code that so often afflict a project. You will be taken through the process 
of programming the Arduino step by step, starting with the basics of the C programming language that 
Arduinos use. 


So, What Is Arduino? 


Arduino is a small microcontroller board with a universal serial bus (USB) plug to connect to your 
computer and a number of connection sockets that can be wired to external electronics such as 
motors, relays, light sensors, laser diodes, loudspeakers, microphones, and more. They can either be 
powered through the USB connection from the computer, from a 9V battery, or from a power supply. 
They can be controlled from the computer or programmed by the computer and then disconnected and 
allowed to work independently. 

The board design is open source. This means that anyone is allowed to make Arduino-compatible 
boards. This competition has lead to low costs for the boards. 

The basic boards are supplemented by accessory shield boards that can be plugged on top of the 
Arduino board. In this book, we will use two shields — an LCD display shield and an Ethernet shield 
— that will allow us to turn our Arduino into a tiny web server. 

The software for programming your Arduino is easy to use and also freely available for Windows, 
Mac, and LINUX computers. 

What Will I Need? 


This is a book intended for beginners, but it is also intended to be useful to those who have used 
Arduino for a while and want to learn more about programming the Arduino or gain a better 
understanding of the fundamentals. 

You do not need to have any programming experience or a technical background, and the book’s 
exercises do not require any soldering. All you need is the desire to make something. 

If...
Zgłoś jeśli naruszono regulamin