You are on page 1of 56

1

1 -7

Arduino

2014


arduino.
(12.10)
, ,
.
(Hands on Training).
arduino.
,
.

.

.

. ,
Arduino .
1,2 8


Arduino


: 1989

: 2084
: . ,

, 2012

.............................................................................................................................................. vi
1 ............................................................................................................................................. 1
Arduino ....................................................................................................................................................... 1
2 ............................................................................................................................................. 4
Arduino Uno ............................................................................................................................................... 4
2.1

.......................................................................................................................................... 4

2.2

Arduino Uno ........................................................................................................... 4

2.3

........................................................................................................................... 5

2.4

Arduino ................................................................................................... 6

2.5

......................................................................................................................... 6

2.6

............................................................................................................................... 7

2.7

............................................................................................................................... 8

2.8

..................................................................................................... 8

2.9

........................................................................................... 8

2.10

Arduino ........................................................13

2.11

(Serial Monitor) ............................................................................................14

2.12

H .....................................................................................................15

2.13

.............................................................15

2.14

(Digital pins) ..............................................................................................18

2.15

(Analog input pins)..................................................................19

2.16

(Libraries) ...................................................................................20

vi

8 .........................................................................................................................................100
Processing .............................................................................................................................................100
8.1

Processing..........................................................................................100

8.2

Processing .....................................................................................100

8.3

.................................................................................................101

8.4

.....................................................................................................102

8.5

5: .......................103

9 .........................................................................................................................................110
.................................................................................................110

viii

1 : Arduino

1
Arduino

Arduino
Smart Project. , Arduino
2005 ,
Ivrea



.
, Arduino

, .
, ,
/
. Arduino

( )
.
.
.
; Arduino .
,

Arduino
. , Projects
( hardware) software /
( Flash, Processing, MaxMSP). Arduino
Atmega382 chip FTDI

.
Arduino
(BootLoader).

Flash Memory Arduino USB .
, ,

Arduino
.

1 : Arduino

Arduino:

: Arduino
. ,
.

:
Arduino
.

: Arduino
. ,

. ,
.

Arduino
,
:

Arduino Uno
Arduino Diecimila
Arduino Duemilanove
Arduino Mega1280
Arduino Mega2560
Arduino Mini
Arduino Nano
Arduino USB

Arduino Stamp
Arduino Fio
Arduino NG
Arduino NG+
Arduino Extreme
Arduino Bluetooth
LilyPad Arduino
Serial Arduino

Arduino
.

Arduino

Atmel AVR

Flash
KiB

EEPROM
KiB

SRAM
KiB

Diecimila
Duemilanove
Uno
Mega
Fio
Mega 2560

ATmega168
ATmega168/328
ATmega328
ATmega1280
ATmega328P
ATmega2560

16
16
32
128
32
256

0.5
0.5
1
4
1
4

1
1
2
8
2
8

/
14
14
14
54
14
54

PWM
6
6
6
14
6
14

6
6
6
16
8
16

1 : Arduino

1.1 Arduino
Arduino (IDE)
Java
. IDE Arduino , C,
C++, Arduino, ..
,
Arduino Wiring (C, C++). IDE Arduino
GNU toolchain AVR Libc
C, C++ AVR ,
avrudude Flash
memory Arduino.
Arduino
Creative Commons
Attribution Share-Alike 2.5. , (IDE) Arduino
GNU General Public License
Version2.

2 : Arduino Uno

2
Arduino Uno

2.1
Arduino Uno .
Arduino 14
input/output pins 6 .
20 pins
(, LEDs, LCD ) (Ultrasonic, ,
accelometers .).
USB.
,
, . ,


.

2.2 Arduino Uno


Arduino 14 /
- pinMode(), digitalWrite(),
digitalRead() . 5 Volts
40mA. pin
Pull-up 20-50K. , 6
.
0-1023.
0 5 volts. , 6
14 P3, P5, P6, P9, P10 P11
.

: 0 (RX) 1 (TX). (RX)


(TX) TTL .

: 2 3.
.
4

10

2 : Arduino Uno

attachInterrupt().
0,1.

PWM: 3, 5, 6, 9, 10, 11 pins. 8-bit PWM


analogWrite().

SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK).


SPI, hardware
Arduino.

LED: 13. 13 LED.


HIGH, LED pin LOW
.

2.1 Arduino Uno

2.3
Arduino
. ,
. 0 (RX) 1 (TX),
USB. , (USB),
0 1
.
,
Arduino Arduino.
Serial Monitor
Serial.begin().

11

2 : Arduino Uno

begin()
end()
available()
read()
peek()
flush()
print()
println()
write()

( )
( )
( )
( )
( byte )
( buffer )
( )
( Print(), )
( )

2.4 Arduino

Microcontroller:
:
:
():
Digital I/O Pins:

Analog Input Pins:


DC I/O Pin:
DC 3.3V Pin:
Flash Memory:

SRAM:
EEPROM:
Clock Speed:

ATmega328
5V
7-12V
6-20V
14 ( 6 PWM
)
6
40 mA
50 mA
32 KB 0,5 KB
bootloader
2 KB
1 KB
16 MHz

2.5
Arduino :

Flash memory (32 Kbytes)



.

SRAM memory ( 2 Kbytes)



.

EPROM memory (1 Kbytes)


(OFF).
Reset
Arduino.

12

2 : Arduino Uno

, Flash EPROM (
). SRAM
.
SRAM, ,
. ,
, . ,
sketch (
). ,
SRAM.
,

.

2.6
Arduino Uno
2.1mm ( )
Arduino USB .
H .
, 9Volt 220V.
Arduino Vin GND
.

.
6 20 Volts.
7 Volt pin 5Volt
5 Volts. , 12 Volts
. ,
9 Volts.

:
VIN. .
.
5V. 5Volt.

.
Vin , USB
5V
3V3. 50mA.
GND.

13

2 : Arduino Uno

2.7
rduino Uno
, Arduino .
ATMega382 L 5 Volt UART,
( RX) 0 ( TX) 1 .
,
Arduino


ATmega382
chip FTDI. ,

.
Arduino USB
COM
2.2 ATmega328
. firmware ATmega382
USB COM
. , Windows .inf .
Arduino

Arduino. RX TX LED
USB-to-chip USB
( 0 1).

2.8
Arduino Uno Wiring (
C++ ).

.
STK500.

Processing Wiring,
(pen source).
" ".

2.9
,
, .
Windows XP.

14

2 : Arduino Uno

1. Arduino USB

Arduino Uno. USB
.

2. Arduino
Arduino-1.0
http://arduino.cc/en/Main/Software.
3.
Arduino Uno
USB. LED .
4.
+ , .
+ ,
.

15

2 : Arduino Uno

+ Arduino Uno.
.

+ Arduino.

10

16

2 : Arduino Uno

+ drivers .

+ Arduino.

+ (COM & LPT)


Serial Port COM 15 Arduino .
Arduino 15.

11

17

2 : Arduino Uno

5. Arduino
Arduino.exe

6. Board
, Tools Board
Arduino Uno COM15.

12

18

2 : Arduino Uno

2.10 Arduino
Arduino
, , ,
, .
Arduino .
sketch. sketch
.
/ / .
Arduino
.
,
sketch, sketch .

(Tabs)


2.3 (IDE) Arduino

13

19

2 : Arduino Uno

:
Verify/Compile (/):
Upload:
New(): sketch
Open(): sketch,

Save(): sketch
Serial Monitor( ):

2.11 (Serial Monitor)


Arduino.
, ,
send Enter. ,
, (baud)

Arduino Serial.begin().

2.4 Serial Monitor

2.4: Serial Monitor

14

20

2 : Arduino Uno

2.12 H
Arduino :
//
void setup ()
{
//
}
void loop ()
{
//
}

sketch Arduino
setup() loop(). setup() , sketch
(reset) Arduino. ,
, (pins)
. , loop()
.
sketch,
.

2.13
,
Arduino :

if
if ... else
for
while
do ... while
switch ... case
break
continue
return
goto

( )
( )
( )
( )
( )
( )
( )
( )
( )
( )


=
+
*

( )
( )
( )
( )
15

21

2 : Arduino Uno

/
%

( )
( )


&&
||
!

( )
( )
( )


&
|
^
~
<<
>>

( )
( )
( )
( )
( )
( )


( )
++
-( )

+=, -=, *=, /=, %=
&=, |=, ^=, ~=, <<=, >>=

( )
( )


==
!=
<
>
<=
>=

()
()
()
()
( )
( )


*
&

( )
( )

HIGH
LOW
false
true
INPUT
OUTPUT
A0, ..., 5

( )
( )
( )
( )
( )
( )
( )

16

22

2 : Arduino Uno


boolean
( )
char
( 8 )
unsigned char
( 8 )
byte
( 8 )
int
( 16 )
unsigned int
( 16 )
word
( 16 )
long
( 32 )
( 32 )
unsigned long
float, double
( )
String
( )


char(), byte()
int(), word(), long()
float(), double()

pinMode()
( )

digitalWrite()
( )
digitalRead()
( )

( )
analogReference()
analogWrite()
( PWM )
analogRead()
( )

tone()
( )
noTone()
( )
shiftOut()
( )
pulseIn()
( s HIGH LOW)

millis()
micros()
delay()
delayMicroseconds()

( ms)
( s)
( - ms)
( - s)


max()
( )
min()
( )
abs()
( )
constrain()
( )
map()
( )
pow()
( )
17

23

2 : Arduino Uno

sqrt()
sin()
cos()
tan()

( )
( )
( )
( )


random()
( )
randomSeed()
( )

lowByte()
( byte )
highByte()
( byte )
bitRead()
( )
bitWrite()
( )
bitSet()
( 1 )
bitClear()
( 0 )
bit()
( 2)

attachInterrupt()
( )
detachInterrupt()
( )

interrupts()
( )
noInterrupts()
( )

Serial
( )

2.14 (Digital pins)


Arduino
, .
, Arduino (Atmega),
, .
:

pinMode(): /
.
: pinMode(pin, mode)
:
pin:
.
mode: INPUT/OUTPUT
18

24

2 : Arduino Uno

digitalWrite(): (HIGH) (LOW)


.
pinMode(), : 5V HIGH
0V LOW. , HIGH
digitalWrite() pullup-
20 LOW .
: digitalWrite(pin,value)
:
pin:
.
Value: INPUT/OUTPUT

digitalRead(): ,
HIGH LOW.
: digitalRead(pin)
:
pin:
.
: HIGH/LOW

2.15 (Analog input pins)


Atmega Arduino
-- 6 .
10 bit, 0 1023.
Arduino
,
/.
:

analogWrite(): (PWM ) .
LED
.
analogWrite(),
analogWrite() (
digitalWrite() digitalRead() ).
PWM 490 Hz. Arduino
3, 5, 6, 9, 10, 11.
: analogWrite(pin, value)
:
pin:
value: 0 255
19

25

2 : Arduino Uno

analogRead(): .
: analogRead(pin)
:
pin:
: 0 1023

2.16 (Libraries)

.
sketch, Sketch Import Library.
#include sketch.
sketch,
. sketch ,
.
,
libraries (),
sketchbook. .

Arduino.
( ):
Messenger -
.
NewSoftSerial - SoftwareSerial.
OneWire - ( Dallas Semiconductor)
one Wire.
PS2Keyboard - PS2.
Simple Message System - Arduino
SSerial2Mobile - e-mail
( AT )
Webduino - web server ( Arduino
Ethernet Shield)
X10 - X10
Xbee - XBees API
SerialControl - Arduino
Servo Servo.
:
Capacitive Sensing -
Debounce - (..
).

20

26

2 : Arduino Uno

LED:
Improved LCD library - LCD
Arduino LCD .
GLCD LCD KS0108 chipset.
LedControl - LED MAX7221
MAX7219.
LedControl - Matrix
LED.
LedDisplay - HCMS-29xx LED.
:
Tone -
.
PWM:
TLC5940 - 16 12 bit PWM .
:
DateTime -
.
MsTime2 - 2
.
:
Streaming - .


[1], [2], [3], [4] [5].

21

27

8: Processing

8
Processing

Processing

,
.
,
,
.
, , ,
Processing ,
.

8.1 Processing

/ GPL/LGPL.
,
GNU/Linux, Mac OS X Windows.
, (2D)
(3D) .
OpelGL 3D.
stand-alone desktop Web-based
(applets).
,
, , ..

Processing
Java,
.

8.2 Processing
Processing .

100

28

8: Processing

Wiring Arduino,
hardware. Wiring, Arduino ,
,
Processing AVR
Atmel. Processing : Design By
Numbers, Fritzing, Mobile Processing, Processing.js, Spde, Processing in Clojure
Processing Monsters.

8.3
Processing :
//
void setup()
{
//
}
void draw()
{
//
}

(Tabs)


9.1: (IDE) Processing

101

29

8: Processing

,
Processing. setup()
.
, , ,
.. setup()
, draw()
. draw() setup()

noLoop().

8.4
Processing
, .

, , , ,
, ..
http://processing.org/reference/libraries. ,
Tools,
.
Arduinoscope
http://code.google.com/p/arduinoscope/downloads/list, ControlP5
http://www.sojamo.de/libraries/controlP5/.

processing-1.5\modes\java\libraries ControlP5 arduinoscope
( ).
[22], [23].

102

9:


:
[] . (2009). , .
[] Banzi, M. (2009). Getting Started with Arduino. O'Reilly.
[C] (2011). .
(
)

Arduino.

:
Arduino
[1] http://arduino.cc/en/Guide/Environment?from=Tutorial.Bootloader
[2] http://arduino.cc/en/Guide/Windows
[3] http://arduino.cc/en/Reference/HomePage
[4] http://arduino.cc/en/Main/ArduinoBoardUno
[5] http://arduino.cc/en/Tutorial/Memory

[6] http://makeprojects.com/Wiki/Servos
[7] http://letsmakerobots.com/
[8] http://makeprojects.com/
L293D
[9]http://luckylarry.co.uk/arduino-projects/control-a-dc-motor-with-arduino-and-l293dchip/
[10]http://www.engineersgarage.com/electronic-components/l293d-motor-driver-ic
PWM
[11] http://en.wikipedia.org/wiki/Pulse-width_modulation
[12] http://arduino.cc/en/Tutorial/PWM
Ping
[13] http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor
[14] https://sites.google.com/site/parallaxinretailstores/home/ping-ultrasonic-distancesensor
Bluetooth
111

9:

[15] http://wiring.org.co/learning/tutorials/bluetooth/
[16] http://www.sparkfun.com/tutorials/67
[17] http://www.arduino.cc/playground/Learning/Tutorial01
PID Controller
[18]http://nefeli.lib.teicrete.gr/browse/stef/hlk/2011/KakolevaAggeliki,TsadakisAndreas/
attached-document-1323080192-956324-349/Kakoleva_Tsantakis2011.pdf
[19] http://en.wikipedia.org/wiki/PID_controller
[20] http://www.grobot.gr/phpBB3/viewtopic.php?f=5&t=14&st=0&sk=t&sd=a
[21] http://arduino.cc/playground/Code/PIDLibrary
Processing
[22] http://www.arduino.cc/playground/Interfacing/Processing
[23] http://www.processing.org/reference/
:
[24] http://arduino.cc/forum/
[25] http://www.instructables.com
[27]http://makeprojects.com/Project/Build-your-own-Arduino-Controlled-Robot-/577/

112

32

3

www.arduino.cc
ARDUINO IDE
o ARDUINO IDE ARDUINO UNO
o
o

3.1

:

Arduino Uno

BreadBroad

USB Arduino

: , LED,

33

3.2 1
Led 1sec,
/ . ( -
)

Arduino pin,
, .
, .
pin HIGH LOW. HIGH
'1' 5V DC, LOW '0' 0V DC
(ground).
pin
Arduino. led board

Blink
Arduino
: LED.

Arduino Board

breadboard

LED, 220,

, 220 pin 13.


() . ()
. Arduino , Arduino,
. Arduino LED pin 13.
LED , LED
.

34

Fritzing. ,
: Fritzing project page

35

// Pin 13 has an LED connected on most Arduino boards.


// give it a name:
int led = 13; // Integer () led
13
led.

// the setup routine runs once when you press reset:


void setup() { //
// initialize the digital pin as an output.
pinMode(led, OUTPUT); // led, pin 13
0-5V
}
// the loop routine runs over and over again forever:
void loop() { //
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
// 1 (5V) pin 13, led
delay(1000);
// wait for a second
// 1000ms
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
// 0 (0V) pin 13, led
delay(1000);
// wait for a second
// 1000ms
} // digitalWrite(led, HIGH);

[Get Code]

36

3.3 2
Led button polling

37

// constants won't change. They're used here to


// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
// Integer () buttonPin 2

button.
const int ledPin = 13; // the number of the LED pin
// Integer () ledPin 13

led.
// variables will change:
// variable for reading the pushbutton status
int buttonState = 0;
// ? Integer () buttonState
0
button (on-off)
void setup() { //
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT); // ledPin, pin 13
0-5V
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT); // buttonPin, pin 2
0-5V
}
void loop(){ //
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin); // Pin 2 (
button) buttonState
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) { // button , :
// turn LED on:
digitalWrite(ledPin, HIGH); // 1 (5V) pin 13, led
}
else { //:
// turn LED off:
digitalWrite(ledPin, LOW); // 0 (0V) pin 13, led
}
}

38

3.4 3
Led button Interrupt
( loop
)
const byte LED = 13; // Integer () LED
13
led.
const byte BUTTON = 2; // Integer () BUTTON
2
button.
// Interrupt Service Routine (ISR)
void pinChange () // pinChange (

{

noInterrupts (); //

if (digitalRead (BUTTON) == HIGH) // Pin 2 (
button) button ,
digitalWrite (LED, HIGH); // 1 (5V) pin 13, led
else //:
digitalWrite (LED, LOW); // 0 (0V) pin 13, led
interrupts (); //
} // end of pinChange
void setup () //
{
pinMode (LED, OUTPUT); // so we can update the LED
// LED, pin 13
0-5V
digitalWrite (BUTTON, HIGH); // internal pull-up resistor
// BUTTON, pin 2
0-5V
attachInterrupt (0, pinChange, CHANGE); // attach interrupt handler
//
. 0
. pinChange
ISR . LOW, RISING, FALLING,
CHANGE .

} // end of setup
void loop () //
{
// loop doing nothing
//
}

39

Below is a list of interrupts, in priority order, for the Atmega328:


1 Reset
2 External Interrupt Request 0 (pin D2)
(INT0_vect)
3 External Interrupt Request 1 (pin D3)
(INT1_vect)
4 Pin Change Interrupt Request 0 (pins D8 to D13) (PCINT0_vect)
5 Pin Change Interrupt Request 1 (pins A0 to A5) (PCINT1_vect)
6 Pin Change Interrupt Request 2 (pins D0 to D7) (PCINT2_vect)
7 Watchdog Time-out Interrupt
(WDT_vect)
8 Timer/Counter2 Compare Match A
(TIMER2_COMPA_vect)
9 Timer/Counter2 Compare Match B
(TIMER2_COMPB_vect)
10 Timer/Counter2 Overflow
(TIMER2_OVF_vect)
11 Timer/Counter1 Capture Event
(TIMER1_CAPT_vect)
12 Timer/Counter1 Compare Match A
(TIMER1_COMPA_vect)
13 Timer/Counter1 Compare Match B
(TIMER1_COMPB_vect)
14 Timer/Counter1 Overflow
(TIMER1_OVF_vect)
15 Timer/Counter0 Compare Match A
(TIMER0_COMPA_vect)
16 Timer/Counter0 Compare Match B
(TIMER0_COMPB_vect)
17 Timer/Counter0 Overflow
(TIMER0_OVF_vect)
18 SPI Serial Transfer Complete
(SPI_STC_vect)
19 USART Rx Complete
(USART_RX_vect)
20 USART, Data Register Empty
(USART_UDRE_vect)
21 USART, Tx Complete
(USART_TX_vect)
22 ADC Conversion Complete
(ADC_vect)
23 EEPROM Ready
(EE_READY_vect)
24 Analog Comparator
(ANALOG_COMP_vect)
25 2-wire Serial Interface (I2C)
(TWI_vect)
26 Store Program Memory Ready
(SPM_READY_vect)

Arduino pins
Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The table
below shows the available interrupt pins on various boards.
Board
int.0
int.1
int.2
int.3
Uno, Ethernet
2
3
Mega2560
2
3
21
20
Leonardo
3
2
0
1
Due
(see below)

int.4

int.5

19
7

18

The Arduino Due board has powerful interrupt capabilities that allows you to attach an interrupt function on all
available pins. You can directly specify the pin number in attachInterrupt().

3.5 4
, Led1
Led2 PWM
.

41

:
int sensorPin = A0; // select the input pin for the potentiometer
// Integer () sensorPin
0 ( 0
pin, 1 ADC)
.
int ledPin1 = 13;
// select the pin for the LED1
// Integer () ledPin1
13
led1.
int sensorValue = 0; // variable to store the value coming from the sensor
// Integer () sensorValue
0
ADC
int ledPin2 = 9; // LED2 connected to digital pin 9
// Integer () ledPin2
9
led2. PWM Output
int outputValue = 0;
// value output to the PWM (analog out)
// Integer () outputValue
0
led2
void setup() { //
// declare the ledPin1 as an OUTPUT:
pinMode(ledPin1, OUTPUT); //
ledPin1, pin 13 0-5V
pinMode(ledPin2, OUTPUT); //
ledPin2, pin 9 0-5V
}
void loop() { //
// read the value from the sensor:
sensorValue = analogRead(sensorPin); //
pin 0 ADC sensorValue
// turn the ledPin on
digitalWrite(ledPin1, HIGH); // 1 (5V) pin 13,
led1
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// ms
0 1023 ms
// turn the ledPin off:
digitalWrite(ledPin1, LOW); // 0 (0V) pin 13,
led1
// stop the program for for <sensorValue> milliseconds:

42

delay(sensorValue);
// ms
0 1023 ms
//fade section
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255); //
0 1023 0-255
outputValue
// change the analog out value:
analogWrite(ledPin2, outputValue); // PWM pin 9 (led2)
ADC 0 255
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2); // // 2ms
ADC
}


, .
analogWrite() 0 255, , analogWrite(255)
100% ,
analogWrite(127) 50% ( ) [12].

(PWM)

43

3.6 5
, Led1 Led2 ,
/

:
int sensorPin = A0;

// select the input pin for the potentiometer


// Integer ()
sensorPin 0 ( 0
pin, 1 ADC)

.
int ledPin1 = 13;
// select the pin for the LED1
// Integer () ledPin1
13
led1.
int sensorValue = 0; // variable to store the value coming from the sensor
// Integer () sensorValue
0
ADC
int ledPin2 = 9; // LED2 connected to digital pin 9
// Integer () ledPin2
9
led2.
int outputValue = 0;
// value output
// Integer () outputValue
0

void setup() { //
// declare the ledPin1 as an OUTPUT:
pinMode(ledPin1, OUTPUT); //
ledPin1, pin 13 0-5V

44

pinMode(ledPin2, OUTPUT); //
ledPin1, pin 9 0-5V
// initialize serial communications at 9600 bps:
Serial.begin(9600); // 9600
}
void loop() { //
// read the value from the sensor:
sensorValue = analogRead(sensorPin); //
pin 0 ADC sensorValue
// turn the ledPin on
if (sensorValue < 511 ) { // ADC 511
2,5 volt :
digitalWrite (ledPin1, HIGH); // 1 (5V) pin 13,
led1
digitalWrite (ledPin2, LOW);
// 0 (0V) pin 9,
led2
}
else //:
{
digitalWrite (ledPin1, LOW); // 0 (0V) pin 13,
led1
digitalWrite(ledPin2, HIGH); // 1 (5V) pin 9,
led2
}
//serial section
// print the results to the serial monitor:
//
Serial.print("\t sensor = " );
Serial.println(sensorValue);

// wait 2 milliseconds before the next loop


// for the analog-to-digital converter to settle
// after the last reading:
delay(2);

45

3.7 6
, EEPROM,
/ EEPROM

#include <EEPROM.h> //
EEPROM
// the current address in the EEPROM (i.e. which byte
// we're going to write to next)
int addr = 0; // addr
0

// start reading from the first byte (address 0) of the EEPROM


int address = 0; // address
0

byte value; // byte value

int sensorPin = A0; // select the input pin for the potentiometer
// Integer ()
sensorPin 0 ( 0
pin, 1 ADC)

.
int sensorValue = 0; // variable to store the value coming from the sensor
// Integer () sensorValue
0
ADC
void setup() { //
Serial.begin(9600); // 9600
}
void loop() { //
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
); // pin 0 ADC
sensorValue

46

// need to divide by 4 because analog inputs range from


// 0 to 1023 and each byte of the EEPROM can only hold a
// value from 0 to 255.
int val = analogRead(sensorPin) / 4; //
val pin 0
ADC 4 8 bit
// write the value to the appropriate byte of the EEPROM.
// these values will remain there when the board is
// turned off.
EEPROM.write(addr, val); //
addr, val
// advance to the next address. there are 512 bytes in
// the EEPROM, so go back to 0 when we hit 512.
addr = addr + 1; // ( )
if (addr == 512) // EEPROM
512 byte, :
addr = 0; //
delay(100); // 100ms
value = EEPROM.read(address); //
address, value
Serial.print(address); //
Serial.print("\t");
Serial.print(value, DEC); //

Serial.println();
// advance to the next address of the EEPROM
address = address + 1; // ( )
// there are only 512 bytes of EEPROM, from 0 to 511, so if we're
// on address 512, wrap around to address 0
if (address == 512) // EEPROM
512 byte, :
address = 0; //
delay(500); // 500ms
}

47

3.8 7
Twinkle Twinkle Little Star

:
int speakerPin = 9; // speakerPin
9
buzzer
int length = 15; // the number of notes
// length 15


char notes[] = "ccggaagffeeddc "; // a space represents a rest
// (char)
ccggaagffeeddc
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; //
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4

int tempo = 300; // tempo
300
()
void playTone(int tone, int duration) { // playTone
tone (khz) duration

for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);

48

delayMicroseconds(tone);

void playNote(char note, int duration) {// playNote


note duration o
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; //
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration); //
playTone
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}

// pause between notes


delay(tempo / 2);

49

3.9 8
Display LCD

50

:
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 6
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins

51

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}

....

52

53

arduino

arduino

54

ATmega328

55

Hitachi HD44780 driver

56

You might also like