Program Arduino with ease! Using clear, easy-to-follow examples, Programming Arduino: Getting Started with Sketches reveals the software side of Arduino and explains how to write well-crafted sketches using the modified C language of Arduino. No prior programming experience is required! The downloadTMProgramming arduinoGetting Started with SketchesSimon monkMcew York Chicago san francisco LisbonLondon Madrid Mexico City Milan New DelhiSan Juan Seoul Singapore Sydney TorontoThe McGraw-Hill companiesCopyright o 2012 by The McGraw-Hill Companies. All rights reserved. Except aspermitted under the United States Copyright Act of 1976, no part of this publication maybe reproduced or distributed in any form or by any means, or stored in a database orretrieval system, without the prior written permission of the publisherISBN:978-0-07-178423-8MHID:0-07-178423-3The material in this e book also appears in the print version of this title: ISBN: 978-0-07178422-1,MHID:0-07-178422-5All trademarks are trademarks of their respective owners. Rather than put a trademarksymbol after every occurrence of a trademarked name, we use names in an editoriafashion only, and to the benefit of the trademark owner, with no intention of infringementof the trademark, Where such designations appear in this book, they have been printedwith initial capsMcGraw-Hill eBooks are available at special quantity discounts to use as premiums andsales promotions, or for use in corporate training programs. To contact a representativepleasee-mailusatbulksales@mcgraw-hill.com.All trademarks or copyrights mentioned herein are the possession of their respec-tiveowners and Mcgraw-Hill makes no claim of ownership by the mention of products thatcontain these marks“ Arduino” is a trademark of the arduino teanInformation has been obtained by McGraw-Hill from sources believed to be reliableHowever, because of the possibility of human or mechanical error by our sources,MCGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, orcompleteness of any information and is not responsible for any errors or omissions or theresults obtained from the use of such informationTERMS OF USEThis is a copyrighted work and The McGraw-Hill Companies, Inc(McGraw-Hill)andits licensors reserve all rights in and to the work. Use of this work is subject to theseterms. Except as permitted under the Copyright Act of 1976 and the right to store andretrieve one copy of the work, you may not decompile, disassemble, reverse engineer,reproduce, modify create derivative works based upon, transmit, distribute, disseminatesell, publish or sublicense the work or any part of it without McGraw- Hill's prior consentYou may use the work for your own noncommercial and personal use; any other use of thework is strictly prohibited. Your right to use the work may be terminated if you fail tocomply with these termsTHE WORK IS PROVIDED"AS IS MCGRAW-HILL AND ITS LICENSORS MAKENO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY ORCOMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORKINCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THEWORK VIA HYPERLINK OR OTHERWISE AND EXPRESSLY DISCLAIM ANYWARRANTY EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TOIMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR APARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guaranteethat the functions contained in the work will meet your requirements or that its operationwill be uninterrupted or error free. Neither Mcgraw-Hill nor its licensors shall be liable toyou or anyone else for any inaccuracy, error or omission, regardless of cause, in the workor for any damages resulting therefrom. Mcgraw-Hill has no responsibility for the contentof any information accessed through the work. Under no circumstances shall McGraw-Hill andor 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 workeven if any of them has been advised of the possibility of such damages. This limitation ofliability shall apply to any claim or cause whatsoever whether such claim or cause arisesin contract tort or otherwiseTo my boys, Stephen and Matthew,from a very proud dad.about the authorSimon monk has a bachelor 's degree in cybernetics and computer science and a doctoratein software engineering. He has been an active electronics hobbyist since his school daysand is an occasional author in hobby electronics magazines. He is also author of 30Arduino Projects for the Evil Genius and 15 Dangerously Mad Projects for the evilGeniusCONTENTSAcknowledgmentsIntroduction1 This Is arduinoMicrocontrollersDevelopment BoardsA Tour of an arduino boardPower supplyPower connectionsAnalog inputsDigital ConnectionsMicrocontrollerOther ComponentsThe Origins of ArduinoThe Arduino FamilyUno, Duemilanove, and DiecimilaMegaNanoBluetoothLilypadOther“ Official” BoardsArduino clones and variantsConclusion2 Getting StartedPowering upstalling the softwareUploading Your First SketchThe Arduino ApplicationConclusion3C Language basicsProgrammingWhat Is a Programming LanguageBlink— Again!rabiesExperiments in CNumeric variables and arithmeticCommandsforwhileThe #define directiveConclusion4 FunctionsWhat is a function?ParametersGlobaL, Local, and Static VariablesReturn valuesOther Variable TypesfloatsbooleanOther data TypesCoding StyleIndentationOpening bracesWhitespaceCommentsConclusion5 Arrays and StringsArraysMorse Code SoS Using ArraysString ArraysString literals