77

The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John
Page 2: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John
Page 3: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John
Page 4: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino Arduino Jeremy Blum

Arduino

Arduino

Arduino

Exploring Arduino: Tools and Techniques for Engineering Wizardry, 978-1118549360, Jeremy Blum Copyright © 2013 by Wiley Publishing, Inc., Indianapolis, Indiana All rights reserved. This translation published under license. No part of this book may be reproduced in any form without the written permission of John Wiley & Sons, Inc. Copies of this book sold without a Wiley sticker on the back cover are unauthorized and illegal.

John Wiley & Sons, Inc.

John Wiley &Sons,Inc.

01-2014-3370

CIP Arduino / Blum,J. .

2014.10 Exploring arduino:tools and techniques for engineering wizardry

ISBN 978-7-121-24067-6 . A . . . TP368.1

CIP (2014) 187091

173 100036

787×980 1/16 20.5 400 2014 10 1 2014 10 1 59.00

010 88254888 [email protected] [email protected]

010 88258888

Page 5: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John
Page 6: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino IV

Jeremy Blum

Cornell University Sustainable Design

U.S. and World Green Building Councils CEOJeremy

LED

JeremyMakerBot Replicator 3D

NASA MakerBot Digitizer 3DCreative Machines Lab

3D

Jeremy YouTubeArduino Discovery Channel

hack-a-thons JeremyAmerican Institute of Electrical and Electronics Engineers 20122012 New Face of Engineering

Blum Idea Labs LLC

Page 7: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

V

JeremyJeremy

www.jeremyblum.com

Scott Fitzgerald 2006Arduino 2005

Interactive Telecommunications Program ITPScott ArduinoArduino 2011 Making Things Talk 22012 Arduino Arduino Starter Kit

Page 8: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino VI

Wiley Mary James Jennifer LynnScott Fitzgerald

element14 ArduinoSabrina Deitch Sagar Jethani

David

Page 9: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

VII

Arduino

Arduino

Arduino Arduino

Arduino

Arduino

Arduino

Page 10: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino VIII

Arduino

Arduino

Arduino

ArduinoArduino

Page 11: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

IX

http://kuangqi.me/

www.exploringarduino.com

Newark

Arduino ArduinoArduino IDE

Mac OS X 10.4 Windows XP Linux

Page 12: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino X

www.exploringarduino.com

Wiley www.wiley.comwww.wiley.com/go/exploringarduino Download Code

www.wiley.com ISBN ISBN 978-1-118- 54936-0

www.exploringarduino.com www.wiley.com ZIP

ISBNISBN 978-1-118-54936-0

www.wiley.com/go/exploringarduinoErrata

Arduino Arduino

Arduino

Arduinowww.arduino.cc/en/Reference/HomePage

Page 13: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

XI

Arduinowww.jeremyblum.com/category/arduino-tutorials adafruit Arduinolearn.adafruit.com/category/learn-arduino SparkFunlearn.sparkfun.com/tutorials Arduinowww.arduino.cc/forum element14 Arduinowww.element14.com/community/groups/arduino

Twitter@sciguy14

www.jeremyblum.com/contact

Arduino

Arduino ArduinoWeb

ArduinoArduino IDE Arduino

shieldArduino

ArduinoArduino

1 ArduinoArduino

Arduino Arduino

Page 14: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino XII

Arduinowww.oshwa.org/definition

TEDxhttp://kuangqi.me/arduino/

Wiley

ArduinoArduino

Arduino ArduinoArduino

Arduino Arduino

ArduinoArduino

ArduinoMakerBot Replicator 3D

Arduino Mega www.thingiverse.com/thing:16058!

ArduinoGNU

GPL

YouTube Arduinowww.youtube.com/sciguy14

Page 15: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

XIII

www.jeremyblum.com/portfolio Arduino

Arduino

Page 16: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino XIV

/ 1

Arduino / 2

Arduino / 3

Arduino / 3

Arduino / 7

/ 12

Arduino IDE / 12

IDE Arduino / 13

/ 15

/ 17

/ 18

/ 19

LED / 19

/ 23

for / 24

analogWrite() / 25

/ 28

/ 28

/ 30

RGB LED / 33

Page 17: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

XV

/ 37

/ 38

/ 39

/ 39

/ 40

Arduino analogRead() / 41

/ 42

/ 45

/ 50

/ 50

/ 52

/ 54

/ 55

/ 56

/ 57

/ 58

PWM / 62

H- / 64

/ 70

/ 71

/ 71

/ 75

/ 76

/ 80

/ 81

/ 82

/ 82

/ 83

tone() / 84

/ 85

/ 86

/ 88

Page 18: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino XVI

tone() / 91

/ 91

/ 94

USB / 95

Arduino / 96

USB- Arduino / 97

USB ATMega MCU Arduino / 99

USB MCU Arduino / 101

USB-Host Arduino / 101

Arduino / 102

print / 102

/ 103

/ 105

Arduino / 105

/ 106

/ 112

Processing / 113

Arduino Leonardo 32U4 Arduino / 119

/ 119

/ 124

/ 127

/ 129

/ 130

/ 131

74HC595 / 131

Arduino / 134

/ 136

/ 137

/ 137

LED / 139

/ 142

Page 19: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

XVII

/ 143

I2C / 144

I2C / 145

I2C / 145

ID / 146

/ 147

I2C / 148

/ 148

/ 149

/ 151

I2C / 153

/ 153

/ 154

Processing / 156

/ 159

SPI / 160

SPI / 161

SPI / 162

/ 162

/ 163

SPI I2C / 164

SPI / 164

/ 164

/ 167

/ 169

SPI / 171

/ 172

/ 173

/ 175

/ 176

LCD / 177

LiquidCrystal LCD / 180

Page 20: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino XVIII

/ 180

/ 182

/ 185

/ 185

/ 188

/ 189

/ 190

/ 194

/ 194

XBee / 195

XBee / 196

XBee / 197

XBee / 198

XBee / 201

USB / 202

Xbee XBee / 203

X-CTU XBee / 204

XBee / 207

/ 209

Arduino / 209

Processing / 211

RGB LED / 214

Arduino / 216

/ 217

/ 218

/ 218

/ 219

/ 220

/ 223

/ 225

/ 226

/ 227

Page 21: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

XIX

/ 228

Arduino / 229

/ 230

/ 237

/ 237

/ 238

/ 238

/ 239

/ 239

/ 240

/ 242

SD / 243

/ 244

CSV / 244

SD / 245

SD Arduino / 249

SD / 249

SD SPI / 253

SD / 253

SD / 258

/ 261

/ 261

/ 263

/ 268

/ 268

/ 270

/ 273

/ 274

Arduino / 275

Arduino / 276

/ 276

/ 279

Arduino / 279

Page 22: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino XX

Arduino / 279

I/O / 280

/ 280

Arduino / 282

Arduino / 286

/ 290

Xively / 290

/ 295

/ 299

ATMega Arduino / 300

/ 300

/ 300

/ 302

Arduino / 304

Page 23: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

1 Arduino

2

3

Page 24: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

2

Arduino

Arduino Uno USB

www.exploringarduino.com/content/ch1

www.wiley.com/go/exploringarduinoDownload Code 1

Arduino Arduino

ArduinoArduino

Arduinohttp://kuangqi.me/arduino/

Wiley

Page 25: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

1 Arduino

3

Arduino

Arduino 3

Arduino Arduino Arduino IDE

Arduino

Arduino ArduinoIDE

Arduino

Arduino Uno

Arduino

Arduino

Arduino Arduino Uno 1-1

Atmel USB / I/O RX/TX LED ICSP

Page 26: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

4

Ard

uino

ww

w.a

rdui

no.c

c

1-1 Arduino Uno

LED

M

CU

ICSP

ATM

ega3

28

MC

U I/O

AD

C

7-12

V

USB

USB

Page 27: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

1 Arduino

5

Atmel

Arduino Atmel MCUArduino Arduino Uno AVR ATMega1-1 Arduino Uno ATMega 328p Due

ARM CortexArduino

ADC / I/O I2C SPIArduino

16MHzATMega

Arduino 13LED

LED

ATMega C1-2 ICSP Arduino

USBArduino bootloader

ICSP ATMegaUSART / Arduino

Arduino

Arduino Uno Mega 2560ATMega 16U2 8U2 USB USART

ATMega 32U4 Arduino Leonardo USBArduino

FTDI USB ATMega USART USB

Page 28: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

6

©20

13 A

tmel

Cor

pora

tion.

1-2 AVR ISP MKII

I/O ADC

Arduino I/O

ADC 0 5V

USB 5VArduino

DC Vin 6 20V 7 12VArduino 5V 3.3V

5V I/O5V 0V

3.3V 3.3V

Arduino

bootloader Arduino MCUAVR ICSP

SPISTK500 AVR ISP MKII

Page 29: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

1 Arduino

7

1-2 Arduino

MCU UART IDEMCU

Arduino IDE USB

Arduino Uno ATMega 16U2 8U2 MCUMCU

UART USB-

USB

2KB

Arduino ATMega ICSPIDE File-Upload Using Programmer

Arduino

ArduinoArduino

Uno 1-3 Arduino16U2 USB- ATMega 328p MCU

DIP SMD MCU

Page 30: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

8

Ard

uino

ww

w.a

rdui

no.c

c

1-3 Arduino Uno

Leonardo 1-4 USB 32U4MCU -USB

6 USB

Ard

uino

ww

w.a

rdui

no.c

c

1-4 Arduino Leonardo

Mega2560 1-5 ATMega 2560 MCU 54I/O Mega ADC 4

Uno

Page 31: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

1 Arduino

9

Ard

uino

ww

w.a

rdui

no.c

c

1-5 Arduino Mega 2560

8 AVR MCU Arduino Due 1-632 ARM Cortex-M3 SAM3X MCU Due ADC

PWM DAC USB Host84MHz

Ard

uino

ww

w.a

rdui

no.c

c

1-6 Arduino Due

Nano 1-7

Page 32: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

10

Coo

king

Hac

ksw

ww

.coo

king

hack

s.com

1-7 Arduino Nano

Mega ADK 1-8 Mega 2560 USB HostAndroid

Ard

uino

ww

w.a

rdui

no.c

c

1-8 Arduino Mega ADK

LilyPad 1-9LED FTDI

Page 33: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

1 Arduino

11

Ard

uino

ww

w.a

rdui

no.c

c

1-9 LilyPad Arduino

ArduinoArduino Arduino IDE

Seeeduino adafruit 32U4 SparkFun Pro Mini Arduino

ArduPilotDIY 1-10

Arduino MakerBot Replicator Replicator 2 3D

3D R

obot

ics,

Inc.

,ww

w.3d

robo

tics.c

om

1-10 ArduPilot Mega

Page 34: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

12

Arduino

Arduino IDE

Arduino www.arduino.cc Download IDE1-11

1-11 Arduino.cc

Arduino IDE WindowsIDE ZIP

Page 35: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

1 Arduino

13

IDE Arduino

IDE Arduino USB1-12 Mac Linux

1-12 Arduino Uno USB

OS X Uno Mega 2560Network Preferences

Apply Not Configured System Preferences

Windows ArduinoWindows

Windows IDEWindows ZIP

Page 36: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

14

WindowsArduino.cc

1

2

3

4 COM LPT Arduino

5

6

7 Arduino IDE driverFTDI

8 Windows

Arduino IDE ArduinoBlink LED

Arduino 13 LED File-Example-BasicBlink IDE Blink

Arduino2

IDE ArduinoTools-Board Uno

13 LED

IDETools-Serial Port Windows

COM* *

COM COM

Linux Mac/dev/tty.usbmodem* /dev/tty.usbserial* *

Page 37: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

1 Arduino

15

IDE UploadIDE Arduino

LED Arduino

Blink Arduino1-13

1-13 Blink

1Arduino

/* */

1 2 3 4 5 6 7 8 9 10

Page 38: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

16

2 //

3 Arduinoint

integer led 13 LEDArduino Uno 13led LED

I/O

4 void setup() Arduinosetup()

5 ArduinopinMode()

pinModeled

13 INPUTOUTPUT

LEDled I/O

6 Arduino void loop() loop()Arduino Arduino

loop()

7 digitalWrite()5V 0V LED 5V LED

2 digitalWrite()HIGH 5V LOW 0V

8 delay() delay()Arduino 1000ms

Page 39: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

1 Arduino

17

1s LED 1s

9 digitalWrite() LOW LED

10 1s LED

Arduino Arduino USB Arduino Arduino Arduino

Page 40: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

18

Arduino Uno

10k 220 ×3 USB

5mm LED 5mm RGB LED

www.exploringarduino.com/content/ch2

www.wiley.com/go/exploringarduinoDownload Code 2

1 LED Arduino

Page 41: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

19

Arduino

Arduino

RGB LED

PWM http://kuangqi.me/arduino/

Wiley

http://kuangqi.me/arduino/Wiley

1 Arduino LEDArduino

for

LED

1 LEDLED Arduino 9

LED9 PWM

Page 42: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

20

Arduino 5V

2-1

2-1

LED

LED LED

LED

LED Light-Emitting Diode

Page 43: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

21

LEDLED 5V

LED

LED 9 LEDLED

220 2-2

Fritz

ing

2-2 Arduino Uno LED

9V LED

Page 44: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

22

U = IR U I R

LED LEDLED

LED LED20 1 1/1000 mA LED

2V 2-3 LED

Eagl

e

2-3 LED

LED 20mA 2V 5V5V LED 2V 3V

20mA R

R = U/I

Page 45: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

23

U = 3V I = 20mA R R = 3V / 0.02A = 150 150 20mA

LED 220150 LED

1/8 W mW

P = UI P I U

3V 20mA P = 3V × 0.02A = 60mW1/8W 125mW

ArduinoArduino Arduino

setup() loop()

1 setup() loop()

setup()9

pinMode() 9digitalWrite() 5V 2-1

2-1 LED——led.ino const int LED=9; // LED 9 void setup() { pinMode (LED, OUTPUT); // LED digitalWrite(LED, HIGH); // LED } void loop() { // loop }

Page 46: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

24

Arduino 2-2const

constLED

9 const

1 digitalWrite() loop

for

forfor

2-2

2-2 LED blink.ino const int LED=9; // LED 9 void setup() { pinMode (LED, OUTPUT); // LED } void loop() { for (int i=100; i<=1000; i=i+100) { digitalWrite(LED, HIGH); delay(i); digitalWrite(LED, LOW); delay(i); } }

Arduinofor for

i100

Page 47: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

25

<= i

1000

i 100 for

1 i 100

2 LED 100ms i

3 LED 100ms i

4 i 100 200

5 200 1000

6 LED 200ms i

7 LED 200ms i

8 i 100 300

9 i 1000 loop()i 100

Arduino PWM ArduinoI/O

analogWrite()

LED0V 5V

Due DACDAC

PWMArduino analogWrite()

PWM~ Arduino Uno 3 5 6 9

10 11 PWM Uno 2-1analogWrite() LED

Page 48: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

26

LEDanalogWrite() PWM analogWrite()

PWM 8 0 28-10 255 for LED

2-3

2-3 LED ——fade.ino const int LED=9; // LED 9 void setup() { pinMode (LED, OUTPUT); // LED } void loop() { for (int i=0; i<256; i++) { analogWrite(LED, i); delay(10); } for (int i=255; i>=0; i--) { analogWrite(LED, i); delay(10); } }

LED LED

for i++i=i+1 i-- i=i-1 for

LED

PWMPWM DC

PWM2-4

Page 49: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

27

MAT

LAB

2-4 PWM

PWM

50%

analogWrite() analogWrite() 0 0

255 100% 127 50%

2-4 25% 25% 75%Arduino 490Hz

5V 0V 490

LED LEDLED 1ms

50%

LED

25% 50% 75%

100% 0%

V

V

V

V

V

ms ms ms

ms ms

Page 50: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

28

2-1 2-5

Arduino

2-5 10k

0V 5V

2-5

10k10k

5V

5V

Page 51: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

29

Fritz

ing

2-5 LED Arduino

5V

5V

LED2-4

2-4 LED ——led_button.ino const int LED=9; //LED 9 const int BUTTON=2; // 2 void setup() {

Page 52: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

30

pinMode (LED, OUTPUT); // LED pinMode (BUTTON, INPUT); // } void loop() { if (digitalRead(BUTTON) == LOW) { digitalWrite(LED, LOW); } else { digitalWrite(LED, HIGH); } }

digitalRead if/elseconst int setup()

digitalRead()BUTTON digitalRead()

HIGH 1 LOW 0 if()if()

digitalRead() LOW ==digitalRead() LOW

LED LOWelse LED HIGH

-

2-6MATLAB

Page 53: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

31

MAT

ALA

B

2-6

25ms 2-6

2-6

1 LOW

2

3 5ms

4 5ms

5 LED

6

7 2

digitalWrite()

ms ms

V

V

Page 54: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

32

LEDArduino

2-5

2-5 ——debounce.ino const int LED=9; //LED 9 const int BUTTON=2; // 2 boolean lastButton = LOW; // boolean currentButton = LOW; // boolean ledOn = false; //LED / void setup() { pinMode (LED, OUTPUT); // LED pinMode (BUTTON, INPUT); // } /* * * */ boolean debounce(boolean last) { boolean current = digitalRead(BUTTON); // if (last != current) // { delay(5); // 5ms current = digitalRead(BUTTON); // } return current; // } void loop() { currentButton = debounce(lastButton); // if (lastButton == LOW && currentButton == HIGH) // { ledOn = !ledOn; // LED } lastButton = currentButton; // digitalWrite(LED, ledOn); // LED }

2-5 LED3 const

Page 55: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

33

3LOW LOW false

=

boolean debounce(boolean last)last /

/ / 1/0last

!= ifif if

5ms 5ms

return current currentcurrent debounce()

debouncecurrentButton

debounce currentButton=debounce(lastButton)setup() loop() currentButtondebounce

currentButton if&&

AND if &&if LOW

HIGH ledOnledOn !

LED

LEDLED

RGB LED

Page 56: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

34

PWM LED RGB LEDRGB LED

RGB LED

LED LED 43

LED Arduino 3 PWM 2-7

Fritz

ing

2-7

LEDRGB LED

2-6 7 LEDanalogWrite()

loop() LEDLEDArduino analogWrite()

RGB LED

Page 57: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

35

2-6 LED ——rgb_nightlight.ino const int BLED=9; // LED 9 const int GLED=10; // LED 10 const int RLED=11; // LED 11 const int BUTTON=2; // 2 boolean lastButton = LOW; // boolean currentButton = LOW; // int ledMode = 0; // LED void setup() { pinMode (BLED, OUTPUT); // LED pinMode (GLED, OUTPUT); // LED pinMode (RLED, OUTPUT); // LED pinMode (BUTTON, INPUT); // } /* * * */ boolean debounce(boolean last) { boolean current = digitalRead(BUTTON); // if (last != current) // { delay(5); // 5ms current = digitalRead(BUTTON); // } return current; // } /* * LED * LED */ void setMode(int mode) { // if (mode == 1) { digitalWrite(RLED, HIGH); digitalWrite(GLED, LOW); digitalWrite(BLED, LOW); } // else if (mode == 2) {

Page 58: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

36

digitalWrite(RLED, LOW); digitalWrite(GLED, HIGH); digitalWrite(BLED, LOW); } // else if (mode == 3) { digitalWrite(RLED, LOW); digitalWrite(GLED, LOW); digitalWrite(BLED, HIGH); } // + else if (mode == 4) { analogWrite(RLED, 127); analogWrite(GLED, 0); analogWrite(BLED, 127); } // + else if (mode == 5) { analogWrite(RLED, 0); analogWrite(GLED, 127); analogWrite(BLED, 127); } // + else if (mode == 6) { analogWrite(RLED, 127); analogWrite(GLED, 127); analogWrite(BLED, 0); } // + + else if (mode == 7) { analogWrite(RLED, 85); analogWrite(GLED, 85); analogWrite(BLED, 85); } // (mode = 0) else { digitalWrite(RLED, LOW); digitalWrite(GLED, LOW); digitalWrite(BLED, LOW); } } void loop() { currentButton = debounce(lastButton); // if (lastButton == LOW && currentButton == HIGH) // {

Page 59: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

37

ledMode++; // LED } lastButton = currentButton; // // 0 if (ledMode == 8) ledMode = 0; setMode(ledMode); // LED }

1 LED

LED LED Arduino PWM LED for

Page 60: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

38

Arduino Uno

10k 10k ×2 220 ×3 USB

TMP36 5V 5mm RGB LED

www.exploringarduino.com/content/ch3http://kuangqi.me/arduino/

www.wiley.com/go/exploringarduinoDownload Code 3

Page 61: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

3

39

Arduino

ArduinoUSB

Arduino

http://kuangqi.me/arduino/Wiley

http://kuangqi.me/arduino/Wiley

2 LED1 0 Arduino

3-10V 5V 2

Page 62: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

40

0V 5V

MAT

LAB

3-1

lux0 130 000 lux

400lux401lux 401.1lux 401.11lux

Arduinoanalog-to-digital converter ADC

0V5V

ArduinoArduino ADC

ADC Arduino Uno 10

ADC 10 ADC210 210=1024

s s V

V

Page 63: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

3

41

Arduino 0 10235V

10235V ADC 0V 0 2.5V

512 1023 5V 10233 ADC 3-2

MAT

LAB

3-2 3

Arduino analogReference() www.arduino.cc/

en/Reference/AnalogReference

3 ADC 3 23=8 8 0 73 ADC 0 7 3-2

Arduino Uno 10243-2 8

Arduino analogRead()

ArduinoanalogRead()

s

V

s

V

Page 64: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

42

pot

35V 5V

Arduino 0 3-3Arduino

Fritz

ing

3-3

0 0 5V3-4

3-4

Page 65: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

3

43

3-4

ArduinoADC analogRead()

Arduino Serial.println()Arduino IDE Arduino 3-1

3-1 ——pot.ino // const int POT=0; // 0 int val = 0; // void setup() { Serial.begin(9600); } void loop() { val = analogRead(POT); Serial.println(val); delay(500); }

setup() Serial.begin()

Page 66: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

44

9600

val ADC 0analogRead() ADC 0

A0 analogRead()0

0 1023 Serial.println()

0.5s

Arduino Arduino TX LED 500msLED Arduino USB

ArduinoArduino IDE 3-5

3-5

Page 67: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

3

45

01023 3-6

3-6

9600

LED

VCC GND Arduino ADCVCC GND

Page 68: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

46

Arduino 0 5V

www.exploringarduino.com/parts/IR-Distance-Sensor

www.exploringarduino.com/parts/JST-Wire

5

TMP36

www.exploringarduino.com/parts/TMP36

TMP3610mV 1

=[( ) – 500]/10-500 0 3-7

Ana

log

Dev

ices

, Inc

., w

ww.

anal

og.c

om

3-7

V

Page 69: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

3

47

www.exploringarduino.com/parts/TriAxis-Analog-Accelerometer

X Y Z

3.3VanalogReference() AREF 3.3V

www.exploringarduino.com/parts/DualAxis-Analog-Gyroscope

ArduinoSudoGlove www.sudoglove.com

3.3V

TMP36

Arduino Uno 5V

2 RGB LED0 3-8

Page 70: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

48

Fritz

ing

3-8

LED

3-1

20 1433-7

143/1023 700mVmV

10 = mV 500

700mV 2022 147

18 139 LEDif digitalWrite()

analogRead()LED

Page 71: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

3

49

3-2

3-2 ——tempalert.ino const int BLED=9; // LED 9 const int GLED=10; // LED 10 const int RLED=11; // LED 11 const int TEMP=0; // A0 const int LOWER_BOUND=139; // const int UPPER_BOUND=147; // int val = 0; // void setup() { pinMode (BLED, OUTPUT); // LED pinMode (GLED, OUTPUT); // LED pinMode (RLED, OUTPUT); // LED } void loop() { val = analogRead(TEMP); if (val < LOWER_BOUND) { digitalWrite(RLED, LOW); digitalWrite(GLED, LOW); digitalWrite(BLED, HIGH); } else if (val > UPPER_BOUND) { digitalWrite(RLED, HIGH); digitalWrite(GLED, LOW); digitalWrite(BLED, LOW); } else { digitalWrite(RLED, LOW); digitalWrite(GLED, HIGH); digitalWrite(BLED, LOW); } }

Page 72: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

50

3-9 3-9A0 Arduino 0

3-9

Page 73: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

3

51

Vout = Vin(R2 / (R1 + R2))

5V ArduinoR1 R2 10k 5V 2

2.5V

Vout = 5V(10k / (10k + 10k)) = 5V × 0.5 = 2.5V

3-10200k 200k

R1 R2

R1 R2 10k3-11 RGB LED

elem

ent1

4w

ww

.ele

men

t14.

com

3-10

Page 74: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

52

Fritz

ing

3-11

3-10 1023

200 900

analogWrite() LED8 0 255 ADC

1023 Arduinomap() constrain() map()

output = map(value, fromLow, fromHigh, toLow, toHigh)

value fromLow

Page 75: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

3

53

fromHigh200 900 toLow toHigh

analogWrite() 0 255LED

ADC LED

map()255 0 map()fromLow fromHigh 200

900 toLow toHigh 255 0 550 127550 200 900 127 255 0 map()

200255255 analogWrite() constrain()

constrain()

output = constrain(value, min, max)

map() constrain() min0 max 2550 255 LED

3-3

3-3 ——nightlight.ino const int RLED=9; // LED 9 PWM const int LIGHT=0; // 0 const int MIN_LIGHT=200; // const int MAX_LIGHT=900; // int val = 0; // void setup() { pinMode(RLED, OUTPUT); // LED } void loop() { val = analogRead(LIGHT); // val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0); // val = constrain(val, 0, 255); // analogWrite(RLED, val); // LED }

Page 76: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John

Arduino

54

valmap() val val

2

Page 77: The Bloom of Youth | 锦瑟华年 - 正文kuangqi.me/arduino/exploring_arduino_preview.pdfNo part of this book may be reproduced in any form without the written permission of John