Programming Arduino Next Steps: Going Further with Sketches 2nd Edition Simon Monk all chapter instant download
Programming Arduino Next Steps: Going Further with Sketches 2nd Edition Simon Monk all chapter instant download
com
https://textbookfull.com/product/programming-arduino-next-
steps-going-further-with-sketches-2nd-edition-simon-monk/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/hacking-electronics-learning-
electronics-with-arduino-and-raspberry-pi-2nd-edition-simon-monk/
textboxfull.com
https://textbookfull.com/product/electronics-cookbook-practical-
electronic-recipes-with-arduino-raspberry-pi-1st-edition-simon-monk/
textboxfull.com
https://textbookfull.com/product/action-movement-light-and-sound-with-
arduino-and-raspberry-pi-first-edition-monk/
textboxfull.com
https://textbookfull.com/product/raspberry-pi-cookbook-software-and-
hardware-problems-and-solutions-2nd-edition-simon-monk/
textboxfull.com
Teach Yourself Electricity and Electronics Simon Monk
https://textbookfull.com/product/teach-yourself-electricity-and-
electronics-simon-monk/
textboxfull.com
https://textbookfull.com/product/arduino-programming-with-net-and-
sketch-1st-edition-agus-kurniawan/
textboxfull.com
https://textbookfull.com/product/arduino-programming-with-net-and-
sketch-1st-edition-agus-kurniawan-2/
textboxfull.com
https://textbookfull.com/product/practical-electronics-for-inventors-
fourth-edition-paul-scherz-simon-monk/
textboxfull.com
https://textbookfull.com/product/psychiatry-p-r-n-principles-reality-
next-steps-2nd-edition-sarah-stringer-laurence-church-susan-davison/
textboxfull.com
Programming
Arduino®
Next Steps
Next Steps
Going Further with Sketches
Second Edition
Simon Monk
ISBN: 978-1-25-958860-0
MHID: 1-25-958860-2
The material in this eBook also appears in the print version of this title: ISBN: 978-1-25-958859-4,
MHID: 1-25-958859-9.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occur-
rence 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 Education eBooks are available at special quantity discounts to use as premiums and sales promo-
tions or for use in corporate training programs. To contact a representative, please visit the Contact Us page at
www.mhprofessional.com.
McGraw-Hill Education, the McGraw-Hill Education logo, TAB, and related trade dress are trademarks or reg-
istered trademarks of McGraw-Hill Education and/or its affiliates in the United States and other countries and
may not be used without written permission. All other trademarks are the property of their respective owners.
McGraw-Hill Education is not associated with any product or vendor mentioned in this book.
Information has been obtained by McGraw-Hill Education from sources believed to be reliable. However, because
of the possibility of human or mechanical error by our sources, McGraw-Hill Education, or others, McGraw-Hill
Education 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 McGraw-Hill Education 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 Education’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 EDUCATION 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
Education 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 Education 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 Education has no responsibility for the content of any
information accessed through the work. Under no circumstances shall McGraw-Hill Education and/or its licen-
sors 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.
CONTENTS AT A GLANCE
1 Programming Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
7 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
A Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1 Programming Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is Arduino? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Installation and the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Installing the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Blink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
A Tour of Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Power Supply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Power Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Analog Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Digital Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Uno and Similar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Big Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Small Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Unofficial Arduinos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Modifying the Blink Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Digital Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Digital Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
The Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Analog Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
vii
Analog Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Using Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Arduino Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Arduino Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Arduino Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Minimizing RAM Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Use the Right Data Structures . . . . . . . . . . . . . . . . . . . . . . . . 121
Store String Constants in Flash Memory . . . . . . . . . . . . . . . 122
Common Misconceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Measure Free Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Minimizing Flash Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Use Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Remove Unwanted Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Bypass the Bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Static vs. Dynamic Memory Allocation . . . . . . . . . . . . . . . . . . . 125
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
C char Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
The Arduino String Object Library . . . . . . . . . . . . . . . . . . . . 130
Using EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
EEPROM Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Using the avr/eeprom.h Library . . . . . . . . . . . . . . . . . . . . . . 135
EEPROM Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Using Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Using SD Card Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
A Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Components and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Suppliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Arduino Starter Kits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Since the first edition of this book was released in 2013, some things in
the world of Arduino have stayed surprisingly constant and others have
changed. The Arduino Uno is still most people’s idea of what an Arduino
should be and the Arduino IDE has kept its simple interface, while adding
some great new features such as the Library and Board Managers.
However, what has changed is that the word “Arduino” has come to
denote a way of programming boards as much as the boards themselves.
New Arduino-compatible boards provide a vast variety of hardware to
choose from, all of which can be programmed using the Arduino IDE.
The main changes to this edition include the addition of two new
chapters:
• Chapter 3, “When Is an Arduino Not an Arduino?” This chapter
shows you how to use the Arduino IDE with Arduino-compatible
boards such as the ESP8266 and ESP32 boards as well as ARM m0
boards such as the Adafruit Circuit Playground Express and Trinket
m0 boards.
• Chapter 8, “Interfacing with Arduino” This chapter explains the
theory behind interfacing with microcontrollers that you will need
to understand when working with I2C, SPI, and UART serial.
The “Network Programming” chapter from the first edition (now
Chapter 14, “Network and Internet of Things Programming”) has been
extensively revised to include both official Arduino network program-
ming and the more widely used ESP8266 and ESP32 WiFi programming.
The chapter has also been extended to look at parsing JSON web services
and use of the dweet.io IoT service.
xv
Chapter 16, “Managing with One Process,” has gained a new section on
designing programs using finite state machine diagrams.
The other chapters of the book have also been given a general update.
Simon Monk
Many thanks to all those at McGraw-Hill Education who have done such
a great job in producing this book. In particular, thanks to my editor Lara
Zoble and to Patricia Wallenburg, Elizabeth Houde, Lynn Messina, Stephen
Smith, and Claire Splan.
Thanks to Duncan Amos for his diligent technical review. This book is
greatly improved by his keen eye for detail and unerring ability to sniff
out waffle and vagueness.
I would also like to thank Adafruit, SparkFun, and CPC for supplying
many of the modules and components used in the preparation of this
book.
And last but not least, thanks once again to Linda, for her patience and
generosity in giving me space to do this.
xvii
Downloads
The book includes some 75 example sketches, which are all open source and
available from the book’s web page at http://simonmonk.org/nextsteps2.
Follow the link to the pages for this book where you will be able to down-
load the code as well as an up-to-date list of errata for the book.
Resources
This book is supported by a page on the author’s website (http://
simonmonk.org/nextsteps2/) and a GitHub repository containing all the
examples (https://github.com/simonmonk/nextsteps2). Follow the link
for this book, and you will find all the source code, as well as other
resources such as errata.
What Is Arduino?
The term Arduino is used to describe both the physical Arduino board (of
which the most popular type is the Arduino Uno) and the Arduino system
as a whole. The system also includes the Arduino IDE software you need to
run on your computer (to program the board) and the peripheral shields
that you can plug into an Arduino board. Arduino has also come to mean a
whole array of third-party Arduino compatible boards that have nothing to
do with the Arduino organization, but that will work with the Arduino IDE.
To use an Arduino, you also need a “proper” computer. This can be a
Mac, Windows PC, Linux PC, or even something as humble as a Raspberry
Pi. The main reason that you need the computer is so you can install pro-
grams onto the Arduino board. Once installed on the Arduino, these pro-
grams can then run independently.
Figure 1-1 shows an Arduino Uno.
The Arduino can also communicate with your computer over USB.
While the computer is connected, you can send messages in both direc-
tions. Figure 1-2 shows the relationship between the Arduino and your
computer.
Sensors
Computer
Actuators
Pressure
Temp Sensor Stress
Sensor Sensor
Ethernet
Shield Switches
WiFi Variable
Shield Resistor
Bluetooth Range
Shield Finder
USB PIR
Host Sensor
Graphic
Relay
Display
16x2 Motor
Display Controller
LED
Here is a short selection of some of the amazing projects that have been
built using an Arduino:
• Bubblino—an Arduino linked to a bubble machine that blows
bubbles when you tweet it!
• 3D LED cubes
• Geiger counters
• Musical instruments
• Remote sensors
• Robots
"I'll try," said Jockie; but she knew she would not, and
she made a mental note of Sidney's strange speech, and
determined to keep her eyes open for the possible "sweet
bride."
"I had got it into my head that Mrs. Norman was the
attraction in the hunting field, but she happened to call late
yesterday afternoon, and from what she said I see my fears
were quite unfounded. In fact, she assured me that Austin
was quite offended with her because she talked to him for
his good, and told him his duty was to stay at home and
help his father and me. You used to have influence with
him, Sidney dear; can't you exert it now? It's a bitter
confession for a mother to make, but it is true. My words
make no impression upon him. He will listen to a stranger
rather than to me."
"I am sorry."
CHAPTER X
JOCKIE'S FRIEND
"'It will keep him more at home, Mrs. Norman, and that
will be a good thing,' for Austin had been telling me a little
of his home affairs. She gave me a nasty gleam out of her
eye, and then, ignoring me, went on to talk about people in
the hunting field, whom, of course, I didn't know; and then,
looking at her, it suddenly flashed across me, and I said:
'I've seen you before, Mrs. Norman, and I know someone
whom you know.'
"She gave a little start, but smiled and said:
"'I think when her aunts can spare her, she will come to
me,' she said, and she tried to speak very haughtily. So I
laughed and said:
"Oh, Jockie, dear, you must not talk so. She is her
mother. If your friend is a nice girl, she must feel attached
to her own mother."
"I'll tell you what she said. 'Do come and deliver me
from that poor old Major. He has come down every day for
the last week. I feel so sorry for him. It is a great pity he
has such an unhappy home. I cannot understand Miss
Urquhart; but then I don't know her. She seems to me such
a pleasant girl to outsiders, but she does not show much
affection to her poor old uncle.'
"And then Austin laughed. 'You have got hold of the pig
by the wrong ear, Mrs. Norman,' he said. 'The old Major is a
confounded bore, but his niece has always been most
awfully good to him. I've had the run of the house since I
was quite a small boy, so I know.'
"I don't see why you should try to quarrel with Austin,"
said Sidney very quietly.
"I don't think she has any skates with her," said Mrs.
Norman slowly.
"I should hope she won't, for Jockie's sake," Gavine said
earnestly. "When she does undertake a thing, she ought to
stick to it. That was her great fault at school, she was
brilliantly clever, but would never persevere."
"And perseverance comes easily to you?" Sidney asked
with a smile.
"You must come and see me, and we will have a good
talk together," said Sidney, being almost startled by the
vehemence in the girl's tone.
"My dear child. You will know how later on. Life has
hardly touched you yet. You are only on the threshold."
"Am I absurd!"
"No, dear, you two will never hit it off together, never!
Has your mother disappointed the old Major again?"
CHAPTER XI
AUSTIN'S ENLIGHTENMENT
"Oh, no, but women are beyond me. And it's my fate to
have my ideals shattered."
"I want to. But you're the only one on earth I'll speak to
about it; for I know you won't crow, and say 'I told you so.'
It's only—only I've had a nasty shock about that little
woman. I really can't bring myself to tell you, but it's all up
between us, and I'm going to clear out for a bit. The mother
condescended to say that I could have a holiday, and I'm off
to Cairo next week. I know a fellow going out, and I've fixed
it up with him."
"You don't ask questions? But I'll try to tell you. She
sent me a letter intended for your uncle. 'Pon my honour, I
feel sorry for her, but she began, 'My dearest,' and dashed if
I didn't read it right through before I twigged she had put it
in the wrong envelope. And she told him not to be angry
with her, for the 'poor boy' would not keep away, and she
could not make him see how he bored her. Then she went
on to hint that if this poor infatuated youth still frequented
her cottage, the Major must take into consideration that
there was her girl ready to amuse him, and young people
liked each other's company. Now what do you think of that?
After assuring me that the Major was a daily purgatory to
her. It bowled me over, I can tell you."
"I am sorry for you," Sidney said, "but you would take
no warnings. She has wanted to keep you both as friends."
"Oh!" cried Austin with a little groan. "I tell you it has
been a pretty stiff eye-opener to me! I sent the letter back
to her, and told her she need not trouble to send the letter
she originally meant for me, and, of course, I haven't been
near her since. She wrote an abject apology, saying she
could explain if I called, but mum's the word! And I shall be
out of it soon. And I'm jolly well cured of a leaning towards
your sex, Sid. If it were not for you, I'd never believe in a
single woman again. By George! How she's taken me in,
and befooled me. Do you think the Major got my letter?"
"I shouldn't like to tell you of the jeers and jibes she
has flung at his courting. But she may pull it off with him.
And I say, do befriend that poor girl; she does have a time
of it."
"And now I feel that Uncle Ted is doomed. She will not
let him escape her."
Jockie giggled.
"Will you let Gavine walk a part of the way with you. I
have promised to be home in time for the choir practice;
and she's dying to talk to you."
"You shall not go to London till you know how you can
be happy, dear. You will want the deep fountains of content
inside you to tide you over all the sin and misery that you
will see in London's slums. I wish you could come back to
dinner with me this evening. Do you think you could? We
will send you home."
"Oh, yes, she will. I will send a note down and say I
have kept you; that is the best way. Now let us go on
talking. I wonder what foundation you have under your
feet? I mean what do you rely on when things go wrong?
What is your aim, your hope, your inspiration?"
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com