Открыть Электронные книги
Категории
Открыть Аудиокниги
Категории
Открыть Журналы
Категории
Открыть Документы
Категории
2
004.3
78
.
78 Arduino. 65 . .: , 2017. 400 .:
. ( ).
ISBN 978-5-496-02421-1
Arduino? , -
, .
Arduino,
.
Arduino, -
. .
, .
32.973.23-018.2
004.3
No Starch Press. .
.
, , , -
. , ,
, .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2. Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . 39
3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
6. , . . . . . . . . . . . . . . . . . . . . . . . 134
7. . . . . . . . . . . . . . . . . . . . . . 173
8. Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
12. . . . . . . . . . . . . . . . . . . . . . . . . 253
14. . . . . . . . . . . . . . . . . . . . 300
15. . . . . . . . . . . 314
17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Windows XP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Ubuntu Linux 9.04 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2. Arduino IDE . . . . . . . . . . . . . . . . . . . 39
Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
7
3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
1: . . . . . . . . . 63
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
2: for . . . . . . . . . . . . . . . . 67
-
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
3: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
. . . . . . . . . . . . . . . . . . . . . . . . 73
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
8
4: . . . . . . . . . . . . . . . . . 86
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
#define . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
if-then-else . . . . . . . . . . . . . . . 93
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
6: . . . . . . . . . . . . . . . . . . 103
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Arduino . . . . . . . . . . . . . . . . . . . . . . 106
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
. . . . . . . . . . . . . . 107
. . . . . . . . . . . . 109
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
. . . . . . . . . . . . . . . . . 111
9
7: . . . . . . . . . . . . . . 111
8: . . . . . . . . . . . . . . . . . . . . . . 113
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
9: . . . . . . . . . 119
10: , . . . . . . 120
, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
11: ,
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Arduino . . 125
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
12: . . . . . . . . . . 126
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
while . . . . . . . . . . . . . . . . . . . . . . 128
do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Arduino . . . . . . . . . . . . . . . . . . . . . . . . 129
13: . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
14: long . . . . . . . . . . . . . . . . 131
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
6. , . . . . . . . . . . . . . . . . . . . 134
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
. . . . . . 135
15: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
10
. . . . . . . . . . . . . . . . . . . . . . 139
byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
. . . 141
16: . . . . . . . . . 142
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
74HC595 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
17: . . . . . . . . . . . . . . . . . . . . . . . . . 145
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
. . . . . . . . . . . . . . . . . . . . . . . . . . 150
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
18: . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
19: . . . . 156
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
20: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
(AND) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
(OR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
(XOR) . . . . . . . . . . . . . . . . . . . 165
(NOT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
21: . . . . . . . . . . . . . . . . . . . . . 166
11
22: . . . . . . . . . . . 168
23: . . . . . . . . 169
24: . . . . . . . . . . . . . . . . . . 171
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
7. . . . . . . . . . . . . . . . . . 173
. . . . . . . . . . . . . . . . . . . . . . . 173
. . . . . . . . . . . . . . . . . . . . . . . . . . 174
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
25: . . . . . . . . . . . . . . . . . 177
. . . . . . . . . . . . . . . . . . . . . . 179
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
26: . . . . . . . . . . 182
. . . . . . . . . . . . . . . . . . 183
27: . . . . . . . . . . . . . . . . . . . . . 184
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
8. Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
ProtoShield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
28:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
ProtoShield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
. . . . . . . . . . . . . . . . 196
12
. . . . . . . . . . . . . . . 196
microSD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
microSD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
29: . . . . . . . . . . . . . . . . . . . . . . 203
30: . . . . . . . . . . . . . . . . . 204
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
millis() micros() . . . . . . . . . . . . . . . . . . . . . . . . 207
31: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
32: . . . . . . . . . . . . . . . . . . . . . . . . . 214
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
switch-case . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
33: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
34: . . . . 224
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
13
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
35: . . . . . . . . . . . . . . . . . . . . . . . . . . 227
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
36: . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
12. . . . . . . . . . . . . . . . . . . . . . 253
. . . . . . . . . . 253
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
38: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
14
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
TIP120 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
39: . . . . . . . . . . . . . . . . . . . . . . 261
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
40: . . . . . . . . . . . . . 263
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
41:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
42: -
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
. . . . . . . . . . . . . . . . . . . . 282
43:
. . . . . . . . . . . . . . . . . . . . . . . . . . . 283
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
46:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
14. . . . . . . . . . . . . . . . 300
. . . . . . . . . . . . . . . . . . . 300
47: . . . . . . . . . . . . . . . . . . . 301
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
XBee
. . . . . . . . . . . . . . . . . . . . . . . 307
48: XBee . . . . . . . . . . . . . . . . . . . 308
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
. . . . . . . . . . . . . . . . . . . . . . . . . . . 309
49: . . . . . . . . . . . . 310
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
15. . . . 314
? . . . . . . . . . . . . . 314
- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
50: Arduino
- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
16
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
51: - . . . . . . . . 320
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
54: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
55: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
SPI- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
17
56: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
18. . . . . . . . . . . . . . . . . . . . . . . . . . . 351
RTC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
57: . . . . . . . . . . . . . 352
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
58: . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
59: . . . . . . . . . . . . . 360
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
60: . . . . . . . . . . . . . . . . 369
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
61: Arduino Tweeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . 376
62: Arduino .376
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . 379
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
18
20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
63: . . . . . . . . . . . . . . . . . . 388
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
64: . . . . . . . . . . . . . . . . . . . . . . 390
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
65:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
.
.
-
Arduino: (Massimo Banzi), (David
Cuartielles), (Tom Igoe), (Gianluca Martino)
(David Mellis). ,
.
(Marc
Alexander) , , , , ,
.
: Adafruit Industries, Keysight Technologies, Gravitech,
Freetronics, Oomlout, Seeed Studio, Sharp Corporation SparkFun. ,
Freetronics , -
. ,
Arduino, .
Fritzing -
,
.
, : -
(Iraphne Childs), (Limor Fried),
(Jonathan Oxer), (Philip Lindsay), (Nicole Kilah),
(Ken Shirriff), (Nathan Kennedy),
(David Jones) (Nathan Seidle).
, No Starch Press:
(Sondra Silverhawk) ,
(Serena Yang) , -
(Bill Pollock) , -
,
.
1
, - , ,
? ,
, , ? -
, ,
?
? -
?
Arduino (. 1.1) -
. C , Arduino
,
,
GPS .
2005 , Arduino -
. , ,
. 1.1. Arduino
22 1
. -
, ,
,
Arduino.
Arduino, -
. -
, ,
.
!
, , Arduino
, , -
, ,
.
, ,
.1.2.
. Arduino -
,
( 65).
,
.
. 1.2. SMS
, Arduino, ,
. ( -
http://www.labradoc.com/i/follower/p/project-sms-text-scroller.)
?
: ,
(reverse geocache box). -
,
23
(.1.3). ( -
GPS), , -
.
Arduino, GPS -
(. 13), ,
, , , ,
. -
.
,
. ( http://
learn.adafruit.com/reverse-geocache-engagement-box/.)
. 1.3. Arduino
. -
. , -
Arduino ,
, -
, .
, ,
, , , , GPS .
,
,
. , . 1.4,
.
, -
. ( http://www.
janspace.com/b2evolution/arduino.php/2010/06/26/scooterputer/.)
24 1
. 1.4. ( )
,
Nixie,
,
81 !
9 9, Arduino, -
. -
, ,
.
, , . 1.5.
( http://trashbearlabs.
wordpress.com/2010/07/09/nixie-sudoku/.)
Oomlout TwypeWriter
Arduino. Arduino Ethernet,
, Twitter
. -
. Arduino
, -
, . 1.6. (
http://oomlout.co.uk/blog/twitter-monitoring-typewritter-twypwriter/.)
, -
Arduino. ,
.
25
. 1.5. Nixie
. 1.6. TwypeWriter
26 1
Arduino .
,
Arduino, -
Arduino, ,
. Arduino Arduino
. Arduino- .
,
Arduino .
, , ,
Arduino Workshop : http://www.tronixforum.com/.
,
http://nostarch.com/arduino/.
, Arduino
,
. , ,
Arduino, , -
; -
, ,
. Arduino http://
arduino.cc/en/Main/Buy/.
( ), -
Arduino:
Adafruit Industries (http://www.adafruit.com/);
Altronics (http://www.altronics.com.au/);
DigiKey (http://www.digikey.com/);
Jameco Electronics (http://www.jameco.com/);
Jaycar (http://www.jaycar.com.au/);
Newark (http://www.newark.com/);
Oomlout (http://www.oomlout.co.uk/);
SparkFun Electronics (http://www.sparkfun.com/);
Tronixlabs (http://tronixlabs.com/).
27
, Arduino-
Freetronics (http://www.freetronics.com/).
, -
.
, , ,
, .
, Arduino
, , -
(Integrated Development Environment,
IDE). -
:
Mac OS X .
Windows XP 32- 64- .
Linux 32- 64- (Ubuntu ).
,
IDE. -
, , .
Arduino USB.
Arduino, IDE -
. IDE , ,
, ,
, , .
Mac OS X
Arduino IDE
MacOS X.
IDE
IDE Mac OS,
:
1. -, Safari, http://arduino.cc/
en/Main/Software/, . 1.7.
2. Mac OS X.
Downloads (), . 1.8.
28 1
. 1.8.
3. ,
. ,
Open ().
.
4. Arduino Applications ()
. ,
.
5. Arduino USB.
, . 1.9.
. 1.9. Arduino .
Eleven Uno
29
IDE
IDE , -
IDE:
1. Applications () Finder (-
. 1.10) Arduino.
. 1.10. Applications ()
2. , , -
. Open (), .
Arduino IDE, . 1.11.
3. , , Arduino
IDE . , -
Arduino. ToolsSerial
Port () /dev/tty.usbmodem1d11 ,
. 1.12.
4. Arduino.
, Arduino -
. ,
Uno, ToolsBoardArduino Uno (
Arduino Uno), . 1.13.
Arduino 11.
. -
. 38.
30 1
. 1.12. USB
. 1.13. Arduino
31
Windows XP
Arduino IDE, -
IDE Windows.
IDE
Arduino IDE Windows, -
:
1. -, Firefox, http://arduino.
cc/en/Main/Software/, . 1.14.
2. Windows. ,
. 1.15. Open with Windows Explorer (
( )) OK. ,
. 1.16.
3. ,
, . 1.17.
4. arduino-1.0.6-windows ( , ) -
, . ,
, Arduino, -
. 1.18.
.
32 1
. 1.15.
. 1.16. Firefox
. 1.17. IDE
33
USB Arduino. :
1. Arduino USB. -
: Device driver software not successfully installed (
). .
2. Windows Control Panel ( Windows).
Device Manager ( ) ,
Arduino, . 1.19.
. 1.19.
...). Browse my
computer for driver software (
-
) -
. Browse
For Folder ( -
); Browse
() drivers,
-
Arduino IDE (
. 1.20). OK.
4. Next () -
,
.
cannot verify the publisher of the driver
software ( -
), Install
this software anyway ( -
- . 1.20. drivers
).
Windows , , COM-,
Arduino, . 1.21.
. 1.21.
IDE
IDE.
1. Arduino IDE. , -
Arduino, ToolsSerial
35
Port () COM ,
Update Driver Software ( ...).
2. Arduino.
, Arduino -
. , -
Uno, Tools Board Arduino Uno
(Arduino Uno), . 1.13. -
Arduino 11.
Arduino IDE . . 38.
IDE
, IDE:
1. -, Firefox, http://arduino.
cc/en/Main/Software/, . 1.22.
2. IDE,
1.0.6.
. 1.23.
3. ,
. 1.24. arduino-1.0.6-windows ( , -
) , , .
. 1.24. IDE
IDE
IDE.
1. Arduino USB.
Arduino IDE, arduino-1.0.6, -
, arduino,
. 1.25.
37
2. , . 1.26, Run
(), Arduino IDE,
1.27.
. 1.26. IDE
3. IDE ,
Arduino. ToolsSerial Port ()
/dev/ttyACMx , x (
).
4. Arduino.
, Arduino -
. , -
Uno, Tools Board Arduino Uno
(Arduino Uno), . 1.13. -
Arduino 11.
.
, -
. ,
,
, .
-
. -,
. ,
.
,
, .
65 Arduino,
. ,
- . , !
2 Arduino
IDE
Arduino
Arduino? - Arduino (http://www.
arduino.cc/), -
,
. , -
, ,
.
, Arduino , -
. Arduino, Uno,
, . 2.1.
, -
, .
, , , -
, ,
, . -
, ,
,
, ,
40 2
.
-
Arduino.
, Arduino Uno
( ) , . -
, ,
, , .
Uno . -
, . 2.2.
(Universal Serial Bus, USB). ,
,
. -
.
.
, , ,
. 2.3.
Arduino IDE 41
. 2.2. USB
. 2.3.
Arduino, , -
, ,
.
, , . 2.4.
. 2.4.
. ,
42 2
. ,
A4 A5 .
, . 2.5.
. 2.5. /
( ) 0 13 /.
. 0 1,
, -
, ,
USB. , (~), -
, , ,
.
, -
. , ,
. Arduino : , ON,
-
, , ,
. 2.6.
. 2.6.
Arduino IDE 43
RX TX ,
Arduino -
USB. L ( -
/ 13).
,
USB.
, .
, . 2.7 RESET
().
,
Arduino -
.
-
,
Arduino.
RESET (. . 2.7) . 2.7. RESET ()
Arduino ,
.
Arduino ,
.
Arduino
, Arduino. -
, . 2.8 Ethernet,
Arduino .
, Ethernet .
. ,
. 2.9 ,
, -
, .
,
, .
, .
8.
Arduino ,
, . -
, :
(Integrated Development Environment, IDE),
, .
Arduino.
44 2
. 2.9.
Arduino IDE 45
. 2.10, Arduino IDE
. IDE : -
, .
( . 2.10) ,
. (sketch_jun28a)
IDE (Arduino 1.0.5). (File (), Edit (-
), Sketch (), Tools () Help ())
.
, -
:
46 2
File () , ;
, ,
Preferences ();
Edit () , ,
;
Sketch () ,
;
Tools () ,
Arduino USB;
Help () IDE.
.
, -
. :
Verify ()
;
Upload ()
Arduino;
New () ;
Open () ;
Save () . ,
;
Serial Monitor ( )
Arduino IDE.
( . 2.10)
.
. ( ,
.) ,
.
( . 2.10)
IDE.
Arduino IDE 47
, ,
.
Arduino USB, , Arduino Uno
on COM4.
IDE
Arduino , -
; , .
, -
(. 2.11),
.
. 2.11. L Arduino
, ,
. , Arduino -
, , - , .
Arduino USB. -
IDE, ToolsSerial Port ()
USB. ,
.
, . -
, -
.
,
. Arduino, ,
;
, .
, -
, :
48 2
// . : , 01.07.13
, -
, , . ( ,
, IDE
.)
, , /*
, , */ ,
, :
/*
: , 01.07.13
*/
, /*
*/ , ,
.
,
FileSave As ( ...).
(, blinky) OK.
.ino
.
blinky.ino .
setup
void setup().
, Arduino
. setup,
:
void setup()
{
}
L Arduino.
/ 13.
/ ,
. ,
. , , -
Arduino IDE 49
/.
.
({ }):
pinMode(13, OUTPUT); // 13
13 . -
OUTPUT, ( output)
. -
, INPUT. ,
pinMode() (;).
Arduino .
, .
loop
, ,
. loop,
Arduino , -
RESET ().
, ,
loop.
(}) .
/*
: , 01.07.13
*/
void setup()
{
pinMode(13, OUTPUT); // 13
}
void loop()
{
// , :
}
Arduino IDE , -
.
void loop() .
,
loop, Verify (), -
, :
50 2
digitalWrite(13, HIGH); // 13
delay(1000); //
digitalWrite(13, LOW); // 13
delay(1000); //
. digitalWrite()
, :
13, L.
(HIGH) , (high) -
; L ,
. ( LOW,
, .)
delay(1000) -
1 . delay()
1000 , 1 .
digitalWrite(13, LOW);.
delay(1000); ,
.
:
/*
: , 01.07.13
*/
void setup()
{
pinMode(13, OUTPUT); // 13
}
void loop()
{
digitalWrite(13, HIGH); // 13
delay(1000); //
digitalWrite(13, LOW); // 13
delay(1000); //
}
, !
, -
Arduino. Verify
() IDE . ,
, . 2.12.
Arduino IDE 51
. 2.12.
Done compiling ( ) ,
Arduino. ,
( 1076 ) 32256 .
, ? , ,
delay(1000). -
,
, . 2.13.
. 2.13.
, void loop, -
, , IDE, (blinky:16 -
: 16 blinky), (expected ';'
before '}' token }). , -
,
. .
, , , -
Arduino Upload ()
IDE. -
52 2
. TX/RX
(.2.6) , Arduino .
: Arduino .
, L -
.
! , , Arduino.
,
,
. IDE -
, ,
, . ,
,
delay , 250 , :
delay(250); //
. , -
.
, , -
Arduino, ,
,
.
3
99 ;
99 ;
99 : , , -
, ;
99 ;
99 , for
, .
Arduino. , -
Arduino, , -
, .
. ,
,
. ,
. , -
Arduino.
-
, . ,
. ,
Arduino ,
Arduino ,
, - .
54 3
, -
, .
Arduino :
1. . , ,
.
2. . ,
. ,
.
3. . , -
Arduino.
4. . ,
Arduino, .
5. .
, Arduino.
6. . - ? -
, -
, .
, -
.
-
. , -
.
, -
. .
,
Arduino.
, ,
, . -
, : ,
.
.
,
55
, -
, , .
. .
(ground, GND).
().
(): 1000 1 .
. (). -
, .
,
.
(). , 100 , -
60,
.
,
:
() = () ().
, , , ,
. -
,
.
, ,
, , , -
, .
, .
, -
.
, Arduino, -
10 . -
56 3
, ,
.
, , -
. ,
, .
-
. . 3.1 .
. 3.1.
. -
() (,
) (, ).
, ( -
) .
, ,
, .
, ,
.
. .
. .
. ( )
( ).
. .
. -
().
. 3.1 ,
.
57
3.1. ,
0
1
2
3
4
5
6
7
8
9
.
.
-
,
.
1%, 5% 10%.
. 3.2 .
, 4, 7 3 -
, . 3.1.
47 103=47000 , 47 . , , , -
, 1%.
. 3.2.
, , -
- , . 3.3. -
, ,
. , . 3.3 10000 ,
10.
58 3
. 3.3.
- (-
, 01C), Google EIA-96,
.
, -
, , -
. , . 3.4 ,
.
. 3.4.
560 1%
,
. , -
, ,
.
, -
, . ,
. 3.1, 1/4
Arduino.
59
, , -
. / ,
.
. ,
. 3.5, 5 , 26 7,5 .
. 3.5. 5
, -
. ,
. . 3.6 .
. 3.6. 5
,
; -
.
() (), . 3.7.
( -
) .
, , -
, . , , ,
, . 3.8.
, .
, 1,7
5 20 . ,
60 3
LED1
(633 )
. 3.7.
. 3.8. ( )
( )
Arduino 5 . ,
, , -
. ?
.
-
:
R = (Vs Vf) / I,
Vs (Arduino 5 ); Vf
(, 1,7 ) I , -
(10 ). ( I , 10
0,01 .)
Vs = 5 , Vf = 1,7 I = 0,01 .
330 . -
10 .
, , -
, ,
, 560 -
1/4 , 6 .
,
!
61
, :
(V) = (I) (R)
, .
, . 3.9.
V V=IR
I=VR
R=VI
I R
. 3.9.
,
, . ,
, R,
, . ,
V, , -
.
,
. -
() .
, .
, , . 3.10.
,
.
-. , , -
. 3.11, ,
. -
, . ,
.
,
.
-
, .
.
. 3.12.
62 3
. 3.10.
. 3.11.
63
. 3.12.
1:
.
KITT Knight
Rider1.
:
1. 1.
2. .
3. 1.
4. 2.
5. .
6. 2.
1
. . .
64 3
7. , 5,
, 5 1.
8. .
, :
.
560 .
.
.
Arduino USB.
2- 6- 560-
.
. :
// 1 -
void setup()
{
pinMode(2, OUTPUT); // ,
pinMode(3, OUTPUT); // ,
pinMode(4, OUTPUT); //
pinMode(5, OUTPUT); //
pinMode(6, OUTPUT);
}
void loop()
{
digitalWrite(2, HIGH); // 1
delay(500); //
digitalWrite(2, LOW); // 1
digitalWrite(3, HIGH); //
delay(500); // 2- 5-
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
65
delay(500);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
delay(500);
digitalWrite(3, LOW);
// loop()
//
}
void setup() ()
, -
. void loop() () ,
, digitalWrite().
. .
. 3.13.
-
, . , digitalWrite(2, HIGH),
. 3.13. 1
66 3
2 5 , -
, ,
GND (), . ,
digitalWrite(2, LOW), .
Arduino . - -
, .
!
, ,
, . , -
. ,
, .
, Arduino,
. , , -
,
delay(500). .
.
, 1 delay(500), -
.
. -
, .
, ,
delay().
1 void setup(),
:
int d = 250;
250 d.
500 d. ,
, Arduino delay() d.
, ,
250.
int ,
(integer) 32768 32767.
, . , , -
67
. ,
100, :
int d = 100;
. -
HIGH LOW. . ;
, .
2:
for
. , , -
,
,
Arduino. for.
for ,
.
for :
// 2 - for
int d = 100;
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
for ( int a = 2; a < 7 ; a++ )
{
digitalWrite(a, HIGH);
delay(d);
digitalWrite(a, LOW);
delay(d);
}
}
for , , -
, .
, a, -
68 3
2. a++ -
1. , a
7 (). a , 7, Arduino
, for.
for , -
.
2 for:
for ( int a = 5 ; a > 1 ; a-- )
{
digitalWrite(a, HIGH);
delay(d);
digitalWrite(a, LOW);
delay(d);
}
for () a 5 -
1 a--. ,
a 1 (a > 1), , a 1
.
1, .
!
-
digitalWrite(),
, , -
,
- ( Pulse-Width Modulation, PWM).
-
, 500 .
, ,
, , -
. 50
, , .
( ,
, , , ), -
, .
. 3.14 .
, , , .
, , , -
.
69
5
100%-
0
5
60%-
0
5
40%-
0
5
20%-
. 3.14.
3, 5, 6, 9, 10 11 Arduino
. (~), . 3.15.
. 3.15. (~)
analogWrite(x, y), x -
, y ,
0 255, 0 0%, 255 100%.
3:
2. IDE
Arduino:
// 3 -
int d = 5;
void setup()
70 3
{
// , 3,
//
pinMode(3, OUTPUT);
}
void loop()
{
for ( int a = 0 ; a < 256 ; a++ )
{
analogWrite(3, a);
delay(d);
}
for ( int a = 255 ; a >= 0 ; a-- )
{
analogWrite(3, a);
delay(d);
}
delay(200);
}
, 3,
-
. , ,
, .
. , -
.
, , -
, , -
. , ,
Arduino Uno 40 200
. Arduino -
, - .
40 200
, .
, -
, . ,
. ,
71
Arduino Uno.
Arduino.
BC548, . 3.16.
Q1
C
B
E
. 3.16. : BC548
,
.
( . 3.16), BC548
( ) , . ( ,
, , BC548
.)
, , Arduino,
.
, .
BC548 100
30 , Arduino
. , ,
.
, -
.
,
. (. 3.17).
1N4004.
,
. ,
, -
0,7 . 1N4004
72 3
1 400 ,
. , .
D1
1N4004
. 3.17. 1N4004
, ,
.
, Arduino
. ,
,
. :
(. 3.18).
NO NC
COM
. 3.18.
,
, .
, ,
73
.
,
. ,
, , .
, , , -
.
, . 3.19.
. 3.19.
, 12- .
, .
10 Arduino -
1 . , ,
. , -
( ): ,
.
12 . -
GND
Arduino. , 1N4004
74 3
.
, ,
, .
. ,
, -
- .
, .
Arduino -
.
,
(110250 ), , .
.
. , -
.
,
, Arduino. -
, , , .
4 . -
, , , . ,
, !
4
99 -
;
99 ;
99 ;
99, ;
99 if-then-else;
99, ;
99
;
99 ,
;
99 ,
.
Arduino. -
, , -
( ) ,
. Arduino,
, . -
.
76 4
3 , , -
, . -
-
,
.
,
( ). . 4.11
.
. 4.1.
, -
, .
-
.
, .
, ,
.
Arduino
. 4.2 , Arduino. ,
.
. -
1
..
77
N/C
IO REF
AREF
RST
A5
A4
A3
A2
A1
A0
3V3 5V
Arduino
GND
V in
/
D10
D11
D12
D13
SDA
SCL
D0
D1
D2
D3
D4
D5
D6
D7
D8
D9
PWM
PWM
PWM
PWM
PWM
PWM
RX
TX
. 4.3 , .
R1
220
( 220 R1).
. 4.3.
.
, R, 220 R.
D1
1N4004
. 4.4 , -
.
+
3,
-
. . 4.4 ,
. , ,
, -
. 4.4.
. -
, , ,
.
78 4
. 4.5 , .
: .
, -
.
LED1
(633 )
. 4.5.
. 4.6 , .
BC548.
( C)
(collector), ( B) (base)
( E) (emitter).
, , ,
NPN-, -
. (
PNP-
.)
Q,
R.
Q1
C
. 4.6.
79
. 4.7 , .
- -
, . -
.
. COM (common ),
NO (normally open ) NC (normally
closed ).
,
, COM NC.
COM NO.
NO NC
COM
. 4.7.
-
, .
, -
, ,
. 4.8. ;
.
. 4.8.
80 4
-
, ,
. 4.9. . 4.9.
,
(GND)
, . 4.10.
, . 4.10. ,
GND Arduino.
, ,
, , 1. ,
.
. 4.11 . 3.13: ,
.
-
, .
, -
Fritzing, http://www.
fritzing.org/.
, . -
, ,
. -
, -
( , ),
.
, ,
, . ,
,
.
81
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
D8
/
R1
D7 560
PWM R2
D6 560
PWM
R3
D5 560
R4
A0 D4 560
R5
PWM
A1 D3 560
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
LED5 LED4 LED3 LED2 LED1
SDA
GND
. 4.11. 1
(, pF) , -
(, F) . -
, .
,
, 10 ,
,
, . -
10, 16, 25 50 .
, .
,
. , ,
.4.12, 104. 10, ,
100000 (), 100 () 0,1 -
().
82 4
. 4.12. 0,1
, -
http://www.justradios.com/uFnFpF.html.1
:
.
, , . 4.12,
.
.
, . 4.13.
100nF
. 4.13.
, -
, ,
.
http://
1
www.translatorscafe.com/cafe/RU/units-converter/electrostatic-capacitance/. . .
83
, , . 4.14,
, , ,
. (+),
() . . 4.14 -
(), . ,
.
.4.14 20% 100 .
. 4.14.
,
1F
4.15; + -
.
. 4.15.
-
. , -
,
,
. , ,
.
, Arduino,
.
3 / -
, .
, ,
.
84 4
, :
.
, . 4.16.
. ,
,
, , .
. 4.16.
, . 4.16 -
23 25. ,
. ,
. 4.17.
- S. ,
.
S1
. 4.17.
85
,
, ,
. - , -
,
.
-
, , -
. , . 4.18, ,
.
. 4.18.
. 4.18 . -
, . 4.18 ,
(5 ), .
Stop -
. . 4.18
. A , 0 .
, - ,
5 B, . , - -
, C,
.
.
86 4
4:
:
1. , .
2. ,
.
3. , .
4. .
, :
.
.
560 .
10 .
100 .
.
.
Arduino USB.
, . 4.19.
, 10 GND 7.
, ,
. , 100 ,
10 , ,
, -
.
87
. , -
10 , 100
. , ,
. 0
.
3V3 5V V in
D13
R1
RST D12 560
PWM
AREF D11
D8
D7
PWM
D6 LED1
PWM C1
D5 R2 100nF
10k
A0 D4
PWM
A1 D3
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 4.19. 4
,
, ; ,
:
1. , . 4.20.
2. 90
10 , , -
. 4.21.
88 4
. 4.20.
10
. 4.21. , 10
3. 5 V Arduino
.
GND Arduino , -
, 5 V.
GND
. . 4.22.
4. 7 Arduino
, . 4.23.
5. ()
GND, ()
. -
560 , . 4.24.
89
(GND)
5 (5V)
. 4.22. : 5 () ()
. 4.23.
90 4
560
. 4.24. 560
6. 560
12 Arduino, . 4.25.
. 4.25. Arduino
, ,
.
.
91
, 4.1, .
4.1.
// 4 -
#define LED 12
#define BUTTON 7
void setup()
{
pinMode(LED, OUTPUT); //
pinMode(BUTTON, INPUT); //
}
void loop()
{
if ( digitalRead(BUTTON) == HIGH )
{
digitalWrite(LED, HIGH); //
delay(500); // 0.5
digitalWrite(LED, LOW); //
}
}
, ,
.
, ,
,
3. ( ;
.)
, 4: -
#define, if-then.
#define
void setup() #define (),
: IDE -
. , IDE
LED , 12.
92 4
#define
/, . -
, #define .
, (,
) .
, -
, . -
LED ; ,
#define.
, void setup()
:
pinMode(BUTTON, INPUT); //
, , -
5 ( ), digitalRead(pin),
pin , .
HIGH ( 5 ) LOW (-
0 ).
if
if ,
, . , 4
, 4.2.
4.2. if-then
// 4.2
if (digitalRead(BUTTON) == HIGH)
{
digitalWrite(LED, HIGH); //
delay(500); // 0,5
digitalWrite(LED, LOW); //
}
4.2 if.
( HIGH ), ,
.
( digitalRead(BUTTON)
HIGH), (==). == -
!= ( ),
. , .
93
(=), -
, (==), ,
. , if
!
if-then-else
if, else,
. , 4.1, else,
4.3, , ,
. else Arduino
, if
.
4.3. else
// 4.3
#define LED 12
#define BUTTON 7
void setup()
{
pinMode(LED, OUTPUT); //
pinMode(BUTTON, INPUT); //
}
void loop()
{
if ( digitalRead(BUTTON) == HIGH )
{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
}
, -
, / /. ()
, -
: (0, ) (1, ).
:
94 4
: true false. ,
:
boolean raining = true; // "raining" ()
// true
-
, :
raining = false;
-
if. true false,
!= ==. :
if ( raining == true )
{
if ( summer != true )
{
// ,
}
}
.
(!), (&&) (||).
(!).
, . -
:
if ( !raining )
{
// (raining == false)
}
&&. -
if. :
||. ,
:
if . :
.
, if-then.
, (!), -
== true ==
false. , :
,
, .
, .
5:
-
. ,
96 4
, . -
- - ,
, ,
. ,
, , -
. , .
,
Arduino.
. -
.
, , -
.
-
.
, .
, , ,
, , , .
( )
, -
, .
, ,
, . ,
, -
, .
, ,
.
, -
:
(LED1 LED2).
(LED3 LED4).
(LED5 LED6).
97
560 (R1R6).
10 (R7 R8).
100 (C1 C2).
(S1 S2).
.
Arduino USB.
.
-
, ,
. 4.26.
C1
S1 100nF
R7
10k
3V3 5V V in LED1
D13
R1
560
RST D12 LED3
R2
PWM 560
AREF D11 LED5
R3
IO REF Arduino D10
PWM 560
PWM
N/C D9
/
D8
D7
PWM C2
D6 100nF
PWM S2
D5
A0 D4 R8
10k
PWM
A1 D3 LED2
R4
560
A2 D2 LED4
R5
TX 560
A3 D1 LED6
R6
RX 560
A4 D0
A5 SCL
SDA
GND
. 4.26. 5
4
, .
98 4
, -
: , -
GND Arduino,
. 4.27.
. 4.27.
. -
?
// 5 -
// :
#define westButton 3
#define eastButton 13
#define westRed 2
#define westYellow 1
#define westGreen 0
#define eastRed 12
#define eastYellow 11
#define eastGreen 10
99
void setup()
{
// /
pinMode(westButton, INPUT);
pinMode(eastButton, INPUT);
pinMode(westRed, OUTPUT);
pinMode(westYellow, OUTPUT);
pinMode(westGreen, OUTPUT);
pinMode(eastRed, OUTPUT);
pinMode(eastYellow, OUTPUT);
pinMode(eastGreen, OUTPUT);
// -
digitalWrite(westRed, LOW);
digitalWrite(westYellow, LOW);
digitalWrite(westGreen, HIGH);
digitalWrite(eastRed, HIGH);
digitalWrite(eastYellow, LOW);
digitalWrite(eastGreen, LOW);
}
void loop()
{
// ?
if ( digitalRead(westButton) == HIGH )
{
// ,
//
if ( trafficWest != true )
{
trafficWest = true; // ->
delay(flowTime); //
digitalWrite(eastGreen, LOW); //
digitalWrite(eastYellow, HIGH); // ,
delay(changeDelay); //
digitalWrite(eastYellow, LOW);
digitalWrite(eastRed, HIGH);
delay(changeDelay);
for ( int a = 0; a < 5; a++ ) //
{
digitalWrite(westYellow, LOW);
delay(yellowBlinkTime);
digitalWrite(westYellow, HIGH);
delay(yellowBlinkTime);
}
100 4
digitalWrite(westYellow, LOW);
digitalWrite(westRed, LOW); //
digitalWrite(westGreen, HIGH); //
}
}
// ?
if ( digitalRead(eastButton) == HIGH )
{
// ,
//
if ( trafficWest == true )
{
trafficWest = false; // ->
delay(flowTime); //
digitalWrite(westGreen, LOW);
//
//
digitalWrite(westYellow, HIGH);
delay(changeDelay);
digitalWrite(westYellow, LOW);
digitalWrite(westRed, HIGH);
delay(changeDelay);
for ( int a = 0 ; a < 5 ; a++ ) //
{
digitalWrite(eastYellow, LOW);
delay(yellowBlinkTime);
digitalWrite(eastYellow, HIGH);
delay(yellowBlinkTime);
}
digitalWrite(eastYellow, LOW);
digitalWrite(eastRed, LOW); //
digitalWrite(eastGreen, HIGH); //
}
}
}
()
/ .
,
. trafficWest ()
true ( ) false ( ).
, trafficWest ,
true false. ,
, ( , ) , -
. .
101
flowTime () , -
. ,
, , , -
, , .
changeDelay ()
.
, .
,
if ( trafficWest == true )
, -
.
/ -
, .
,
.
, . , digital
Write(pin, HIGH) digitalWrite(pin, LOW), ,
digitalRead(), (HIGH) (LOW)
. . 4.28 ,
.
,
. , . 4.29 -
. ,
.
Arduino 5 , 0 .
(. 4.30), -
.
0 5 .
analogRead(), 0 1023,
, .
102 4
. 4.28. HIGH,
, LOW
. 4.29.
, analogRead()
A0 a:
a = analogRead(0); // 0 (A0)
// 0 1023,
// 0,000 4,995
6:
, -
, AA,
AAA, C D, ,
. 5 ,
Arduino .
.
, AA,
1,6 , .
. analogRead()
. ,
, 5 , , 5 1024 ( -
), 0,0048,
. analogRead()
512, , 0,0048, 2,4576 .
:
1. .
2. 0,0048,
.
3. 1,6 ,
.
4. 1,4 1,6 , -
.
5. 1,4 ,
.
6. .
104 4
, :
560 (R1R3).
(LED1).
(LED2).
(LED3).
.
.
Arduino USB.
. 4.31 -
. , + .
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
/
D8
D7 R3
PWM 560 LED3
D6
PWM
D5 R2
560 LED2
+ A0 D4
PWM
A1 D3 R1
560 LED1
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 4.31. 6
105
, , -
.
(+), ().
5 -
,
Arduino.
:
// 6 -
#define newLED 2 // ''
#define okLED 4 // ''
#define oldLED 6 // ''
int analogValue = 0;
float voltage = 0;
int ledDelay = 2000;
void setup()
{
pinMode(newLED, OUTPUT);
pinMode(okLED, OUTPUT);
pinMode(oldLED, OUTPUT);
}
void loop()
{
analogValue = analogRead(0);
voltage = 0.0048*analogValue;
if ( voltage >= 1.6 )
{
digitalWrite(newLED, HIGH);
delay(ledDelay);
digitalWrite(newLED, LOW);
}
else if ( voltage < 1.6 && voltage > 1.4 )
{
digitalWrite(okLED, HIGH);
delay(ledDelay);
digitalWrite(okLED, LOW);
}
else if ( voltage <= 1.4 )
{
digitalWrite(oldLED, HIGH);
delay(ledDelay);
digitalWrite(oldLED, LOW);
}
}
106 4
6 0 ()
(). float () -
. ,
if-else, - ,
, .
Arduino
, Arduino -
, , , . -
:
a = 100;
b = a + 20;
c = b - 200;
d = c + 80; // d 0
, -
float. 3,4028235
1038 3,4028235 1038, -
. .
, f a
g:
int a = 100;
float f;
float g;
f = a / 3; // f = 33.333333
g = a + f; // g = 133.333333
== != if -
5, .
,
:
< ;
> ;
107
<= ;
>= .
, , 6, -
.
6, analogRead() , -
0 5 . (5 ),
, -
, Arduino -
1023.
,
. , -
5 , analogRead()
0 1023. ()
2 , Arduino 01023
0 2 , .
, .
AREF (analog
reference ), . 4.32.
,
AREF Arduino,
GND. , -
, , ,
Arduino Uno, 5 .
108 4
, . 4.33.
100nF
R1 R2
N/C
IO REF
AREF
RST
A5
A4
A3
A2
A1
A0
3V3 5V
Arduino
GND
V in
/
D10
D11
D12
D13
SDA
SCL
D0
D1
D2
D3
D4
D5
D6
D7
D8
D9
PWM
PWM
PWM
PWM
PWM
PWM
RX
TX
. 4.33.
-
R1 R2 :
R2
Vout Vin ,
R1 R 2
Vout , Vin ,
5 ; R1 R2 .
Vin ,
R1 R2 , 10 .
,
, 1%;
. , -
100 AREF GND,
AREF .
-
void setup():
analogReference(EXTERNAL); // AREF
//
109
analogReference(INTERNAL); //
// 1,1
, ,
0 .
,
. 4.34.
: -
.
,
.
. -
, -
, -
.
,
. Arduino
, , . 4.35.
. 4.34. . 4.35.
()
110 4
,
(. 4.36). -
,
, .
. 4.36.
, .
, , .
, , , , . 4.36,
.
( )
, -
,
. . 4.37 TDK PS1240 25-
1, .
25- (24,3 )
1
(25 ). . .
111
,
. ,
, .
, -
, , .
.
, , (. 4.38), -
.
. 4.38.
, ,
; . 4.38,
, . , -
Arduino.
7:
, -
Arduino:
// 7 -
#define PIEZO 3 // 3
//
int del = 500;
void setup()
{
pinMode(PIEZO, OUTPUT);
}
112 4
void loop()
{
analogWrite(PIEZO, 128); //
// 50%
delay(del);
digitalWrite(PIEZO, LOW); //
delay(del);
}
- .
analogWrite(),
128, 50% (),
.
3V3 5V V in
D13
RST D12
12V
PWM
AREF D11
PWM
N/C D9
D8 R2
1k
/
D7
PWM
D6
PWM
D5
A0 D4 R1 Q1
1k
PWM
A1 D3
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 4.39. 7
113
, , -
. 5 ,
, 9 12.
Arduino ,
, 9- , -
.
, . 4.39.
, 12 V, -
,
GND Arduino.
8:
, , -
TMP36 (. 4.40),
Analog Devices (http://www.analog.com/tmp36/).
. 4.40. TMP36
, TMP36 BC548,
3. TMP36 ,
,
. , 25C
750 , 1
10 . TMP36
40C 125C.
analogRead() 0 1023, -
0 () 5000 (5 ).
analogRead() (5000/1024), ,
. 500 (, -
TMP36, -
), 10. .
,
1,8 32.
114 4
TMP36
. +20C,
. +20C +26C -
, +26C,
.
, :
560 (R1R3).
.
(LED1).
(LED2).
(LED3).
TMP36.
.
.
Arduino USB.
.
TMP36 ,
5 , , -
, (GND),
. 4.41.
// 8 -
// :
#define HOT 6
#define NORMAL 4
#define COLD 2
115
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
/
D8
D7
R1
PWM 560 LED1
D6
+V s PWM
D5
R2
TMP Vout A0 D4 560 LED2
36
PWM
A1 D3
GND R3
560 LED3
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 4.41. 8
float voltage = 0;
float celsius = 0;
float hotTemp = 26;
float coldTemp = 20;
float sensor = 0;
void setup()
{
pinMode(HOT, OUTPUT);
pinMode(NORMAL, OUTPUT);
pinMode(COLD, OUTPUT);
}
void loop()
{
//
//
sensor = analogRead(0);
voltage = (sensor*5000)/1024; //
voltage = voltage-500; //
116 4
celsius = voltage/10; //
//
if ( celsius < coldTemp )
{
digitalWrite(COLD, HIGH);
delay(1000);
digitalWrite(COLD, LOW);
}
else if ( celsius > coldTemp && celsius <= hotTemp )
{
digitalWrite(NORMAL, HIGH);
delay(1000);
digitalWrite(NORMAL, LOW);
}
else
{
// celsius > hotTemp
digitalWrite(HOT, HIGH);
delay(1000);
digitalWrite(HOT, LOW);
}
}
, TMP36, -
(). if-else ( )
,
. delay(1000) -
,
.
,
. ,
PowerSwitch Tail,
. 4.42.
PowerSwitch Tail , -
, , .., -
Arduino. (
http://www.powerswitchtail.com/.) , PowerSwitch Tail -
, ,
,
,
.
117
. -
, ,
.
, -
, .
5
:
99 ;
99, while do-while;
99 Arduino -
;
99 long.
, Arduino -
,
, ,
. , ,
,
, long.
.
,
. Arduino
, , .
-
, -
. ,
. ,
.
119
9:
, ,
. , ( )
( ) .
void blinkLED()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
,
Arduino:
// 9 -
#define LED 13
#define del 200
void setup()
{
pinMode(LED, OUTPUT);
}
void blinkLED()
{
digitalWrite(LED, HIGH);
delay(del);
digitalWrite(LED, LOW);
delay(del);
digitalWrite(LED, HIGH);
delay(del);
digitalWrite(LED, LOW);
delay(del);
}
void loop()
{
blinkLED();
delay(1000);
}
120 5
10:
,
. ,
. -
, , :
void blinkLED() :
cycles (, ) del ( -
).
12 100 , -
blinkLED(12, 100). IDE
:
// 10 ,
#define LED 13
void setup()
{
pinMode(LED, OUTPUT);
}
void loop()
{
blinkLED(12, 100);
delay(1000);
}
() , blinkLED() 12
100 ( ),
cycles 12, del 100. ,
12 100 .
,
, -
( void blinkLED() 10),
, analogRead(),
0 1023, .
8. void
, , void ().
- , .
, -
:
(convertTemp), -
( float) ,
(float celsius). , -
. , 40
float tempf,
:
tempf = convertTemp(40);
40 celsius convertTemp,
fahrenheit = (1.8 * celsius) + 32
tempf , convertTemp
return fahrenheit.
122 5
11:
,
, , , -
TMP36
4 Arduino. +20C,
;
+20C +26C, ;
+26C, .
, , -
, .
: -
( ).
:
TMP36.
.
.
Arduino USB.
, . 5.1.
.
TMP36, 2,
4 6, .
8.
blinkLed() 9. -
void loop 2
.
,
!
123
PWM
PWM
PWM
PWM
PWM
PWM
RX
TX
SCL
SDA
D9
D8
D7
D6
D5
D4
D3
D2
D1
D0
D13
D12
D11
D10
/
V in
Arduino
GND
3V3 5V
IO REF
AREF
RST
N/C
A0
A1
A2
A3
A4
A5
Vout
TMP
36
GND
+Vs
. 5.1. 11
IDE:
// 11 - ,
//
#define LED 13
int blinks = 0;
void setup()
{
pinMode(LED, OUTPUT);
}
int checkTemp()
{
float voltage = 0;
float celsius = 0;
float hotTemp = 26;
float coldTemp = 20;
float sensor = 0;
int result;
124 5
//
sensor = analogRead(0);
voltage = (sensor*5000)/1024; //
voltage = voltage-500; //
celsius = voltage/10; //
//
if (celsius < coldTemp)
{
result = 2;
}
else if (celsius >= coldTemp && celsius <= hotTemp)
{
result = 4;
}
else
{
result = 6; // (celsius > hotTemp)
}
return result;
}
void loop()
{
blinks = checkTemp();
blinkLED(blinks, 500);
delay(2000);
}
,
void loop() .
checkTemp() , blinks,
blinkLED() blinks 500 .
2 .
. (
, .)
125
Arduino
Arduino (,
) .
Arduino,
. , -
Arduino IDE
.
, IDE Serial
Monitor ( ) , . 5.2.
, . 5.3.
. 5.3.
. 5.3,
Send () ,
, Arduino. Autoscroll (-
), ,
.
Autoscroll (),
.
126 5
, , -
void setup():
Serial.begin(9600);
9600 Arduino,
. ,
(. . 5.3).
Serial.print:
, .
Serial.println:
.
, , results:
Serial.println(results);
float
. , 0 6
. ,
results ,
:
Serial.print(results,4);
12:
8,
. -
127
, ,
.
IDE:
// 12 -
float celsius = 0;
float fahrenheit = 0;
void setup()
{
Serial.begin(9600);
}
void findTemps()
{
float voltage = 0;
float sensor = 0;
//
//
sensor = analogRead(0);
voltage = (sensor*5000)/1024; //
voltage = voltage - 500; //
celsius = voltage / 10; //
//
fahrenheit = (1.8 * celsius) + 32; //
//
}
void displayTemps()
{
Serial.print("Temperature is ");
Serial.print(celsius, 2);
Serial.print(" deg. C / ");
Serial.print(fahrenheit, 2);
Serial.println(" deg. F");
// .println,
//
}
void loop()
{
findTemps();
displayTemps();
delay(1000);
}
, ,
findTemps() () displayTemps() (), . -
void loop(), .
:
128 5
.
, -
.
, . 5.4.
. 5.4. 12
(
) . Serial.println();
,
, Arduino . ,
:
Serial.println("now in findTemps()");
findTemps(), , Arduino -
.
while
while()
, (while) .
while() . -
129
int a = 0; //
while ( a < 10 )
{
a = a + 1;
delay(1000);
}
a 0. 1 -
a ( 0), 1 (delay(1000))
, a
10 (while ( a < 10 )). a 10, -
while false, Arduino ,
, ,
while.
do-while
while, do-while()
do-while. :
int a = 0; //
do
{
delay(1000);
a = a + 1;
} while ( a < 100 );
(while ( a < 100 )). ,
. ,
while do-while .
Arduino
Arduino, , Arduino -
Arduino, ,
( / 0 1), -
130 5
USB .
,
.
13:
. -
, 2 .
// 13 -
int number;
void setup()
{
Serial.begin(9600);
}
void loop()
{
number = 0; // ,
//
Serial.flush(); // ""
while (Serial.available() == 0)
{
// , -
}
while (Serial.available() > 0)
{
number = Serial.read() - '0';
// ,
// ASCII-
// '0',
}
// !
Serial.print("You entered: ");
Serial.println(number);
Serial.print(number);
Serial.print(" multiplied by two is ");
number = number * 2;
Serial.println(number);
}
Serial.available() while () 0,
. ,
Arduino: , -.
while () ,
131
, . Arduino ,
, .
Serial.flush() ,
, .
. 5.5 .
. 5.5. 13
-
Arduino, int
. ,
long, .
long
,
, , .
, , -
int, ,
32767. , -
long. long
2147483648 2147483647,
int ( 32768 32767).
14:
long
long
, . ,
.
132 5
// 14 - long
long number = 0;
long a = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
number = 0; // ,
//
Serial.flush(); // ""
while (Serial.available() == 0)
{
// , - ,
// - , Serial.available
// ,
}
// ,
//
while (Serial.available() > 0)
{
// ;
// , 1 10,
number = number * 10;
//
// '0',
a = Serial.read() - '0';
//
number = number + a;
// ,
//
delay(5);
}
Serial.print("You entered: ");
Serial.println(number);
Serial.print(number);
Serial.print(" multiplied by two is ");
number = number * 2;
Serial.println(number);
}
while Arduino
. (
), number.
133
, -
. (, 2 42),
number 10, -
, number . ,
number . -
No line ending ( )
(.5.6) .
. 5.6 .
. 5.6. 14
, ,
,
.
.
6 ,
99 ;
99 ;
99 ;
99 ,
;
99 ;
99 ;
99 ;
99 ;
99 ;
99 ;
99 .
,
, ,
, .
, ,
. -
, .
, 135
-
. ,
Arduino
-
-. , Arduino
- .
, ,
.
Arduino -
, ( -
) (, ) void setup():
randomSeed(analogRead(0));
Arduino ,
, -
. . -
-
random(lower, upper). lower upper
. ,
100 1000, :
int a = 0;
a = random(100, 1001);
1001, 1000, -
.
C , 0 -
, .
0 6 ():
a = random(7);
6.1 0 1000,
10 50:
136 6
6.1.
// 6.1
int r = 0;
void setup()
{
randomSeed(analogRead(0));
Serial.begin(9600);
}
void loop()
{
Serial.print("Random number between zero and 1000 is: ");
r = random(0, 1001);
Serial.println(r);
Serial.print("Random number between ten and fifty is: ");
r = random(10, 51);
Serial.println(r);
delay(1000);
}
. 6.1 , -
.
. 6.1. 6.1
, , ,
.
15:
: ,
.
1 6 , .
, 137
,
.
Arduino -
, -, -
,
, , .
, .
:
(LED1LED6).
560 (R1).
.
.
Arduino USB.
,
GND -
. . 6.2.
, :
// 15 -
void setup()
{
randomSeed(analogRead(0)); //
//
for ( int z = 1 ; z < 7 ; z++ ) // 1-6
{ //
pinMode(z, OUTPUT);
}
}
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
/
D8
D7
PWM LED6
D6
PWM LED5
D5
LED4
A0 D4
PWM LED3
A1
D3
LED2
A2 D2
TX LED1
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
R1
. 6.2. 15
digitalWrite(r, HIGH); //
if (del > 0)
{
delay(del); //
} //
else if (del == 0)
{
do // ,
{} // ""
while (1);
}
digitalWrite(r, LOW); //
}
void loop()
{
int a;
//
for ( a = 0 ; a < 100 ; a++ )
, 139
{
randomLED(50);
}
//
for ( a = 1 ; a <= 10 ; a++ )
{
randomLED(a * 100);
}
//
randomLED(0);
}
, void setup(), -
. randomLED() ,
delay() (), -
. 0 (),
, ()
do {} while (1);
1 1.
, Arduino. -
100 -
50 ().
, ,
100 1000 100 .
,
Arduino ,
:
randomLED(0);
. ,
, ,
,
. , -
!
,
( Arduino)
140 6
. , 10101010.
, , 2 ,
( ) (
). ,
.
, 11111111, . 6.1.
11111111 ,
, . 6.1:
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
255. -
( ) 1 ; 1
0 255.
(Most Significant Bit, MSB), (Least Significant
Bit, LSB).
,
/ , / -
. -
.
6.1.
27 26 25 24 23 22 21 20
1 1 1 1 1 1 1 1
128 64 32 16 8 4 2 1
byte
byte .
byte :
B , , , -
( 11111111), . -
6.2.
6.2.
// 6.2
byte a;
, 141
void setup()
{
Serial.begin(9600);
}
void loop()
{
for ( int count = 0 ; count < 256 ; count++ )
{
a = count;
Serial.print("Base-10 = ");
Serial.print(a, DEC);
Serial.print(" Binary = ");
Serial.println(a, BIN);
delay(1000);
}
}
byte Serial.print()
DEC () -
BIN ().
, . 6.3.
. 6.3. 6.2
Arduino Uno 13 ,
. . -
. -
, -
142 6
, .
74HC595,
. 6.4.
74HC595
,
. 6.4.
Arduino.
74HC595
,
.
: 1
(8 ),
. , ,
.
7 , 0.
, B10000110, -
7, 2 1 0 36.
,
.
-
; ,
.
,
.
16:
0 255
. 0 255
,
.
:
74HC595.
(LED1LED8).
560 (R1R8).
.
, 143
.
Arduino USB.
74HC595
. 6.5 , 74HC595 -
.
74HC595
1 Q1 V CC 16
2 Q2 Q0 15
3 Q3 DS 14
4 Q4 OE 13
5 Q5 ST_CP 12
6 Q6 SH_CP 11
7 Q7 MR 10
8 GND Q7 9
. 6.5. 74HC595
16 :
15 1- 7- ,
( Q0Q7 ).
Q7 , , -
Q0 .
8 (GND).
9 ,
, .
10 5 (, -
5 V Arduino).
11 12 .
13 -
(GND).
144 6
14 ,
Arduino.
16 5 ( 5 V Arduino).
, . 6.4 1 16.
,
. 6.6
.
GND
SDA
SCL A5
D0 A4
RX
74HC595
D1 A3
LED1 R1 TX
D2 A2
R2 1 16
/
Q1 VCC D3 A1
LED2
PWM
R3 2 15 D4 A0
LED3 Q2 Q0
R4
D5
3 Q3 DS 14 PWM
D6
LED4 R5 4 13
PWM
Q4 OE D7
LED5 R6 5 Q5 ST_CP 12 D8
R7 6 11
Q6 SH_CP
D9 N/C
LED6
PWM
R8 7 10
D10 Arduino IO REF
LED7 Q7 MR PWM
D11 AREF
8 9
PWM
GND Q7
D12 RST
LED8
D13
V in 3V3 5V
. 6.6. 16
, , .
// 16
void setup()
{
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
}
void loop()
{
int i;
for ( i = 0; i < 256; i++ )
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, i);
digitalWrite(LATCH, HIGH);
delay(200);
}
}
, ,
void setup() -
void loop() , 0 255.
. for (,
240, B11110000) , :
12 () LOW ( -
8 Arduino).
12 HIGH,
, shiftOut .
(, B11110000) 6
Arduino , shiftOut -
. , shiftOut
LSBFIRST, 1- 4- , -
. MSBFIRST, 5- 8-,
.
, 12 HIGH (5 ).
, .
-
.
17:
,
, 16. Arduino
146 6
,
.
, ,
.
. displayNumber()
. getAnswer()
. , checkAnswer()
,
.
0 255,
,
. -
, , , ,
.
.
// 17 -
#define DATA 6 // 14 74HC595
#define LATCH 8 // 12 74HC595
#define CLOCK 10 // 11 74HC595
int number = 0;
int answer = 0;
void setup()
{
pinMode(LATCH, OUTPUT); // 74HC595
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0)); //
//
displayNumber(0); //
}
void displayNumber(byte a)
{
//
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, a);
digitalWrite(LATCH, HIGH);
}
void getAnswer()
, 147
{
//
int z = 0;
Serial.flush();
while (Serial.available() == 0)
{
// , -
}
// ,
//
while (Serial.available() > 0)
{
// ;
// , 1 10,
answer = answer * 10;
//
// '0',
z = Serial.read() - '0';
//
answer = answer + z;
// ,
//
delay(5);
}
Serial.print("You entered: ");
Serial.println(answer);
}
void checkAnswer()
{
//
if (answer == number) // !
{
Serial.print("Correct! ");
Serial.print(answer, BIN);
Serial.print(" equals ");
Serial.println(number);
Serial.println();
}
else //
{
Serial.print("Incorrect, ");
Serial.print(number, BIN);
Serial.print(" equals ");
Serial.println(number);
Serial.println();
}
answer = 0;
delay(10000); //
}
void loop()
148 6
{
number = random(256);
displayNumber(number);
Serial.println("What is the binary number in base-10? ");
getAnswer();
checkAnswer();
}
, . void setup() () -
, ,
-
. displayNumber() ()
,
( 16). getAnswer() ()
, ( 14),
, . 6.7.
checkAnswer() () , -
getAnswer(), ,
void loop(). ,
. ,
void loop() -
, ,
.
. 6.7 .
. 6.7. 17
, , ,
.
,
.
, 149
. , ,
,
: ,
temperatures:
float temperatures[6];
.
. :
, ([])
({}).
0; temperatures[]
0 5.
,
. ,
temperatures[] ( 16,6) 12,34 :
temperatures[0] = 12.34;
6.3
. for
, for
.
6.3. /
// 6.3
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
}
int array[5]; //
void loop()
{
150 6
int i;
Serial.println();
for ( i = 0 ; i < 5 ; i++ ) //
{
array[i] = random(10); // 0 9
}
for ( i = 0 ; i < 5 ; i++ ) //
{
Serial.print("array[");
Serial.print(i);
Serial.print("] contains ");
Serial.println(array[i]);
}
delay(5000);
}
. 6.8 .
. 6.8. 6.3
, , , -
, .
.
, -
. -
,
. 6.9.
, -
, .
.
, 151
. 6.9.
. ,
,
. -
.
A G DP (Decimal Point -
). , ,
. -
. 6.10, A , B
.. , , 7,
A, B C.
-
, , -
. 6.10. ,
,
.
, . 6.11.
A
A DP
F G
B B
G C F
D E
E C
D DP
Cathode
. 6.10. . 6.11.
152 6
, 17,
A DP Q0Q7. -
,
. 6.2.
, -
, .
-
, .
6.2.
Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7
A B C D E F G DP
0 1 1 1 1 1 1 0 0 252
1 0 1 1 0 0 0 0 0 96
2 1 1 0 1 1 0 1 0 218
3 1 1 1 1 0 0 1 0 242
4 0 1 1 0 0 1 1 0 102
5 1 0 1 1 0 1 1 0 182
6 1 0 1 1 1 1 1 0 190
7 1 1 1 0 0 0 0 0 224
8 1 1 1 1 1 1 1 0 254
9 1 1 1 1 0 1 1 0 246
A 1 1 1 0 1 1 1 0 238
B 0 0 1 1 1 1 1 0 62
C 1 0 0 1 1 1 0 0 156
D 0 1 1 1 1 0 1 0 122
E 1 0 0 1 1 1 1 0 158
F 1 0 0 0 1 1 1 0 142
, 7, . 6.12,
A, B C, Q0, Q1 Q2 -
. , -
,
B1110000 ( shiftOut LSBFIRST).
, 153
A DP
B G
C F
D E
Cathode
. 6.12. 7
,
0 9 A F. , -
.
18:
, .
:
74HC595.
.
560 (R1R8).
.
.
Arduino USB.
. 6.13.
, -
AG Q0Q6 , DP
Q7.
154 6
GND
D0 A5
R1
RX
D1 A4
74HC595
TX
D2 A3
D3 A2
1 16
/
PWM
Q1 VCC
R2
D4 A1
2 Q2 Q0 15
D5 A0
R3
PWM
3 Q3 DS 14 D6
R4 PWM
4 Q4 OE 13 D7
R5
5 12
D8
Q5 ST_CP
R6
D9
6 11
PWM
Q6 SH_CP D10
R7
PWM
7 Q7 MR 10 D11 Arduino
R8
PWM
D13 RST
in
V 3V3 5V
. 6.13. 18
18 (. . 6.2) int
digits[]. void loop
(), -
, 1 , ():
// 18 -
#define DATA 6 // 14 74HC595
#define LATCH 8 // 12 74HC595
#define CLOCK 10 // 11 74HC595
//
// 0 - 9 A - F ( . 6.2)
int digits[] = {252, 96, 218, 242, 102, 182, 190, 224,
254, 246, 238, 62, 156, 122, 158, 142};
void setup()
{
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
}
void loop()
, 155
{
int i;
for ( i = 0 ; i < 16 ; i++ ) // 0-9, A-F
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, LSBFIRST, digits[i]);
digitalWrite(LATCH, HIGH);
delay(250);
}
for ( i = 0 ; i < 16 ; i++ ) // 0-9, A-F
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, LSBFIRST, digits[i]+1); // +1,
digitalWrite(LATCH, HIGH);
delay(250);
}
}
. ,
.6.14 9 .
. 6.14. , 18
-
, 9 74HC595 (
Arduino) 14 .
2 :
. :
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, 254); // 74HC595
shiftOut(DATA, CLOCK, MSBFIRST, 254); // 74HC595
digitalWrite(LATCH, HIGH);
156 6
19:
,
.
:
74HC595.
.
560 (R1R16).
.
.
Arduino USB.
. 6.15.
,
(11 12 )
Arduino. Arduino ( 6)
14 , 9 ,
14 .
0 99 .
10, 0,
, 0. 10,
, ,
. ,
.
, . ,
10 7 3 ,
10 7 3. C %.
, :
int a = 8;
int b = 3;
c = a % b;
, 157
AREF
RST
A5
A4
A3
A2
A1
A0
3V3 5V
Arduino
GND
V in
/
D10
D11
D12
D13
D0
D1
D2
D3
D4
D5
D6
D7
D8
D9
PWM
PWM
PWM
PWM
PWM
PWM
RX
TX
16
15
14
13
12
11
10
16
15
14
13
12
11
10
9
9
ST_CP
DS
MR
SH_CP
OE
VCC
ST_CP
74HC595
Q0
DS
MR
SH_CP
Q7
OE
VCC
Q0
74HC595
Q7
GND
GND
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q1
Q2
Q3
Q4
Q5
Q6
Q7
1
8
R1 R9
R7 R8 R15 R16
R2 R3 R4 R5 R6 R10 R11 R12 R13 R14
. 6.15. 19
c 2.
,
, .
-
displayNumber().
, . ,
23, , 23 10,
2 ( ). ,
23 10, 3.
// 19 -
//
158 6
// 0 - 9 A - F ( . 6.2)
int digits[] = {252, 96, 218, 242, 102, 182, 190, 224,
254, 246, 238, 62, 156, 122, 158, 142};
void setup()
{
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
}
void displayNumber(int n)
{
int left, right=0;
if (n < 10)
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, LSBFIRST, digits[n]);
shiftOut(DATA, CLOCK, LSBFIRST, 0);
digitalWrite(LATCH, HIGH);
}
else if (n >= 10)
{
right = n % 10; // 10
left = n / 10; // 10
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, LSBFIRST, digits[right]);
shiftOut(DATA, CLOCK, LSBFIRST, digits[left]);
digitalWrite(LATCH, HIGH);
}
}
void loop()
{
int i;
for ( i = 0 ; i < 100 ; i++ )
{
displayNumber(i);
delay(100);
}
}
displayNumber() 10 ().
10, 0.
10,
()
. , void loop() ()
0 99 displayNumber().
, 159
20:
TMP36, 4, . -
:
TMP36 ( 12)
.
:
19.
TMP36.
TMP36 A5,
5 V GND.
// 20 -
int temp = 0;
float voltage = 0;
float celsius = 0;
float sensor = 0;
int digits[]={252, 96, 218, 242, 102, 182, 190, 224,
254, 246, 238, 62, 156, 122, 158, 142};
void setup()
{
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
}
void displayNumber(int n)
160 6
{
int left, right = 0;
if (n < 10)
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, LSBFIRST, digits[n]);
shiftOut(DATA, CLOCK, LSBFIRST, digits[0]);
digitalWrite(LATCH, HIGH);
}
if (n >= 10)
{
right = n % 10;
left = n / 10;
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, LSBFIRST, digits[right]);
shiftOut(DATA, CLOCK, LSBFIRST, digits[left]);
digitalWrite(LATCH, HIGH);
}
}
void loop()
{
sensor = analogRead(5);
voltage = (sensor * 5000) / 1024; //
voltage = voltage - 500; //
celsius = voltage / 10; //
temp = int(celsius); //
//
displayNumber(temp);
delay(500);
}
: displayNumber()
19 12. delay(500);
, -
.
,
.
,
.
( . 6.16),
, 64 .
.
, 161
. 6.16.
,
. 6.17.
1 2 3 4 5 6 7 8
. 6.17.
162 6
, , . 6.17,
.
, 1 . , . 6.18
1 .
. 6.18. 1
( ), 8 , -
16 .
74HC595, , 19
.
. 6.19. R1R8
560 . ,
. ( )
. 6.3.
6.3. 74HC595
, ,
15 9 15 13
1 14 1 3
2 8 2 4
3 12 3 10
4 1 4 6
5 7 5 11
6 2 6 15
7 5 7 16
, 163
-
. 6.3 (
, , ).
GND
SDA
SCL A5
R1 RX
D0 A4
D1 A3
TX
74HC595
/
D2 A2
D3
R2
A1
1 Q1 VCC 16 PWM
D4 A0
R3 2 15
Q2 Q0 D5
PWM
R4 3 14
Q3 DS
D6
PWM
R5 4 13
Q4 OE
D7
R6 5 12
D8
Q5 ST_CP
D9 N/C
R7 6 11
PWM
Q6 SH_CP D10 Arduino IO REF
R8
PWM
7 Q7 MR 10 D11 AREF
PWM
74HC595
1 Q1 VCC 16
2 Q2 Q0 15
3 Q3 DS 14
4 Q4 OE 13
5 Q5 ST_CP 12
6 Q6 SH_CP 11
7 Q7 MR 10
8 GND Q7 9
. 6.19. 20
164 6
, . 6.19,
, , , -
GND.
. ,
.
int byte.
,
.
: (AND),
(OR), (XOR), (NOT), .
. (
,
int byte.)
(AND)
(AND) & .
- 1, -
1.
0, 0.
byte:
byte a = B00110011;
byte b = B01010001;
byte c = 0;
c = a & b;
00010001. ,
:
byte a = B00110011;
// ||||||||
byte b = B01010001;
// ||||||||
// 00010001 c = a & b; // c a '' b
, 165
(OR)
(OR) | -
, , (AND), 1,
1. -
0, 0.
, -
:
byte a = B00110011;
byte b = B01010001;
byte c = 0;
c = a | b;
01110011.
(XOR)
(XOR) ^ 1,
, 0, .
:
byte a = B00111100;
byte b = B01011010;
byte c = 0;
c = a ^ b;
01100110.
(NOT)
(NOT) ~ :
, . :
(NOT) a b, -
:
byte a = B00111100;
byte b = 0;
b = ~a;
b 11000011.
166 6
(<<) (>>)
. , a
, :
byte a = B00100101;
byte b = a << 4;
b 01010000. a
, .
, :
byte a = B11110001;
byte b = a >> 4;
b 00001111.
21:
, ;
,
. 6.20.
7 6 5 4 3 2 1 0
7
6
5
4
3
2
1
0
. 6.20.
, . 6.20, B10101010 -
, , ~B10101010 , -
.
.
, 167
(NOT) ~
. , , 0,
, . , -
, 1, ,
. , ,
, ~.
, . 6.20,
:
// 21 -
void setup()
{
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
}
void loop()
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, ~B10101010); //
shiftOut(DATA, CLOCK, MSBFIRST, B10101010); //
digitalWrite(LATCH, HIGH);
do {} while (1); //
}
. 6.21.
, .
. 6.21. 21
168 6
22:
, . -
, -
.
, .
, , -
.
, .
-
. void setLED()
, . -
.
// 22 -
void setup()
{
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
randomSeed(analogRead(0));
}
int binary[] = {1, 2, 4, 8, 16, 32, 64, 128};
int r, c = 0;
void loop()
{
r = random(8);
c = random(8);
//
// 1
setLED(r, c, 1);
}
, 169
shiftOut() int
binary[] ().
. , -
,
binary[3] ( 8, B00001000),
(~).
, -
;
, -
.
1 -
22
,
;
-
: -
,
,
. . 6.22. 22
. 6.22.
23:
,
. 6.23.
7 6 5 4 3 2 1 0
7
6
5
4
3
2
1
0
. 6.23. ,
170 6
,
, . 6.23.
.
MSBFIRST LSBFIRST shiftOut() -
.
for ():
// 23 -
void setup()
{
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
}
void loop()
{
int i;
for ( i = 0 ; i < 8 ; i++ )
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, ~smile[i]); //
shiftOut(DATA, CLOCK, LSBFIRST, binary[i]); //
digitalWrite(LATCH, HIGH);
delay(1);
}
}
, . 6.24.
, 171
. 6.24. 23
24:
23 . ,
: , >>
.
// 24 -
void setup()
{
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
}
void loop()
{
int a, hold, shift;
172 6
25
for (). shift
. shift 1 ().
.
1 . MSBFIRST LSBFIRST
for (), .
,
. , -
.
,
.
7
99
;
99, -
;
99
;
99 ,
.
- ,
. -
() Arduino.
, , ( ).
, ,
, -
. ,
.
. HD44780-
KS0066- 5
Arduino. -
, 2 16 ,
. 7.1.
174 7
. 7.1.
( )
10 . -
,
.
1 16. 1
, . 7.2,
VSS ( ).
4.2 , 1N4004 5V Arduino
LED+ . -
, .
,
. 7.1,
, . .
, 7.1.
7.1. ,
// 7.1
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9); // RS, E, DB4, DB5, DB6, DB7
void setup()
175
10k
LCD1
3V3 5V V in VSS
D13
VCC
RST D12
PWM V0
AREF D11
RS
IO REF Arduino D10
PWM
PWM R/W
N/C D9
/
E
D8
DB0
D7
DB1
LCD
PWM
D6
PWM DB2
D5
DB3
A0 D4
PWM DB4
A1 D3
DB5
A2 D2
TX DB6
A3 D1
RX DB7
A4 D0
A5 SCL LED+
SDA LED
GND
. 7.2.
{
lcd.begin(16, 2);
lcd.clear();
}
void loop()
{
lcd.setCursor(0, 5);
lcd.print("Hello");
lcd.setCursor(1, 6);
lcd.print("world!");
delay(10000);
}
. 7.3 7.1.
7.1. ,
. -
(
Arduino IDE) , Arduino -
. void setup():
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9); // RS, E, DB4, DB5, DB6, DB7
Arduino,
.
, void setup() ,
. , ,
16 :
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(x, y);
x ( 0 15), y (0 1).
lcd.print(), ,
text, :
lcd.print("text");
, , ,
.
, :
lcd.print(variable);
float
. , lcd.print(pi, 3) pi
, . 7.4:
177
float pi = 3.141592654;
lcd.print("pi: ");
lcd.print(pi, 3);
. 7.4.
, ,
7.2.
7.2. -
// 7.2
int zz = 170;
lcd.setCursor(0, 0);
lcd.print("Binary: ");
lcd.print(zz, BIN); // 170
lcd.setCursor(0, 1);
lcd.print("Hexadecimal: ");
lcd.print(zz, HX); // 170
, . 7.5.
. 7.5. 7.2
25:
, -
. ,
,
, . . 7.6 .
178 7
. 7.6.
, -
. , - :
: 0 , 1
. ;
,
..
, -
. 1 ,
0.
B00000, -
. (
B01010) .
.
( )
, , void setup():
, , void loop():
lcd.write(byte(0));
:
179
// 25 -
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9); // RS, E, DB4, DB5, DB6, DB7
byte a[8] = { B00000,
B01010,
B01010,
B00000,
B00100,
B10001,
B01110,
B00000 };
void setup()
{
lcd.begin(16, 2);
lcd.createChar(0, a);
}
void loop()
{
lcd.write(byte(0)); // 0
//
}
. 7.7 .
. 7.7. 25
-
. ,
, 20.
, -
.
-
, .
, , ,
180 7
.
KS0108B-
128 64 , -
. 7.8.
,
, 10 -
-
. 7.8. .
, -
.
1 20. 1
.
, 20
Arduino, . 7.1.
7.1. Arduino
Arduino
1 5V
2 GND
3 ;
10
4 D8 0
5 D9 1
6 D10 2
7 D11 3
8 D4 4
9 D5 5
10 D6 6
11 D7 7
12 A0 2
13 A1 1
14 RST
15 A2 /
181
Arduino
16 A3 /
17 A4
18 ;
10
( -
5 V)
19 5 (+)
22
20 GND (-)
Arduino. -
http://code.google.com/p/glcd-arduino/downloads/list/, ,
8.
, ,
void setup():
#include <glcd.h> //
#include "fonts/SystemFont5x7.h" //
void setup(), -
:
GLCD.Init(NON_INVERTED); // INVERTED,
// /
GLCD.SelectFont(System5x7); //
GLCD.ClearScreen(); //
, 20 .
(-
x y ):
GLCD.CursorTo(x, y);
182 7
( text -
):
GLCD.Puts("text");
( number
):
GLCD.PrintNumber(number);
26:
// 26 -
#include <glcd.h> //
#include "fonts/SystemFont5x7.h" //
int j = 7;
void setup()
{
GLCD.Init(NON_INVERTED);
GLCD.ClearScreen();
GLCD.SelectFont(System5x7);
}
void loop()
{
GLCD.ClearScreen();
GLCD.CursorTo(1, 1);
GLCD.Puts("Hello, world.");
GLCD.CursorTo(1, 2);
GLCD.Puts("I hope you are ");
GLCD.CursorTo(1, 3);
GLCD.Puts("enjoying this");
GLCD.CursorTo(1, 4);
GLCD.Puts("book. ");
GLCD.CursorTo(1, 5);
GLCD.Puts("This is from ");
GLCD.CursorTo(1, 6);
GLCD.Puts("chapter ");
GLCD.PrintNumber(j);
GLCD.Puts(".");
do {} while (1);
}
, . 7.9.
183
. 7.9. 26
-
. ,
128 64, 0 127
0 63 .
SetDot() x, y,
BLACK (), , WHITE (-
). color .
GLCD.SetDot(x, y, color); // color BLACK WHITE
w h ,
x, y. -
.
GLCD.DrawRect(x, y, w, h, color);
:
GLCD.FillRect(x, y, w, h, color);
,
, r.
GLCD.DrawRoundRect(x, y, w, h, r, color);
x, y r
:
GLCD.DrawCircle(x, y, r, color);
x, y l .
GLCD.DrawVertLine(x, y, l, color);
184 7
x, y l .
GLCD.DrawHoriLine(x, y, l, color);
, , ,
.
,
.
27:
20
100 .
Y, , X, -
. , -
.
.
,
.
TMP36 100 .
99 ,
,
. .
, -
,
.
, :
128 64 , KS0108B
.
10 .
TMP36.
.
185
.
Arduino USB.
. 7.1
TMP36 5 V, A5 GND Arduino.
, -
, .
// 27 -
#include <glcd.h> //
#include "fonts/SystemFont5x7.h" //
int tcurrent;
int tempArray[100];
void setup()
{
GLCD.Init(NON_INVERTED); // GLCD
GLCD.ClearScreen(); //
GLCD.SelectFont(System5x7);
}
// TMP36
void getTemp()
{
float sum = 0;
float voltage = 0;
float sensor = 0;
float celsius;
//
//
sensor = analogRead(5);
voltage = (sensor * 5000) / 1024;
voltage = voltage - 500;
celsius = voltage / 10;
tcurrent = int(celsius);
//
for (int a = 99 ; a >= 0 ; --a )
{
tempArray[a] = tempArray[a-1];
}
tempArray[0] = tcurrent;
}
//
186 7
void drawScreen()
{
int q;
GLCD.ClearScreen();
GLCD.CursorTo(5, 0);
GLCD.Puts("Current:");
GLCD.PrintNumber(tcurrent);
GLCD.CursorTo(0, 1);
GLCD.PrintNumber(40);
GLCD.CursorTo(0, 2);
GLCD.PrintNumber(32);
GLCD.CursorTo(0, 3);
GLCD.PrintNumber(24);
GLCD.CursorTo(0, 4);
GLCD.PrintNumber(16);
GLCD.CursorTo(1, 5);
GLCD.PrintNumber(8);
GLCD.CursorTo(1, 6);
GLCD.PrintNumber(0);
for (int a = 28 ; a < 127 ; a++)
{
q = (55 - tempArray[a-28]);
GLCD.SetDot(a, q, BLACK);
}
}
void loop()
{
getTemp();
drawScreen();
for (int a = 0 ; a < 20 ; a++) // 20
{
delay(60000); // 1
}
}
, -
. 7.10.
,
, .
,
, .
. ,
, .
187
. 7.10. 27
, , -
, Arduino :
.
.
Arduino, microSD
Arduino .
8 Arduino
:
99 Arduino;
99
ProtoShield;
99, Arduino -
;
99 microSD
, -
;
99 ;
99, micros()
millis();
99 Arduino .
Arduino ( ), ,
. , -
Arduino, ,
ProtoShield ,
.
/
microSD. -
,
; , ,
.
micros() millis()
,
. .
Arduino 189
Arduino -
. , -
, Arduino.
. , ,
GPS ( )
, microSD, , -
, -
, .
Ethernet,
Arduino (. 8.1).
GPS
Arduino (. 8.2). microSD -
(. 8.3).
. 8.3. microSD
Arduino
. ,
. 8.4 Arduino Uno microSD,
, Ethernet
.
,
/. /
, . -
/.
/, -
, http://www.shieldlist.org/.
Arduino 191
ProtoShield
- (, http://
www.shieldlist.org/) ProtoShield.
ProtoShield ,
. 8.5. , ProtoShield -
, -
Arduino (, , . 8.6).
- Blu-Tack
,
. ProtoShield
, .
ProtoShield
,
. , -
,
, , . ProtoShield
PDF , ,
.
28:
-
.
, .
192 8
, :
Arduino ProtoShield.
.
560 (R1R8).
6 .
8 .
. 8.7.
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
/
D8
R8
D7
PWM
R7
D6
PWM
R6
D5
R5
A0 D4
R4
PWM
A1 D3
R3
A2 D2
TX
R2
A3 D1
RX R1
A4 D0
A5 SCL
LED1 LED2 LED3 LED4 LED5 LED6 LED7 LED8
SDA
GND
. 8.7. 28
ProtoShield
ProtoShield.
ProtoShield
. ProtoShield, -
Arduino 193
. 8.8, ,
.
/
5V,
/ GND.
.
, : -
, .
, .
Arduino.
,
. 8.9.
, ,
ProtoShield, ,
. ProtoShield ,
, -
Arduino .
194 8
. 8.9.
ProtoShield , -
. ,
2540 , . 8.10.
. 8.10.
,
: http://mightyohm.com/soldercomic/.1
https://geektimes.ru/post/256784/. -
1
. .
Arduino 195
, . 8.11.
,
.
,
, . -
,
, , .8.12.
. 8.11. . 8.12.
. 8.13 :
Arduino .
. 8.13. !
196 8
, 0 7.
,
.
. !
Arduino
, -
,
.
,
Arduino .
Arduino IDE -
. ,
Sketch Import Library ( ),
, Ethernet,
LiquidCrystal, Servo . . (,
,
.)
.
, , ,
microSD, . 8.3.
1. https://github.com/greiman/SdFat/
Download ZIP ( ZIP-). . 8.14 ,
.
2. Downloads ()
SdFat-master.zip. , -
, , . 8.15.
3. , , ,
.
Arduino 197
. 8.14.
. 8.15.
Mac OS X
Mac OS X,
:
1. Downloads () , -
, . 8.16.
. 8.16.
198 8
. 8.17.
3. , . 8.18, SdFat-
master, Downloads (),
Choose ().
. 8.18.
4. SdFat , IDE
SketchImport Library ( ).
SdFat. ( ,
.)
, microSD,
.
Arduino 199
Windows XP
Windows XP -
, :
1. ,
SdFat, . 8.19.
. 8.19.
. 8.20.
3. SdFat , IDE
SketchImport Library ( ).
SdFat. ( ,
.)
200 8
, microSD,
.
. 8.21.
2. SdFat , .8.21,
Extract (), /libraries
(. 8.22).
. 8.22.
Arduino 201
3. SdFat , IDE
SketchImport Library ( ).
SdFat. ( ,
.)
.
microSD
microSD Arduino, -
, ,
TMP36 4. microSD
- , . -
microSD, . 8.23. -
microSD ( microSDHC) 2 .
. 8.23. microSD 2
,
SparkFun, Adafruit Industries Snootlab. -
( . 8.3) SparkFun ( DEV-09802
PRT-10007).
, .
. FAT16. ,
,
, , .
microSD
, -
. :
202 8
1. Arduino, -
Arduino USB.
2. IDE FileExamplesSdFatSdInfo (
SdFatSdInfo). SdInfo, IDE.
3. Serial Monitor ( ),
9600, ENTER.
microSD,
. 8.24.
, ,
, :
USB Arduino,
microSD .
,
.
, 9600 -
, Arduino Arduino
Uno. Mega
SPI, -
.
microSD .
. 8.24. microSD
Arduino 203
29:
,
microSD , :
// 29 -
int b = 0;
#include <SD.h>
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
// microSD
if (!SD.begin(8))
{
Serial.println("Card failed, or not present");
//
return;
}
Serial.println("microSD card is ready");
}
void loop()
{
//
File dataFile = SD.open("DATA.TXT", FILE_WRITE);
// , :
if (dataFile)
{
for ( int a = 0 ; a < 11 ; a++ )
{
dataFile.print(a);
dataFile.print(" multiplied by two is ");
b = a * 2;
dataFile.println(b, DEC);
}
dataFile.close(); // ,
// ()
}
// , :
else
{
Serial.println("error opening DATA.TXT");
}
Serial.println("finished");
do {} while (1);
}
204 8
microSD DATA.txt, -
. 8.25.
. 8.25. 29
void loop() ,
. void loop()
. -
dataFile.print() dataFile.println().
, , Serial.println(),
, -
. , ,
,
DATA.txt.
DEC. , b -
, .
( float)
( ).
() dataFile.close(),
. ,
.
30:
, , ,
TMP36, 4,
8 microSD.
microSD 29
27.
Arduino 205
:
TMP36;
;
;
microSD;
Arduino USB.
microSD ,
Arduino. TMP36
5V Arduino,
GND.
// 30 -
#include <SD.h>
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
// microSD
if (!SD.begin(8))
{
Serial.println("Card failed, or not present");
//
return;
}
Serial.println("microSD card is ready");
}
void loop()
{
//
File dataFile = SD.open("DATA.TXT", FILE_WRITE);
// , :
if (dataFile)
{
206 8
8 , -
, delay(599900).
microSD
, . 8.26.
. 8.26. 30
, -
. , OpenOffice Calc,
InsertSheet From File ( ), -
, . 8.27,
, . 8.28.
-
.
, Arduino.
Arduino 207
. 8.27. ,
. 8.28.
millis() micros()
, Arduino ,
.
(0,001) , (0,000001) . -
.
,
unsigned long:
unsigned long a,b;
a = micros();
b = millis();
- unsigned long 0 -
4294967295, 50 millis() 70
208 8
micros(). , - Arduino
micros() , .
, -
Arduino, LOW -
HIGH . micros()
digitalWrite(), .
Arduino USB.
8.1.
8.1.
micros()
// 8.1
void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
}
void loop()
{
start = micros();
digitalWrite(3, HIGH);
finished = micros();
elapsed = finished start;
Serial.print("LOW to HIGH: ");
Serial.print(elapsed);
Serial.println(" microseconds");
delay(1000);
start = micros();
digitalWrite(3, LOW);
finished = micros();
elapsed = finished - start;
Serial.print("HIGH to LOW: ");
Serial.print(elapsed);
Serial.println(" microseconds");
delay(1000);
}
micros()
digitalWrite(HIGH) ( ),
(). -
().
Arduino 209
, ,
. 8.29.
. 8.29. 8.1
4 , -
8 ,
4 8.
31:
, ,
Arduino .
: ( ),
.
. -
millis(),
millis(). displayResult() -
, . ,
.
, :
.
(S1 S2).
10 (R1 R2).
.
Arduino USB.
210 8
. 8.30.
, , -
.
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
/
D8
D7
PWM
D6
PWM
D5 S1
A0 D4
S2
PWM
A1 D3
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL R1 R2
10k 10k
SDA
GND
. 8.30. 31
// 31
void setup()
{
Arduino 211
Serial.begin(9600);
pinMode(2, INPUT); //
pinMode(3, INPUT); //
Serial.println("Press 1 for Start/reset, 2 for elapsed time");
}
void displayResult()
{
float h, m, s, ms;
unsigned long over;
h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
void loop()
{
if (digitalRead(2) == HIGH)
{
start = millis();
delay(200); //
Serial.println("Started...");
}
if (digitalRead(3) == HIGH)
{
finished = millis();
delay(200); //
displayResult();
}
}
. -
,
212 8
. , , Arduino , -
millis(), -
(). ,
displayResult() (),
.
. 8.31.
. 8.31. 31
Arduino ,
, ,
. -
, ,
.
, void loop()
, ,
, ,
.
,
. ,
, , -
,
. ,
Hello , -
---,
Arduino 213
: H---ello, He---llo,
Hel---lo, Hell---o Hello---.
Arduino Uno , -
2 3. Arduino
, . -
(, ), ,
, , , Stop
Pressing Me!.
(), :
LOW: .
CHANGE:
, .
RISING: -
.
FALLING :
.
, ,
, RISING. , , -
( 5V
), FALLING ,
- .
delay() Serial.available() , -
.
, void
setup():
0 2, 1 3,
function , , mode
.
214 8
. -
:
noInterrupts(); //
interrupts(); //
,
, ,
.
32:
, 31.
500 -
, .
, 0,
micros(), , 1,
millis().
// 32
#define LED 13
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
attachInterrupt(0, displayMicros, RISING);
attachInterrupt(1, displayMillis, RISING);
}
void displayMicros()
{
Serial.write("micros() = ");
Serial.println(micros());
}
Arduino 215
void displayMillis()
{
Serial.write("millis() = ");
Serial.println(millis());
}
void loop()
{
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}
,
void loop() (). 0, -
displayMicros() (); 1,
displayMillis() (). -
void loop().
.
millis() micros(), . 8.32.
. 8.32. 32
,
. -
Arduino,
microSD -
.
9
:
99, Arduino;
99 , ;
99 switch-case;
99 .
, Arduino
, -
, ,
- .
10 (-
0 9), -
, , -
. 9.1.
-
: 7
12,
. 9.1. Arduino -
, 4. Keypad
http://playground.arduino.cc/Code/Keypad/.
217
Arduino . -
(. 9.2).
. 9.2.
1 7, .
,
. 9.1.
9.1. Arduino
Arduino
1 5
2 4
3 3
4 2
5 8
6 7
7 6
, ,
, , -
.
.
, .
9.1:
218 9
9.1. ,
// 9.1
//
#include "Keypad.h"
const byte ROWS = 4; //
const byte COLS = 3; //
char keys[ROWS][COLS] =
{{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//
void setup()
{
Serial.begin(9600);
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
Serial.print(key);
}
}
- char ,
, . -
. ,
Arduino .
, , . 9.1.
,
. , , -
, . 9.3.
219
. 9.3.
switch-case
,
switch-case if-then, -
switch-case
, . -
, xx 1, 2
3 , ,
, , ,
if-then:
switch(xx)
{
case 1:
// , xx 1
break; // ,
// switch-case
case 2:
// , xx 2
break;
case 3:
// , xx 3
break;
default:
// , xx - ,
// 1, 2 3
// default
}
default:
, ,
switch-case.
220 9
33:
,
. , -
9.1, ,
.
, .
. ,
. ,
*, #.
// 33 -
//
#include "Keypad.h"
const byte ROWS = 4; //
const byte COLS = 3; //
char keys[ROWS][COLS] =
{{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//
char PIN[6]={'1','2','3','4','5','6'}; //
char attempt[6]={0,0,0,0,0,0};
int z=0;
void setup()
{
Serial.begin(9600);
}
void correctPIN() // ,
{
Serial.println("Correct PIN entered...");
}
void incorrectPIN() // ,
221
{
Serial.println("Incorrect PIN entered!");
}
void checkPIN()
{
int correct=0;
int i;
for ( i = 0; i < 6 ; i++ )
{
if (attempt[i]==PIN[i])
{
correct++;
}
}
if (correct==6)
{
correctPIN();
} else
{
incorrectPIN();
}
for (int zz=0; zz<6; zz++) // ,
{
attempt[zz]=0;
}
}
void readKeypad()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
switch(key)
{
case '*':
z=0;
break;
case '#':
delay(100); //
checkPIN();
break;
default:
attempt[z]=key;
z++;
}
}
}
void loop()
{
readKeypad();
}
222 9
( 9.1) -
, readKeypad() ().
, switch-case ().
, ,
attempt[], #, checkPin().
PIN[] (). ,
correctPin() (), , , , ;
, incorrectPin()
(). , attempt[]
.
Arduino, ,
(*) ,
(#). -
. , . 9.4.
. 9.4.
,
, , - -
. correctPIN() incorrectPIN(),
.
Arduino,
, Arduino.
switch-case.
: .
10
:
99, Arduino;
99 , ;
99 , ;
99 .
: ,
.
?
, -
,
SparkFun ( LCD-08977 BOB-
09170),
Nintendo DS.
5 7 . 10.1,
.
,
( . 10.1). . 10.1. ,
-
224 10
Arduino; . 10.2 -
.
. 10.2.
Arduino, . 10.1.
10.1. Arduino
Arduino
Y1 A0
X1 A1
Y2 A2
X2 A3
34:
, .
X, Y. -
, ,
, X Y .
Arduino
.
, :
.
10 .
225
16 2 .
.
.
Arduino USB.
. 10.1
, . 7.2.
.
:
// 34 -
#include <LiquidCrystal.h>
LiquidCrystal lcd(4,5,6,7,8,9);
int x,y = 0;
void setup()
{
lcd.begin(16,2);
lcd.clear();
}
int readX() // X
{
int xr=0;
pinMode(A0, INPUT);
pinMode(A1, OUTPUT);
pinMode(A2, INPUT);
pinMode(A3, OUTPUT);
digitalWrite(A1, LOW); // A1
digitalWrite(A3, HIGH); // A3
delay(5);
xr=analogRead(0); // X
return xr;
}
int readY() // Y
{
int yr=0;
pinMode(A0, OUTPUT); // A0
pinMode(A1, INPUT); // A1
pinMode(A2, OUTPUT); // A2
pinMode(A3, INPUT); // A3
digitalWrite(14, LOW); // A0
226 10
digitalWrite(16, HIGH); // A2
delay(5);
yr=analogRead(1); // Y
return yr;
}
void loop()
{
lcd.setCursor(0,0);
lcd.print(" x = ");
x=readX();
lcd.print(x);
y=readY();
lcd.setCursor(0,1);
lcd.print(" y = ");
lcd.print(y);
delay (200);
}
readX() readY() ( ) -
analogRead() .
-
( ). ( delay(5) ,
/ .)
, , ,
X Y . -
, ,
(.10.3).
. 10.3. ,
, -
.
227
, . 10.4, -
, .
. ,
.
X = 900 X = 100
Y = 900 Y = 900
X = 900 X = 100
Y = 150 Y = 150
. 10.4.
,
readX() readY()
if-then
, 35.
35:
.
, . 10.5:
, .
, Arduino -
, . -
,
.
228 10
X = 500
X = 900 Y = 900 X = 100
Y = 900 Y = 900
X = 900 X = 100
X = 500 Y = 150
Y = 150
Y = 150
. 10.5.
:
// 35 -
int x,y = 0;
void setup()
{
Serial.begin(9600);
pinMode(10, OUTPUT);
}
void switchOn()
{
digitalWrite(10, HIGH);
Serial.print("Turned ON at X = ");
Serial.print(x);
Serial.print(" Y = ");
Serial.println(y);
delay(200);
}
void switchOff()
{
digitalWrite(10, LOW);
Serial.print("Turned OFF at X = ");
Serial.print(x);
Serial.print(" Y = ");
Serial.println(y);
delay(200);
}
229
int readX() // X
{
int xr=0;
pinMode(A0, INPUT);
pinMode(A1, OUTPUT);
pinMode(A2, INPUT);
pinMode(A3, OUTPUT);
digitalWrite(A1, LOW); // A1
digitalWrite(A3, HIGH); // A3
delay(5);
xr=analogRead(0);
return xr;
}
int readY() // Y
{
int yr=0;
pinMode(A0, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, OUTPUT);
pinMode(A3, INPUT);
digitalWrite(A0, LOW); // A0
digitalWrite(A2, HIGH); // A2
delay(5);
yr=analogRead(1);
return yr;
}
void loop()
{
x=readX();
y=readY();
// ""?
if (x<=900 && x>=500)
{
switchOn();
}
// ""?
if (x<500 && x>=100)
{
switchOff();
}
}
if void loop() ,
. , -
. ,
.
230 10
Y , -
.
, Y, 36.
. 10.6 , . -
.
. 10.6. 35
36:
,
3, , -
, (. 3).
. 10.7 .
: , .
, , ,
, .
// 36 -
int x,y = 0;
void setup()
231
X = 900 X = 100
Y = 900 Y = 900
. 10.7. 36
{
pinMode(3, OUTPUT);
}
void switchOn()
{
digitalWrite(3, HIGH);
delay(200);
}
void switchOff()
{
digitalWrite(3, LOW);
delay(200);
}
void setBrightness()
{
int xx, bright;
float br;
xx=x-100;
br=(800-xx)/255;
bright=int(br);
analogWrite(3, bright);
}
int readX() // X
{
int xr=0;
pinMode(A0, INPUT);
pinMode(A1, OUTPUT);
232 10
pinMode(A2, INPUT);
pinMode(A3, OUTPUT);
digitalWrite(A1, LOW); // A1
digitalWrite(A3, HIGH); // A3
delay(5);
xr=analogRead(0);
return xr;
}
int readY() // Y
{
int yr=0;
pinMode(A0, OUTPUT); // A0
pinMode(A1, INPUT); // A1
pinMode(A2, OUTPUT); // A2
pinMode(A3, INPUT); // A3
digitalWrite(A0, LOW); // A0
digitalWrite(A2, HIGH); // A2
delay(5);
yr=analogRead(1);
return yr;
}
void loop()
{
x=readX();
y=readY();
// ""?
if (x<=500 && x>=100 && y>= 150 && y<375)
{
switchOn();
}
// ""?
if (x>500 && x<=900 && y>= 150 && y<375)
{
switchOff();
}
// ""?
if (y>=375 && y<=900)
{
setBrightness();
}
}
, , -
() (),
233
, () -
, .
, X
setBrightness() PWM ()
.
.
Arduino.
Arduino, -
Arduino .
11 Arduino
:
99, Arduino ;
99 , -
Arduino;
99 .
Arduino, ,
. , ,
.
. -
Arduino
.
Arduino Uno .
37:
Arduino
Arduino - ,
.
Arduino ,
, , -
. ,
Arduino
( -
), 10 .
,
Arduino,
Arduino .
Arduino 235
Arduino :
.
.
7805.
16 (, -
Newark, 16C8140).
ATmega328P-PU Arduino.
1 -
25 (C1).
100 -
25 (C2).
22
50(C3 C4).
100
50 (C5).
560 (R1 R2).
10 (R3).
(LED1 LED2).
(S1).
.
PP3.
PP3 9 .
.
, , -
.
. 11.1. 7805
. 11.2 , 7805
. , (IN) -
, (GND)
(OUT) 5 .
, .
, (1),
7805 .
GND. -
7805.
7805
1 3
IN OUT
GND
2
. 11.2. 7805
16
.
. -
16 . , , . 11.3.
Arduino.
.
. ,
. 11.4.
Arduino 237
16 MHz
. 11.3. . 11.4.
. , -
, , 16 ,
16 .
, ,
.
Atmel ATmega328P-PU
, , -
, ,
,
. 2, Arduino.
. 11.5 , ATmega328P-PU.
, 1 -
.
. 11.5. ATmega328P-PU
,
.11.6.
Arduino , -
, Arduino.
Arduino, , .
238 11
, -
Arduino, Adafruit, Freetronics SparkFun.
28
27
26
25
24
23
22
21
20
19
18
17
16
15
328P-PU
ATMEGA
11
1
10
12
13
14
. 11.6.
. 11.7 Arduino.
R2
560 LED2 -
7805
IN OUT
28
27
26
25
24
23
22
21
20
19
18
17
16
15
9V GND
R1
+
328P-PU
C1 C2 560
+ 1F + 100F
ATMEGA
LED1 - -
11
1
10
12
13
14
R3
10k
S1
16 MHz
C3 C4
22pF 22pF
C5
100nF
. 11.7. Arduino
. , , , -
5 . ,
LED1. , , ,
Arduino 239
,
. ATmega328P-PU, -
Arduino 13. ,
.
( . 11.8),
.
Arduino.
9 , -
, . 11.9.
(+) , ().
. 11.8.
. 11.9. 9
240 11
Arduino
Arduino ?
, ,
Arduino, . , -
.
R2 LED2 Arduino -
13. . 11.1 Arduino ()
ATmega328P-PU ().
11.1. ATmega328P-PU
Arduino ATmega328P-PU
RST 1
RX/D0 2
TX/D1 3
D2 4
D3 5
D4 6
5V 7
GND 8
D5 11
D6 12
D7 13
D8 14
D9 15
D10 16
D11 17
D12 18
D13 19
5V 20
AREF 21
GND 22
A0 23
A1 24
A2 25
A3 26
A4 27
A5 28
Arduino 241
, , Adafruit Freetronics,
, . 11.10 (http://www.adafruit.
com/products/554/).
. 11.10.
. ,
:
// 37 - Arduino
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
Arduino, ,
242 11
, -
.
Arduino
, . 11.11.
. 11.11.
, , -
! ,
.
Arduino, -
, .
-
; ,
.11.12.
. 11.12.
Arduino 243
, Arduino, ,
, . 11.13.
. 11.13. Arduino
Arduino
USB Arduino Uno. -
,
USB.
,
USB:
1. USB Arduino Uno -
.
2. ( ).
3. 0 Arduino 2
ATmega328P-PU -
1 Arduino 3 ATmega328P-PU
.
4. 5V GND Uno
.
5. RST Arduino RST 1 -
ATmega328P-PU .
6. USB Arduino Uno.
, -
Arduino Uno.
-
, .
FTDI
, USB,
FTDI ( ,
244 11
USB, FTDI). -
FTDI, , 5,
3,3 .
( . 11.14) USB
. USB -
, USB Arduino Uno.
,
. 11.7 11.8.
. 11.14. FTDI
-
GND .
, Arduino.
,
Arduino Duemilanove Nano w/ ATmega328 Tools
Board (), . 11.15.
. 11.15. IDE
Arduino 245
, ,
37. , -
,
.
, .
Arduino
Arduino Uno, -
.
, , .
Arduino
.
ATmega328 ATmega2560, Due -
, .
( ATmega328) . 11.2.
11.2.
16 16 16 84
5 5 5 3.3
- 14 (6 - 14 (6 - 54 (14 - 54 (12 -
) )
) )
- 6 8 16 12
40 40 40 3 15
/
- 31.5 31.5 248 512
1 1 4
2 2 8 96
Arduino
. :
246 11
- ,
IDE.
(- -
)
, 16.
, , -
.
Arduino Uno , , ,
. ,
Mega.
, Nano
LilyPad.
Arduino Uno
Uno Arduino. - -
Arduino
Uno. Arduino Uno -
USB .
Freetronics Eleven
, Arduino Uno, -
.
Freetronics Eleven, . 11.16.
Arduino Uno, Eleven
, . -
/.
, -
, .
: /
(TX/RX) , D13,
; -
. ,
-USB, USB,
Uno. ,
,
Arduino 247
USB. -
http://www.freetronics.com/products/eleven/.
Freeduino
Freeduino . -
; , -
, Arduino.
Duemilanove ,
. 11.17.
Freeduino, . -
Freeduino
. Freeduino
http://www.seeedstudio.com/.
Pro Trinket
Pro Trinket (. 11.18) Arduino Uno, -
,
, .
Arduino Uno (,
,
FTDI); .
Pro Trinket http://www.adafruit.com/.
Arduino Nano
, -
Arduino- Nano. , Arduino
(. 11.19) .
Arduino LilyPad
LilyPad
, -
. -
, LilyPad
, ,
. -
, . 11.20.
LilyPad -
, ,
- LilyPad -
, . - . 11.20. Arduino LilyPad
, -
,
2,7 5,5 . LilyPad USB,
5- FTDI.
Arduino LilyPad Arduino.
Freetronics EtherMega
Arduino Mega 2560,
microSD Ethernet -
, EtherMega
(. 11.22), , ,
. EtherMega
http://www.freetronics.com/em/.
Arduino Due
Arduino - .
84 .
. 11.23, Arduino Mega 2560,
USB
.
Arduino 251
, Due , 16
Uno, - . Due
3,3 , ,
,
, 3,3 . , -
, Due
.
Arduino ,
, , .
, -
, ,
.
Arduino ,
, , , -
.
,
.
Arduino ,
.
, , -
Arduino, -
.
, ,
.
252 11
,
Arduino, , -
, Arduino, , Arduino
. ,
Arduino, .
Arduino, ,
, . -
, .
,
Arduino!
12
:
99, ;
99 ;
99 Arduino, -
;
99 ;
99, ;
99 -
.
,
. , ,
,
. -
. . 12.1 .
:
, ; -
.
254 12
. 12.1.
90
0 180
. 12.2.
; , 180
( ) 360 ( ).
, .
Arduino
.
255
, .
, -
. ,
.
. 12.1 hexTronik HXT900. -
180 , . 12.2.
Arduino,
. HXT900, (. 12.1)
GND, -
5 , ( )
. ,
.
. -
, .
Arduino, ,
4, 12.1.
12.1. ,
// 12.1
#include <Servo.h>
Servo myservo;
void setup()
{
myservo.attach(4);
}
void loop()
{
myservo.write(180);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(0);
delay(1000);
}
#include <Servo.h>
Servo myservo;
, void setup(), ,
Arduino :
myservo.attach(4); // 4
:
myservo.write(x);
x 0 180 ,
. 12.1 -
, -
(0 180 ) (90 ).
, , ,
180 , , 0 .
,
, . ,
,
38.
38:
TMP36, -
, .
0 180 ,
0C +30C. -
, .
:
TMP36.
.
.
.
Arduino USB.
257
, . 12.3.
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
/
D8
D7
D6
PWM
+Vs PWM 1
D5 +
TMP Vout A0 D4
2
pulse
36
PWM 3
A1 D3
GND
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 12.3. 38
, , -
8. .
:
// 38 -
float voltage = 0;
float sensor = 0;
float currentC = 0;
int angle = 0;
#include <Servo.h>
Servo myservo;
258 12
void setup()
{
myservo.attach(4);
}
void loop()
{
//
sensor = analogRead(0);
voltage = (sensor*5000)/1024;
voltage = voltage-500;
currentC = voltage/10;
//
angle = calculateservo(currentC);
//
if (angle >= 0 && angle <= 180)
{
myservo.write(angle); // angle
delay(1000);
}
}
. 12.4. , ,
259
, -
calculateservo().
:
= (6 ) + 180
,
. . 12.4.
. -
,
.
,
:
3 12 .
, -
, , ,
.
, ,
.
-
(/).
8540 / 3 , ,
. 12.5.
. 12.5.
260 12
,
3. 0,7 (,
BC548), -
, ,
.
TIP120
( )
. TIP120
5 60 ,
. TIP120 -
,
BC548, . 12.6, TIP120 , BC548.
. 12.6. TIP120
. 12.7. TIP120
TIP120 , -
( ): (), () (). -
, ,
.
261
39:
, :
3 .
1 (R1).
.
1N4004.
TIP120.
3 .
.
Arduino USB.
, -
Arduino .
,
1 . , Arduino, -
, .
.
3 -
AA, . 12.8.
. 12.8. AA
262 12
, . 12.9.
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
D8
/
D7
PWM
D6 R1
PWM 1k
D5
A0 D4
PWM
A1 D3
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 12.9. 39
( ) -
. :
// 39 -
void setup()
{
pinMode(5, OUTPUT);
}
void loop()
{
263
-
- ( 3). ,
3, 5, 6, 9, 10
11. -
: , ,
-
. for () ,
, , ;
. -
5 .
for () ,
.
, -
, , . -
.
,
(. 3.19), .
,
, - .
,
.
40:
-
, -
.
264 12
,
. -
.
, -
. ,
, .
, , -
,
. ,
,
.
, :
Pololu RP5 .
Pololu RP5.
.
9-
Arduino.
DFRobot 2A Motor Shield -
.
Arduino USB.
, -
. , Arduino,
.
, -
Pololu RP5,
.12.10 .
Dagu Rover 5 Pololu .
Pololu AA,
,
. 12.11.
.
265
. 12.10.
. 12.11. AA
. 12.11 , -
Arduino ,
, ,
. Arduino
9- , ,
. 12.12.
, , , -
. 12.13.
3. (,
--
.) . 12.14
Arduino.
266 12
. 12.12. Arduino
. 12.13.
,
. , -
. 12.9,
.
, -
.
, ,
Arduino
267
. 12.14. Arduino
. 2A Motor
Shield Arduino DFRobot (http://www.dfrobot.com/),
. 12.15.
. 12.15. DFRobot
: ,
, ,
, . 12.16. () -
, () .
268 12
, .
, ,
. 12.17.
. 12.16. . 12.17.
. : -
Power ()
. -
, , , .12.18.
, ,
PWM (), . 12.19.
. 12.18. . 12.19.
, , ,
,
, .
-
, Arduino -
. ,
. 12.20.
269
. 12.20. !
. ,
.
, :
;
;
;
.
: ( -
39), .
: goForward(),
goBackward(), rotateLeft() rotateRight(). -
, , -
, 0 255. ,
2 ,
goForward(2000, 255).
270 12
( ):
// 40 -
int m1speed=6; // ,
int m2speed=5;
int m1direction=7; // ,
int m2direction=4;
void setup()
{
pinMode(m1direction, OUTPUT);
pinMode(m2direction, OUTPUT);
delay(5000);
}
delay(duration);
analogWrite(m1speed, 0); //
analogWrite(m2speed, 0);
}
void loop()
{
goForward(1000, 255);
rotateLeft(1000, 255);
goForward(1000, 255);
rotateRight(1000, 255);
goForward(1000, 255);
goBackward(2000, 255);
delay(2000);
}
digitalWrite(m1direction,direction);
HIGH direction -
, LOW . , ,
,
. :
analogWrite(m1speed, pwm);
pwm 0 255.
, -
, .
, , -
; ,
!
, USB 9- -
Arduino.
. -
40;
.
, , ,
,
,
272 12
.
: , .
41:
,
4; ,
, (. 12.21).
. 12.21.
-
, NO (normally open -
), . -
, - ,
,
.
, . 12.22.
2,
. , , ,
- -
, , delay() -
. - .
273
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
/
D8
D7
PWM
D6
PWM
D5
S1
A0 D4
PWM
A1 D3
A2 D2
TX R1
A3 D1 10k
RX
A4 D0
A5 SCL
SDA
GND
. 12.22.
: goForward()
,
crash move. crash true, -
2 ,
.
delay() - , -
:
millis(),
, -
. -
, move false -
.
:
274 12
// 41
int m1speed=6; // ,
int m2speed=5;
int m1direction=7; // ,
int m2direction=4;
boolean crash=false;
void setup()
{
pinMode(m1direction, OUTPUT);
pinMode(m2direction, OUTPUT);
attachInterrupt(0, backOut, RISING);
delay(5000);
}
void backOut()
{
crash=true;
}
void backUp()
{
digitalWrite(m1direction,LOW); //
digitalWrite(m2direction,LOW); //
analogWrite(m1speed, 200); //
analogWrite(m2speed, 200);
delay(2000);
analogWrite(m1speed, 0); //
analogWrite(m2speed, 0);
}
a=millis();
do
{
if (crash==false)
{
digitalWrite(m1direction,HIGH); //
digitalWrite(m2direction,HIGH); //
analogWrite(m1speed, pwm); //
analogWrite(m2speed, pwm);
}
if (crash==true)
{
backUp();
crash=false;
}
275
b=millis()-a;
if (b>=duration)
{
move=false;
}
} while (move!=false);
//
analogWrite(m1speed, 0);
analogWrite(m2speed, 0);
}
void loop()
{
goForward(5000, 255);
delay(2000);
}
:
. -
crash. -
, ,
backOut() (). crash
true. , , move.
goForward() millis() (), -
, ,
( duration).
, ,
, move
true . ,
.
, backUp() (), -
2 ,
.
, -
40.
() .
, , ,
.
,
276 12
, . -
Sharp GP2Y0A21YK0F,
. 12.23.
. 12.23. - Sharp
Arduino, ,
, 5V GND , -
. , ,
analogRead(). . 12.24
.
3,5
( 90%)
( 18%)
3
2,5
()
1,5
0,5
0
0 10 20 30 40 50 60 70 80
()
. 12.24. -
( )
277
-
, 5- .
, , .
0, 5V
GND, , -
12.2.
12.2. , -
// 12.2
float sensor = 0;
int cm = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
sensor = analogRead(0);
if (sensor<=90)
{
Serial.println("Infinite distance!");
} else if (sensor<100) // 80
{
cm = 80;
} else if (sensor<110) // 70
{
cm = 70;
} else if (sensor<118) // 60
{
cm = 60;
} else if (sensor<147) // 50
{
cm = 50;
} else if (sensor<188) // 40
{
cm = 40;
} else if (sensor<230) // 30
{
cm = 30;
} else if (sensor<302) // 25
{
cm = 25;
} else if (sensor<360) // 20
{
cm = 20;
} else if (sensor<505) // 15
{
278 12
cm = 15;
} else if (sensor<510) // 10
{
cm = 10;
} else if (sensor>=510) // !
{
Serial.println("Too close!");
}
Serial.print("Distance: ");
Serial.print(cm);
Serial.println(" cm");
delay(250);
}
- () -
if () .
, ,
, . 12.24, ( 6),
analogRead() 0 1023,
0 5 .
, ,
.
, . 12.25.
. 12.25. 12.2
42:
-
.
41.
279
checkDistance(),
crash true, -
20 . goForward(), do... while.
- ,
:
// 42 - -
int m1speed=6; // ,
int m2speed=5;
int m1direction=7; // ,
int m2direction=4;
boolean crash=false;
void setup()
{
pinMode(m1direction, OUTPUT);
pinMode(m2direction, OUTPUT);
delay(5000);
}
void backUp()
{
digitalWrite(m1direction,LOW); //
digitalWrite(m2direction,LOW); //
analogWrite(m1speed, 200); //
analogWrite(m2speed, 200);
delay(2000);
analogWrite(m1speed, 0); //
analogWrite(m2speed, 0);
}
void checkDistance()
{
if (analogRead(0)>360)
{
crash=true;
}
}
digitalWrite(m1direction,HIGH); //
digitalWrite(m2direction,HIGH); //
analogWrite(m1speed, pwm); //
analogWrite(m2speed, pwm);
}
if (crash==true)
{
backUp();
crash=false;
}
b=millis()-a;
if (b>=duration)
{
move=false;
}
} while (move!=false);
//
analogWrite(m1speed, 0);
analogWrite(m2speed, 0);
}
void loop()
{
goForward(5000, 255);
delay(2000);
}
, 41, -
, () -
crash true, -
20 .
,
.
, ,
, ,
.
, -
, .
( ) ,
.
Parallax Ping))), . 12.26,
1 .
281
-
2 300 . - ,
, ,
45 .
5V GND
Arduino, SIG ( signal ) -
Arduino.
.
, SIG
5 ().
HIGH, ,
.
,
.
:
delayMicroseconds(mS)
(mS);
pulseDuration(pin, HIGH)
pin .
29,412 (
340 /, 34 /).
282 12
,
getDistance() 12.3. SIG
3, .
12.3.
// 12.3
int signal=3;
void setup()
{
pinMode(signal, OUTPUT);
Serial.begin(9600);
}
int getDistance()
//
// Ping)))
{
int distance;
unsigned long pulseduration=0;
// Ping)))
// ,
//
pinMode(signal, OUTPUT);
//
digitalWrite(signal, LOW);
delayMicroseconds(5);
// 5 ,
// Ping)))
digitalWrite(signal, HIGH);
delayMicroseconds(5);
digitalWrite(signal, LOW);
// ,
//
pinMode(signal, INPUT);
//
pulseduration=pulseIn(signal, HIGH);
//
pulseduration=pulseduration/2;
//
283
distance = int(pulseduration/29);
return distance;
}
void loop()
{
Serial.print(getDistance());
Serial.println(" cm ");
delay(500);
}
int getDistance() .
,
,
.
, , -
- .
,
. 12.27.
. 12.27. 12.3
43:
, , , -
.
284 12
12.3.
,
10 , . , ,
, :
// 43 -
//
int m1speed=6; // ,
int m2speed=5;
int m1direction=7; // ,
int m2direction=4;
int signal=3;
boolean crash=false;
void setup()
{
pinMode(m1direction, OUTPUT);
pinMode(m2direction, OUTPUT);
pinMode(signal, OUTPUT);
delay(5000);
Serial.begin(9600);
}
int getDistance()
//
// Ping)))
{
int distance;
unsigned long pulseduration=0;
// Ping)))
// ,
//
pinMode(signal, OUTPUT);
//
digitalWrite(signal, LOW);
delayMicroseconds(5);
// 5 ,
// Ping)))
digitalWrite(signal, HIGH);
delayMicroseconds(5);
digitalWrite(signal, LOW);
// ,
//
pinMode(signal, INPUT);
285
//
pulseduration=pulseIn(signal, HIGH);
//
pulseduration=pulseduration/2;
//
distance = int(pulseduration/29);
return distance;
}
void backUp()
{
digitalWrite(m1direction,LOW); //
digitalWrite(m2direction,LOW);
delay(2000);
digitalWrite(m1direction,HIGH); //
digitalWrite(m2direction,LOW);
analogWrite(m1speed, 200); //
analogWrite(m2speed, 200);
delay(2000);
analogWrite(m1speed, 0); //
analogWrite(m2speed, 0);
}
if (b>=duration)
{
move=false;
}
} while (move!=false);
//
analogWrite(m1speed, 0);
analogWrite(m2speed, 0);
}
void loop()
{
goForward(1000, 255);
}
() -
crash true,
10 . , ,
,
, .
, Arduino
. -
, -
.
,
.
,
.
.
.
13 Arduino GPS
:
99, GPS;
99 GPS;
99 GPS;
99 ;
99
.
, GPS
, -
microSD, .
GPS?
(Global Positioning System,
GPS) , -
GPS ,
. ,
GPS, -
.
Arduino
,
GPS
,
(
). GPS- . 13.1. GPS-
EM506
EM506, . 13.1.
288 13
, -
GPS SparkFun ( KIT-13199), . 13.2.
, , Arduino,
.
. 13.2.
30- ( SparkFun,
GPS-09123), . 13.3,
.
. 13.3. GPS-
Arduino GPS 289
GPS
GPS , -
GPS. GPS
, , , ,
.
.
, ,
.
GPS- -
, Arduino.
GPS, -
. 13.4.
. 13.4. GPS
GPS, -
DLINE, UART -
.
13.1.
13.1.
// 13.1
void setup()
{
Serial.begin(4800);
}
void loop()
{
byte a;
if ( Serial.available() > 0 )
{
a = Serial.read(); // GPS
Serial.write(a);
}
}
290 13
() ,
, (). ( , -
4800 (),
GPS.)
, UART. -
, GPS. , ,
GPS . 30 -
,
. ,
IDE 4800 .
, . 13.5.
. 13.5. , GPS
GPS Arduino ,
. ,
.
TinyGPS, http://www.arduiniana.org/libraries/tinygps/,
,
8.
44:
GPS
GPS. , GPS
, ,
, ,
. 13.6.
Arduino GPS 291
. 13.6.
Freetronics
Freetronics. -
http://www.freetronics.com.au/collections/display/products/lcd-keypad-shield/.
, -
LiquidCrystal.
,
GPS, GPS,
9 .
:
Arduino USB.
Freetronics ( ).
9 .
GPS SparkFun.
// 44 - GPS
#include <TinyGPS.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
// TinyGPS
TinyGPS gps;
void setup()
{
Serial.begin(4800);
lcd.begin(16, 2);
}
void loop()
{
byte a;
if ( Serial.available() > 0 ) //
{
a = Serial.read(); //
if(gps.encode(a)) // GPS...
{
getgps(gps); //
}
}
}
-
GPS. void loop () -
, GPS, getgps() (),
gps.f_get_position() &latitude
&longitude ( ).
GPS ,
,
. 13.7.
Arduino GPS 293
. 13.7. , 44
? -
Google Maps (http://maps.google.com/). Google Maps -
, , ,
Google Maps . , -
, . 13.7, Google Maps , . 13.8.
. 13.8. , . 13.7
45:
GPS
GPS ,
, .
294 13
, 44.
, GPS:
// 45 - GPS
#include <TinyGPS.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
// TinyGPS
TinyGPS gps;
void setup()
{
Serial.begin(4800);
lcd.begin(16, 2);
}
void getgps(TinyGPS &gps)
{
int year,a,t;
byte month, day, hour, minute, second, hundredths;
gps.crack_datetime(&year,&month,&day,
&hour,&minute,&second,&hundredths);
hour=hour+10; //
if (hour>23)
{
hour=hour-24;
}
lcd.setCursor(0,0); //
lcd.print("Current time: ");
lcd.setCursor(4,1);
if (hour<10)
{
lcd.print("0");
}
lcd.print(hour, DEC);
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
Arduino GPS 295
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
}
void loop()
{
byte a;
if ( Serial.available() > 0 ) //
{
a = Serial.read(); //
if(gps.encode(a)) // GPS...
{
getgps(gps); //
}
}
}
, 44, -
, ,
(). ,
, . -
().
. 13.9.
. 13.9. 45
46:
, GPS
, GPS-
microSD 8. -
.
microSD ,
296 13
, GPS,
.
, -
, , -
microSD 8
.
, .
// 46 -
//
#include <SD.h>
#include <TinyGPS.h>
// TinyGPS
TinyGPS gps;
void getgps(TinyGPS &gps);
void setup()
{
pinMode(10, OUTPUT);
Serial.begin(9600);
// microSD
//
if (!SD.begin(8)) {
Serial.println("Card failed, or not present");
//
return;
}
Serial.println("microSD card is ready");
}
void getgps(TinyGPS &gps)
{
float latitude, longitude;
int year;
byte month, day, hour, minute, second, hundredths;
//
gps.f_get_position(&latitude, &longitude);
File dataFile = SD.open("DATA.TXT", FILE_WRITE);
// ,
Arduino GPS 297
if (dataFile)
{
dataFile.print("Lat: ");
dataFile.print(latitude,5);
dataFile.print(" ");
dataFile.print("Long: ");
dataFile.print(longitude,5);
dataFile.print(" ");
//
gps.crack_datetime(&year,&month,&day,
&hour,&minute,&second,&hundredths);
// , 45
hour=hour+11;
if (hour>23)
{
hour=hour-24;
}
if (hour<10)
{
dataFile.print("0");
}
dataFile.print(hour, DEC);
dataFile.print(":");
if (minute<10)
{
dataFile.print("0");
}
dataFile.print(minute, DEC);
dataFile.print(":");
if (second<10)
{
dataFile.print("0");
}
dataFile.print(second, DEC);
dataFile.print(" ");
dataFile.print(gps.f_speed_kmph());
dataFile.println("km/h");
dataFile.close();
delay(30000); // 30
}
}
void loop()
{
byte a;
if ( Serial.available() > 0 ) //
{
a = Serial.read(); //
if(gps.encode(a)) // GPS...
{
298 13
getgps(gps); //
}
}
}
GPS-
, . 13.10.
. 13.10. 46
Google Maps,
, .
. , -
, . 13.11.
.csv.
- GPS Visualizer (http://www.gpsvisualizer.com/).
Get Started Now ( ) Browse
() Upload a GPS file ( GPS)
. Choose an output format ( )
Arduino GPS 299
. 13.11.
. 13.12. , GPS-
, , , , , -
GPS, Arduino.
: , -
.
14
, ,
. :
99 -
;
99 ;
99 XBee;
99 .
, Arduino
, ,
. 14.1. ,
, , RF Link (). -
WLS107B4B Seeed Studio WRL-10534
SparkFun. -
, 433 .
. 14.1 (
): 5 , , .
, , -
. ( -
, ,
, .)
. 14.2 , .
: V+ V -
5V GND , DATA
Arduino, .
301
. 14.1. . 14.2.
, -
VirtualWire, http://
www.airspayce.com/mikem/arduino/VirtualWire/. -
.
, -
, . -
, -
. , ,
, XBee , .
47:
-
: , Arduino, -
Arduino,
. -
,
,
, .
( 100 , -
-
, .)
:
Arduino USB.
9 ( 12).
, 433 (,
WRL-10534, SparkFun).
302 14
10 (R1 R2).
100 (C1 C2).
.
.
-
, 2 3, ,
, (. 14.3).
S1 S2
3V3 5V V in
D13
R1 R2
RST D12 10k 10k
PWM C1 C2
AREF D11 100nF 100nF VDD
IO REF Arduino D10
PWM
GND
PWM
N/C D9 DATA
/
D8
ANT
D7
PWM
D6
PWM
D5
A0 D4
PWM
A1 D3
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 14.3. 47
:
Arduino USB.
303
9 ( 12).
, 433 (,
WRL-10532, SparkFun).
.
.
560 (R1 R2).
, -
6 7. DATA 8,
. 14.4.
3V3 5V V in
D13
RST D12
PWM
AREF D11
D8 DATA
D7 ANT
PWM
D6
PWM
D5
A0 D4
PWM
A1 D3 R1 R2
560 560
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
LED1 LED2
SDA
GND
. 14.4. 47
304 14
Freetronics, -
433 (. 14.5).
. 14.5. Freetronics
Arduino
:
// 47 - .
//
#include <VirtualWire.h>
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
void setup()
{
vw_set_ptt_inverted(true); //
vw_setup(300); //
vw_set_tx_pin(8);
pinMode(2, INPUT);
pinMode(3, INPUT);
}
void loop()
{
if (digitalRead(2)==HIGH)
{
vw_send((uint8_t *)on2, strlen(on2)); //
vw_wait_tx(); //
delay(200);
305
}
if (digitalRead(2)==LOW)
{
vw_send((uint8_t *)off2, strlen(off2));
vw_wait_tx();
delay(200);
}
if (digitalRead(3)==HIGH)
{
vw_send((uint8_t *)on3, strlen(on3));
vw_wait_tx();
delay(200);
}
if (digitalRead(3)==LOW)
{
vw_send((uint8_t *)off3, strlen(off3));
vw_wait_tx();
delay(200);
}
}
VirtualWire ( ). 8 ( -
Arduino )
. (
, 0 1, .)
,
2 3, , -
. , ,
HIGH 2 , Arduino a,
b. ,
.
if, ,
, if-then (). , ,
, on2, :
vw_send on2,
,
strlen().
. -
Arduino :
306 14
// 47 - .
//
#include <VirtualWire.h>
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
void setup()
{
vw_set_ptt_inverted(true); //
vw_setup(300);
vw_set_rx_pin(8);
vw_rx_start();
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
if (vw_get_message(buf, &buflen))
{
switch(buf[0])
{
case 'a':
digitalWrite(6, HIGH);
break;
case 'b':
digitalWrite(6, LOW);
break;
case 'c':
digitalWrite(7, HIGH);
break;
case 'd':
digitalWrite(7, LOW);
break;
}
}
}
,
VirtualWire (),
() Arduino,
.
Arduino. vw_get_message() () -
, Arduino, -
switch-case (). , S1
a. , ,
HIGH 6 .
307
Arduino, -
,
.
XBee
-
-
,
, ,
XBee. -
, -
.
XBee, -
XBee Series 1,
. 14.6. . 14.6.
XBee
Arduino
XBee,
. 14.7.
XBee Explorer,
. 14.8 ( SparkFun, WRL-08687).
308 14
XBee ; -
, -
Arduino.
48:
XBee
Arduino -
, Explorer XBee. -
: XBee XBee,
Arduino.
-
XBee, . 14.9.
,
GPS, 13. -
USB
. 14.9. Arduino XBee.
DLINE,
XBee
UART.
// 48 - XBee
void setup()
{
Serial.begin(9600);
}
void loop()
309
{
Serial.println("Hello, world");
delay(1000);
}
Hello, world
XBee :
,
XBee. USB Arduino
, 9 , -
, 12. (
UART.)
. Explorer
(.14.8) XBee .
Terminal Windows, https://sites.
google.com/site/terminalbpp/. ,
. 14.10.
. 14.10.
310 14
, , Baud Rate ()
9600, Data Bits ( )
8, Parity ( ) none, Stop Bits (
) 1 Handshaking () none.
ReScan () ,
USB. , Connect
(), , -
Arduino, . 14.11.
. 14.11. XBee
XBee,
. ,
, -
.
Arduino XBee.
49:
, -
.
,
.
:
Arduino USB.
TMP36.
XBee (Series 1).
XBee Arduino.
XBee Explorer USB.
311
.
9 ( 12).
.
XBee ;
Arduino. TMP36,
. 14.12, 5V, GND A0
XBee ( 8).
. 14.12.
Arduino , ,
Explorer XBee USB .
// 49 -
char a;
float voltage=0;
float sensor=0;
float celsius=0;
float fahrenheit=0;
float photocell=0;
void setup()
{
Serial.begin(9600);
}
312 14
void sendC()
{
sensor=analogRead(0);
voltage=((sensor*5000)/1024);
voltage=voltage-500;
celsius=voltage/10;
fahrenheit=((celsius*1.8)+32);
Serial.print("Temperature: ");
Serial.print(celsius,2);
Serial.println(" degrees C");
}
void sendF()
{
sensor=analogRead(0);
voltage=((sensor*5000)/1024);
voltage=voltage-500;
celsius=voltage/10;
fahrenheit=((celsius*1.8)+32);
Serial.print("Temperature: ");
Serial.print(fahrenheit,2);
Serial.println(" degrees F");
}
void getCommand()
{
Serial.flush();
while (Serial.available() == 0)
{
// , XBee
}
while (Serial.available() > 0)
{
a = Serial.read(); //
}
}
void loop()
{
getCommand();//
switch (a)
{
case 'c':
//
sendC();
break;
case 'f':
//
sendF();
break;
}
}
313
, getCommand() ().
, , -
a.
switch-case (). c, sendC()
(), , -
XBees. -
f, sendF() (),
.
Terminal, , -
c f. 1
, Receive (), . 14.13.
. 14.13. 49
, -
Arduino. , ,
Arduino,
Arduino.
, ,
.
.
, Arduino
.
15
99 ;
99 Arduino;
99 , 12.
, Arduino -
.
?
-
, , . ()
-
.
- ,
.
, , -
-. ,
, ,
, .
315
- -
, .
, -,
.
-
, Arduino, -
-. -
https://github.com/shirriff/Arduino-IRremote/ ,
8.
-
- .
- (. 15.1) -
(. 15.2), .
. 15.1. - . 15.2.
, . -
Sony, . 15.3.
, ,
Sony; ,
.
. 15.3. Sony
. -
- Arduino 15.1.
15.1. -
// 15.1
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // -
}
void loop()
{
if (irrecv.decode(&results)) // - ?
{
Serial.print(results.value, HEX); // -
Serial.print(" ");
irrecv.resume(); //
}
}
317
, -
IRremote. , -
. ,
(). , IRremote
.
, ,
.
. , . 15.4 -
1, 2 3.
. 15.4. 15.1
. 15.1 ,
Sony, .
15.1, , .
- Sony,
. ,
. -
.
15.1. - Sony
Power (./) A90 7 610
Mute (. ) 290 8 10
1 10 9 110
2 810 0 910
3 410 Volume up ( ) 490
4 C10 Volume down ( ) C90
5 210 Channel up ( ) 90
6 A10 Channel down ( ) 890
318 15
50:
Arduino -
, -
-.
2- 7- 2 7
Sony.
HIGH 1 ,
LOW.
.
, -
- , -
LED, 28. (
,
.)
// 50 Arduino -
void setup()
{
irrecv.enableIRIn(); // -
for (int z = 2 ; z < 8 ; z++) //
{
pinMode(z, OUTPUT);
}
}
void translateIR()
// - Sony
//
{
switch(results.value)
{
case 0x810: pinOn(2); break; // 2
319
void loop()
{
if (irrecv.decode(&results)) // - ?
{
translateIR();
for (int z = 0 ; z < 2 ; z++) // 2- 3-
{
irrecv.resume(); //
}
}
}
. -
().
translateIR() ()
.
, switch -case
, IRremote.
, 15.1. ,
2 7, pinOn() (),
1 .
, Sony -
, (),
. ,
0x case ().
16,
0 9 A F. , 10 -
A, 15 F, 16 10,
. . , -
0x.
320 15
, -
.
15.1
switch...case.
51:
-
, -
, ,
40. ,
Sony.
,
, -, . -
:
2 , 8 , 4 6 .
-
:
// 51 - -
int m1speed = 6; //
int m2speed = 5;
int m1direction = 7; //
int m2direction = 4;
void setup()
{
pinMode(m1direction, OUTPUT);
pinMode(m2direction, OUTPUT);
irrecv.enableIRIn(); // -
321
// translateIR , -
void translateIR()
{
switch(results.value)
{
case 0x810: goForward(250, 255); break; // 2
322 15
void loop()
{
if (irrecv.decode(&results)) // - ?
{
translateIR();
for (int z = 0 ; z < 2 ; z++) //
{
irrecv.resume(); //
}
}
}
.
,
12.
Arduino
.
( , -
), ,
, .
.
Arduino ,
.
16
RFID
:
99, Arduino;
99 Arduino;
99 Arduino.
(Radio-Frequency Identification, RFID)
, -
. -
Arduino,
RFID,
-
. ,
RFID,
, -
, . 16.1. RFID
, -
, , . .16.1
RFID.
324 16
RFID ,
. -
;
, RFID.
,
. . 16.2 -
- RFID,
.
. 16.2. RFID
, ( RFR101A1M),
Seeed Studio: http://www.seeedstudio.com/.
, 125 ;
RFID, , , -
, 113990014 Seeed Studio.
RFID
Arduino ,
RFID .
RFID 325
. 16.3 RFID.
45
25
3.0
GND
VCC
POWER RX/D0
25 TX/D1
SIGNAL
JUMPER ANTENNA
U W
2.54
. 16.3. RFID
RFID Arduino -
, JUMPER.
RFID Arduino , :
1. ANTENNA.
2. GND RFID GND Arduino.
3. VCC RFID 5V Arduino.
4. RX RFID D0 Arduino.
5. TX RFID D1 Arduino.
Arduino RFID , -
RX RFID D0 Arduino. -
. , D0 Arduino
, .
16.1.
326 16
16.1. RFID
// 16.1
int data1 = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
data1 = Serial.read();
//
Serial.print(" ");
Serial.print(data1, DEC);
}
}
RFID . -
, . 16.4.
. 16.4. 16.1
, 14 . -
RFID, -
.
, , .
52:
RFID-
RFID . -
, Arduino
.
RFID; Accepted (
RFID 327
). ,
Rejected ( ). -
RFID .
. x
, ,
, . ( 6.)
// 52 RFID-
int data1 = 0;
int ok = -1;
// ,
// 16.1
int tag1[14] = {x, x, x, x, x, x, x, x, x, x, x, x, x, x};
int tag2[14] = {x, x, x, x, x, x, x, x, x, x, x, x, x, x};
//
int newtag[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void setup()
{
Serial.flush(); // ,
//
//
Serial.begin(9600);
}
//
//
void checkmytags()
{
ok = 0; //
// :
// 1 -
//
// 0 - ,
//
// -1 -
if (comparetag(newtag, tag1) == true)
{
ok++;
}
if (comparetag(newtag, tag2) == true)
{
ok++;
}
}
void loop()
{
ok = -1;
if (Serial.available() > 0) //
{
// RFID
delay(100); //
//
for (int z = 0 ; z < 14 ; z++) //
//
{
data1 = Serial.read();
newtag[z] = data1;
}
Serial.flush(); //
//
checkmytags();
}
//
if (ok > 0) //
{
Serial.println("Accepted");
ok = -1;
}
else if (ok == 0) //
{
Serial.println("Rejected");
ok = -1;
}
}
RFID 329
RFID ,
. 14 -
newtag[] ().
, , checkmytags() ( ),
comparetag()
().
comparetag() -
(true) (false).
ok 1 ( ). , -
.
, D0
Arduino RX RFID (. . 16.3), -
.
, . 16.5.
. 16.5. 52
Arduino,
.
, ?: -
, 9?
(
). -
ATmega328
.
Arduino 1024 , -
0 1023. , 0
330 16
255. ,
RFID.
, EEPROM
( Arduino IDE), :
#include <EEPROM.h>
EEPROM.write(a, b);
a ( 0 1023) ,
b a.
:
value = EEPROM.read(position);
position
value.
,
! Atmel,
100000 / . .
.
16.2.
16.2. ,
// 16.2
#include <EEPROM.h>
int zz;
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop()
{
Serial.println("Writing random numbers...");
for (int i = 0; i < 1024; i++)
RFID 331
{
zz = random(255);
EEPROM.write(i, zz);
}
Serial.println();
for (int a = 0; a < 1024; a++)
{
zz = EEPROM.read(a);
Serial.print("EEPROM position: ");
Serial.print(a);
Serial.print(" contains ");
Serial.println(zz);
delay(25);
}
}
0 255.
.
, , -
. 16.6.
. 16.6. 16.2
, .
53:
RFID-
52 , RFID -
, , , ,
332 16
.
, Arduino
, . ,
Arduino ,
, . .
(,
).
Arduino, ,
.
. , x -
, ,
52.
// 53 RFID-
//
#include <EEPROM.h>
int data1 = 0;
int ok = -1;
int lockStatus = 0;
// ,
// 16.1
int tag1[14] = {x, x, x, x, x, x, x, x, x, x, x, x, x, x};
int tag2[14] = {x, x, x, x, x, x, x, x, x, x, x, x, x, x};
//
int newtag[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void setup()
{
Serial.flush();
Serial.begin(9600);
pinMode(13, OUTPUT);
checkLock();
}
// comparetag true,
// ;
// RFID
boolean comparetag(int aa[14], int bb[14])
{
boolean ff = false;
int fg = 0;
RFID 333
//
//
void checkmytags()
{
ok = 0;
if (comparetag(newtag, tag1) == true)
{
ok++;
}
if (comparetag(newtag, tag2) == true)
{
ok++;
}
}
void checkLock()
{
Serial.print("System Status after restart ");
lockStatus = EEPROM.read(0);
if (lockStatus == 1)
{
Serial.println("- locked");
digitalWrite(13, HIGH);
}
if (lockStatus == 0)
{
Serial.println("- unlocked");
digitalWrite(13, LOW);
}
if ((lockStatus != 1) && (lockStatus != 0))
{
Serial.println("EEPROM fault - Replace Arduino hardware");
}
}
void loop()
{
ok = -1;
334 16
if (Serial.available() > 0) //
{
// RFID
delay(100);
for (int z = 0 ; z < 14 ; z++) //
//
{
data1 = Serial.read();
newtag[z] = data1;
}
Serial.flush(); //
//
checkmytags();
}
if (ok > 0) //
{
lockStatus = EEPROM.read(0);
if (lockStatus == 1) //
{
Serial.println("Status - unlocked");
digitalWrite(13, LOW);
EEPROM.write(0, 0);
}
if (lockStatus == 0)
{
Serial.println("Status - locked");
digitalWrite(13, HIGH);
EEPROM.write(0, 1);
}
if ((lockStatus != 1) && (lockStatus != 0))
{
Serial.println("EEPROM fault - Replace Arduino hardware");
}
}
else if (ok == 0) //
{
Serial.println("Incorrect tag");
ok = -1;
}
delay(500);
}
52.
, (-
) ()
RFID . -
RFID () . -
: 0 1 .
RFID 335
, ,
(
) .
, -
. , , 0 1,
(). ,
checkLock() (, , ),
,
( ).
,
Arduino, . -
RFID
,
RFID.
18.
17
:
99 I2C;
99, (
) I2C;
99 SPI;
99 SPI.
Arduino
,
.
Arduino , / -
.
Arduino : -
(Serial Peripheral Interface, SPI)
(Inter-Integrated Circuit, I2C).
.
I2C
I2C, (Two Wire Interface,
TWI), , . -
Arduino ,
(Serial Data Line, SDA) (Serial Clock
Line, SCL). Arduino Uno SDA A4,
SCL A5, . 17.1. R3
, I2C
.
337
I2C, Arduino ,
.
, Arduino
.
I2C, .
IC
.
Arduino 5 , I2C
5 . -
.
I2C, Wire (
Arduino IDE):
#include <Wire.h>
, void setup(), :
Wire.begin();
.
Arduino , :
1. , (
address ,
0x50):
Wire.beginTransmission(address);
2. 1 Arduino ,
. data -
, 1 ; ,
Wire.write():
Wire.write(data);
338 17
3. ,
:
Wire.endTransmission();
I2C, -
Wire.beginTransmission(address) :
Wire.requestFrom(address,x);
( x ).
:
Wire.endTransmission().
.
54:
16 , -
Arduino, -
.
1024
. -
, -
. 17.2.
Microchip .
Technology 24LC512
24LC512, Microchip Technology,
64 (65536 ) (. 17.2). -
, Digi-Key ( 24LC512-I/P-ND) element14
( 9758020).
, :
Microchip Technology 24LC512.
.
4,7 .
339
100 .
.
Arduino USB.
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
D8
/
D7
PWM
D6
PWM
D5
C1
100nF A0 D4
24LC512
PWM
A1 D3
1 8
A0 +V A2 D2
2 A1 WP 7 TX
A3 D1
3 6 RX
A2 SCL A4 D0
4 0V SDA 5 SCL
A5
R1 R2
4k7 4k7 SDA
GND
. 17.3. 54
24LC512 I2C -
. 3 A2, A1
340 17
A0 . 0;
5V 1.
1010.
I2C : 1010000 -
0x50 . ,
0x50.
64 ,
20
.
:
// 54 -
#include <Wire.h>
#define chip1 0x50
void setup()
{
Serial.begin(9600);
Wire.begin();
}
Wire.requestFrom(device,1);
result = Wire.read();
return result; //
// readData
}
void loop()
{
Serial.println("Writing data...");
for (int a=0; a<20; a++)
{
writeData(chip1,a,a);
}
Serial.println("Reading data...");
for (int a=0; a<20; a++)
{
Serial.print("EEPROM position ");
Serial.print(a);
Serial.print(" holds ");
d=readData(chip1,a);
Serial.println(d, DEC);
}
}
. -
I2C chip1 .
, I2C.
writeData() readData(),
, -
, -
I2C.
/ .
writeData() () ,
, ,
Wire.write(), .
readData() () ,
, Wire.requestFrom(), (). -
, , result
.
void loop() 20
(0 19) . 20 ,
20 , -
. 17.4.
342 17
. 17.4. 54
55:
, I2C.
/-
. 16- Microchip
Technology MCP23017 (. 17.5), 16 /.
, Newark ( 31K2959) element14
( 1332088).
MCP23017 Arduino , -
/ 16- Arduino.
/
/ Arduino.
, :
Arduino USB.
.
.
343
. 17.6 MCP23017.
54,
I2C, .
MCP23017 15- 17- GND, 0x20.
MCP23017 -
, . 17.7. ,
16 / : GPA7 GPA0
, GPB0 GPB7 . (
560 ) ,
, .
R1 R2 3V3 5V V in
4k7 4k7 D13
RST D12
PWM
15 14 AREF D11
16 13 IO REF Arduino D10
PWM
PWM
17 12 N/C D9
5V
/
18 11 D8
19 10 D7
PWM
20 9 D6
PWM
21 8 D5
A0 D4
22 7
PWM
23 6 A1 D3
24 5 A2 D2
TX
25 4 A3 D1
RX
26 3 A4 D0
27 2 A5 SCL
28 1 SDA
MCP23017 GND
. 17.6. 55
344 17
. 17.7. MCP23017
// 55 -
#include "Wire.h"
#define mcp23017 0x20
void setup()
{
Wire.begin(); // I2C
// / MCP23017
//
Wire.beginTransmission(mcp23017);
Wire.write(0x00); // IODIRA
Wire.write(0x00); // A -
Wire.write(0x00); // B -
Wire.endTransmission();
}
void loop()
{
Wire.beginTransmission(mcp23017);
Wire.write(0x12);
Wire.write(255); // A
Wire.write(255); // B
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(mcp23017);
Wire.write(0x12);
Wire.write(0); // A
Wire.write(0); // B
Wire.endTransmission();
delay(1000);
}
345
MCP23017 void
setup() . ,
, ;
, GPA0 GPA7, , -
GPB0 GPB7.
,
( -
6). ,
7- 4-, 11110000 (
240), Wire.write(), GPA0GPA7
GPB0GPB7.
, I2C. -
, , ,
I2C- Arduino.
SPI
SPI, I2C,
, -
. -
/: Arduino
, () .
,
SPI, , -
: MOSI (Master-Out,
Slave-In , -
), MISO (Master-In, Slave-Out -
, ), SCK ( )
SS CS (Slave Select Chip Select
).
SPI Arduino,
.17.8.
. 17.9
Arduino SPI. . 17.8.
Arduino Uno, -
D11 D13
SPI
MISO, MOSI SCK SPI,
SS
( D10,
, SPI).
346 17
D13 SCK
SPI D11 MOSI
SPI
D12 MISO
(,
Arduino Uno) D10 SS
I2C- SPI- 5 ,
Arduino 5 . -
.
SPI
, SPI.
. ,
SPI ( Arduino IDE):
#include "SPI.h"
, void setup ,
SS, . -
SPI-, D10
HIGH, SPI- -
, SS :
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
SPI:
SPI.begin();
, ,
. SPI- ,
(Most Significant Bit, MSB), , , ,
. (
6, , .)
void setup SPI.begin :
SPI.setBitOrder(order);
SPI-
SPI-, SS
LOW ,
( Arduino) .
, :
SPI.transfer(byte);
, HIGH
SS, , Arduino .
SPI- SS. ,
SPI , SS, ,
D9 Arduino, .17.10.
D13 SCK
SPI
D11 MOSI SPI
D12 MISO
(, 1
Arduino Uno) D10 SS
D9
SPI
2
D9 ( D10).
56 SPI -
.
56:
, ,
, 4,
, :
, . -
348 17
.
, -
. ,
, 20%.
56 Microchip Technology
MCP4162, . 17.11. MCP4162
;
10 .
, Newark (
77M2766) element14 ( 1840698). -
. 17.11. 255
Microchip Technology 40. -
MCP4162 2 : ( 0)
( 0 255).
MCP4162 , -
.
.
, :
Arduino USB.
.
.
Microchip Technology MCP4162.
560 .
.
. 17.12 56.
MCP4162 . 1
Microchip (. 17.11).
// 56 -
#include "SPI.h" //
int ss=10; // 10
349
R1
560
5
7
P0B
V dd
SDO
P0W
MCP4162
SCK
SDI
CS
V ss
1
4
LED1
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
/
D8
D7
PWM
D6
PWM
D5
A0 D4
PWM
A1 D3
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 17.12. 56
// SPI-
int del=200; //
//
void setup()
{
SPI.begin();
pinMode(ss, OUTPUT); //
// SS
digitalWrite(ss, HIGH); //
// SS,
//
SPI.setBitOrder(MSBFIRST);
// MCP4162
350 17
//
}
void loop()
{
for (int a=0; a<256; a++)
{
setValue(a);
delay(del);
}
for (int a=255; a>=0; a--)
{
setValue(a);
delay(del);
}
}
. , -
SPI ( ). ,
MPC4162. -
, (),
( 0 255) MCP4162. ,
, , -
() ().
, .
-
, Arduino.
-
, .
I2C- ,
,
18. , !
18
:
99
;
99 Arduino;
99 ;
99 , .
I2C-
(Real-Time Clock, RTC)
-,
-
Arduino.
RTC
-
-
.
-
, -
-
.
Maxim DS3232; . 18.1. I2C-
,
,
. DS3232
, : ,
Freetronics (http://www.freetronics.com/rtc/), . 18.1.
352 18
RTC
RTC Arduino , I2C
( 17). : GND VCC
GND 5V Arduino ,
SDA SCL A4 A5. - -
I2C -
, , Arduino, .
,
Pullups: SDA and SCL ( SDA SCL),
.
ProtoShield,
. ,
!
57:
, RTC
.
,
.
, :
Arduino USB.
.
Maxim DS3232 RTC.
Arduino, , -
, :
// 57 -
#include "Wire.h"
#define DS3232_I2C_ADDRESS 0x68
// -
353
// -
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setup()
{
Wire.begin();
Serial.begin(9600);
// :
// , , , , , ,
setDS3232time(0, 56, 23, 3, 30, 10, 12);
}
// DS3232, 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
354 18
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// DS3232
readDS3232time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth,
&month, &year);
//
Serial.print(hour, DEC);
//
Serial.print(":");
if (minute<10)
{
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second<10)
{
Serial.print("0");
}
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day of week: ");
switch(dayOfWeek){
case 1:
Serial.println("Sunday");
break;
case 2:
Serial.println("Monday");
break;
case 3:
Serial.println("Tuesday");
break;
case 4:
Serial.println("Wednesday");
break;
case 5:
Serial.println("Thursday");
break;
355
case 6:
Serial.println("Friday");
break;
case 7:
Serial.println("Saturday");
break;
}
}
void loop()
{
displayTime(); //
delay(1000); //
}
,
. I2C -
RTC 0x68.
- -
. , DS3232 -
.
setDS3232time RTC,
:
,
. dayOfWeek 1 7, -
, .
year , 2013
13. ( 20 .) -
( ) byte.
RTC setDS3232time ().
. ,
// setDS3232time,
,
!
, readDS3232time () RTC -
byte. , displayTime,
.
. -
, . 18.2.
356 18
. 18.2. 57
57 ,
. decToBcd, bcdToDec, readDS3232time setDS3232time
. -
Arduino:
.
58:
57 -
,
44 ( GPS).
, :
Arduino USB.
.
.
ProtoScrewShield .
Freetronics LCD.
( ).
, 57. -
RTC Arduino ,
ProtoScrewShield.
LCD .
357
PROTOSCREWSHIELD
, , -
. ,
ProtoScrewShield Arduino, Wingshield Industries
(http://wingshieldindustries.com/), . 18.3.
. 18.3. ProtoScrewShield
Arduino.
, -
, , -
, Arduino,
ProtoScrewShield.
// 58 -
#include "Wire.h"
#define DS3232_I2C_ADDRESS 0x68
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
// -
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
358 18
// -
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setup()
{
Wire.begin();
lcd.begin(16, 2);
// :
// , , , , , ,
//setDS3232time(0, 27, 0, 5, 15, 11, 12);
}
// DS3232, 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
359
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// DS3232
readDS3232time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth,
&month, &year);
// LCD
lcd.clear();
lcd.setCursor(4,0);
lcd.print(hour, DEC);
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.setCursor(0,1);
switch(dayOfWeek){
case 1:
lcd.print("Sun");
break;
case 2:
lcd.print("Mon");
break;
case 3:
lcd.print("Tue");
break;
case 4:
lcd.print("Wed");
break;
case 5:
lcd.print("Thu");
break;
case 6:
lcd.print("Fri");
break;
case 7:
lcd.print("Sat");
break;
}
lcd.print(" ");
lcd.print(dayOfMonth, DEC);
lcd.print("/");
lcd.print(month, DEC);
360 18
lcd.print("/");
lcd.print(year, DEC);
}
void loop()
{
displayTime(); //
delay(1000); //
}
, 57, -
, displayTime , ,
, LCD
(). ( ,
7.) , -
, ,
.
, . 18.4.
. 18.4. 58
, 57 58,
, -
. , , -
- .
59:
(
). , -
ProtoScrewShield -
, .
RFID
(, ). -
microSD .
361
, microSD, 13,
RFID 16 -
. .
, :
Arduino USB.
.
RTC ( ).
LCD Freetronics LCD.
MicroSD ( 13).
ProtoScrewShield .
RFID ( 16).
: Arduino Uno
ProtoScrewShield, microSD LCD.
RFID, 16, RTC
. . 18.5.
. 18.5.
362 18
.
, RX RFID D0
Arduino, .
// 59 -
#include "Wire.h" // RTC
#define DS3232_I2C_ADDRESS 0x68
#include "SD.h" // microSD
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
int data1 = 0;
//
// 16.1
int Mary[14] = {
2, 52, 48, 48, 48, 56, 54, 67, 54, 54, 66, 54, 66, 3};
int John[14] = {
2, 52, 48, 48, 48, 56, 54, 66, 49, 52, 70, 51, 56, 3};
int newtag[14] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //
// -
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// -
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
Wire.endTransmission();
}
// 7 DS3232, 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void setup()
{
Serial.flush(); //
Serial.begin(9600);
Wire.begin();
lcd.begin(16, 2);
// :
// , , , , , ,
//setDS3232time(0, 27, 0, 5, 15, 11, 12);
// microSD
if (!SD.begin(8))
{
lcd.print("uSD card failure");
//
return;
}
lcd.print("uSD card OK");
delay(1000);
lcd.clear();
}
// true,
//
//
boolean comparetag(int aa[14], int bb[14])
{
boolean ff=false;
int fg=0;
for (int cc=0; cc<14; cc++)
364 18
{
if (aa[cc]==bb[cc])
{
fg++;
}
}
if (fg==14)
{
ff=true; // 14
}
return ff;
}
void wipeNewTag()
{
for (int i=0; i<=14; i++)
{
newtag[i]=0;
}
}
void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
if (Serial.available() > 0) //
{
// RFID
delay(100); //
//
for (int z=0; z<14; z++) //
//
{
data1=Serial.read();
newtag[z]=data1;
}
Serial.flush(); //
// DS3232
readDS3232time(&second, &minute, &hour, &dayOfWeek,
&dayOfMonth, &month, &year);
}
//
if (comparetag(newtag, Mary) == true)
{
lcd.print("Hello Mary ");
File dataFile = SD.open("DATA.TXT", FILE_WRITE);
if (dataFile)
{
dataFile.print("Mary ");
dataFile.print(hour);
dataFile.print(":");
if (minute<10) { dataFile.print("0"); }
365
dataFile.print(minute);
dataFile.print(":");
if (second<10) { dataFile.print("0"); }
dataFile.print(second);
dataFile.print(" ");
dataFile.print(dayOfMonth);
dataFile.print("/");
dataFile.print(month);
dataFile.print("/");
dataFile.print(year);
dataFile.println();
dataFile.close();
}
delay(1000);
lcd.clear();
wipeNewTag();
}
if (comparetag(newtag, John)==true)
{
lcd.print("Hello John ");
File dataFile = SD.open("DATA.TXT", FILE_WRITE);
if (dataFile)
{
dataFile.print("John ");
dataFile.print(hour);
dataFile.print(":");
if (minute<10) { dataFile.print("0"); }
dataFile.print(minute);
dataFile.print(":");
if (second<10) { dataFile.print("0"); }
dataFile.print(second);
dataFile.print(" ");
dataFile.print(dayOfMonth);
dataFile.print("/");
dataFile.print(month);
dataFile.print("/");
dataFile.print(year);
dataFile.println();
dataFile.close();
}
delay(1000);
lcd.clear();
wipeNewTag();
}
}
, RFID
. RFID , microSD
, .
366 18
I2C
,
microSD. microSD.
RFID
(John)
(Mary).
microSD.
, () -
.
, data.txt
microSD
. ,
, . 18.6.
. 18.6. , 59
, I2C-
RTC. RFID, 59,
, .
Arduino,
.
19
99 - -;
99 Arduino Twitter;
99,
Arduino -.
, Arduino .
Arduino -
-.
, , :
, .
. Ethernet -
W5100. : -
Ethernet, Arduino
. 19.1, , Arduino Uno,
Ethernet, Freetronics EtherTen, . 19.2.
, ,
. . 19.2, EtherTen
, USB,
Ethernet microSD.
368 19
-
10/100 CAT5, CAT5E CAT6
Ethernet .
, IP- , -
: 192.168.0.1, IP-
, , IP- .
, Arduino
, , -
IP-. IP-,
(Internet Service
369
60:
, , ,
. ,
-,
, .
0-
9- - -
.
, , ,
-.
, :
USB.
.
Arduino Uno Ethernet
Freetronic EtherTen.
, :
/* 60
18 2009 (David A. Mellis),
9 2012 (Tom Igoe)
20 2013 (John Boxall)
*/
#include <SPI.h>
370 19
#include <Ethernet.h>
void loop()
{
// ( -)
EthernetClient client = server.available();
if (client) {
// http-
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// - Refresh,
// 5 :
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
//
for (int analogChannel = 0; analogChannel < 6; analogChannel++)
{
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
// 0 - 9
for (int digitalChannel = 0; digitalChannel < 10; digitalChannel++)
{
boolean pinStatus = digitalRead(digitalChannel);
client.print("digital pin ");
client.print(digitalChannel);
371
client.print(" is ");
client.print(pinStatus);
client.println("<br />");
}
client.println("</html>");
break;
}
if (c == '\n') {
//
currentLineIsBlank = true;
}
else if (c != '\r') {
// -
currentLineIsBlank = false;
}
}
}
//
delay(1);
// :
client.stop();
}
}
. , ,
IP- Ethernet,
.
IP- . ,
192.168.0.1, -
1 254, IP-, -
. , :
IPAddress ip(192, 168, 0, 69); // IP- Ethernet
. Ethernet
Arduino,
Ethernet Arduino.
20 - -
IP-, .
, . 19.3, -
.
, :
IP- .
.
372 19
. 19.3.
-
-
. , ,
. ,
Arduino .
- -
, , Wi-Fi,
.
Ethernet -
Arduino Ethernet -
, .
,
. -
Ethernet,
void setup.
client.print, , -
-. , client.print client.
println -,
. ,
-, . 19.3:
-. HTML -
-,
Arduino. ,
HTML, ,
Arduino. (
. 11.2.)
, , MAC-,
, .
, , MAC-,
.
Arduino, MAC- .
, - ,
, , -
, ,
(port forwarding)
IP-, , no-ip (http://www.no-
ip.com/) Dyn (http://dyn.com/dns/).
-,
http://www.wikihow.com/Port-Forward/,
.
, ,
-, Twitter
Arduino.
61:
Arduino Tweeter
Twitter Arduino.
, ,
, Twitter.
, , ,
- , ,
, .
Arduino Twitter,
:
1. http://twitter.com/ Twitter
Arduino. .
374 19
2. - http://arduino-tweet.appspot.com/,
Arduino Twitter.
.
3. (
Twitter Arduino) .
4. Twitter Arduino, http://
playground.arduino.cc/Code/TwitterLibrary/.
, :
USB.
.
Arduino Uno Ethernet Freetro
nic EtherTen.
, :
// 61 - Arduino Tweeter
#include <SPI.h>
#include <Ethernet.h>
#include <Twitter.h>
void setup()
{
delay(30000);
Ethernet.begin(mac, ip);
Serial.begin(9600);
}
void loop()
{
Serial.println("connecting ...");
if (twitter.post(msg)) {
int status = twitter.wait();
if (status == 200) {
Serial.println("OK.");
375
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("connection failed.");
}
do {} while (1);
}
. 19.4. , Arduino
, ,
. ( -
Twitter.)
Twitter . -
, , , . 19.5. 403 . 19.5
,
. ( Twitter
http://dev.twitter.com/docs/error-codes-responses/.)
. 19.5.
376 19
Arduino
Arduino -
. , ,
: Teleduino.
Teleduino , (Nathan Kennedy)
Arduino . ,
Arduino .
; ,
Arduino, -
URL. Teleduino -
I2C-, -
. 62 ,
Teleduino ,
.
62:
Arduino
Teleduino, -
Teleduino
Arduino. https://www.teleduino.org/tools/
request-key/ .
, : 187654321Z9
AEFF952ABCDEF8534B2BBF.
, https://www.teleduino.
org/tools/arduino-sketch-key/. ,
, . 19.6.
. 19.6. Teleduino
Arduino ,
,
Teleduino.
377
, :
USB.
.
Arduino Uno Ethernet
Freetronic EtherTen.
560 (R1).
.
.
, . 19.7, -
8.
(AREF) MISO
MOSI
SS
Ethernet (D9)
PWM
/
(D8)
R1
(D7) 560
PWM
(D6)
PWM
(A0) (D5)
(A1) SS
LED1
PWM
(A2) (D3)
(A3) (D2)
TX
(A4) (D1)
RX
(A5) (D0)
(GND)
. 19.7. 62
378 19
Teleduino ,
Teleduino. , :
1. Teleduino, https://
www.teleduino.org/downloads/.
2. Arduino IDE File
ExamplesTeleduino328TeleduinoEthernetClientProxy (Teledu-
ino328TeleduinoEthernetClientProxy).
3. Teleduino. Arduino,
, . -
, , , 36. -
, Arduino.
.
. -
Teleduino, . 19.1.
19.1. Teleduino
1
2
3 Teleduino
4
5
6
10
, ,
Arduino, Teleduino.
10, .
Arduino ,
5 .
8, , -
Teleduino.
379
Arduino
Arduino Teleduino
-.
URL- http://us01.proxy.teleduino.org/
api/1.0/328.php?k=<YOURKEY>&r=setDigitalOutput&pin=<X>&output=<S>.
URL-: -, <YOURKEY>
- , Teleduino. -, -
<X> . -, <S> 0, -
LOW, 1, HIGH,
. , 7
HIGH, http://us01.proxy.teleduino.org/api/1.0/328.php?k=<YOUR
KEY>&r=setDigitalOutput&pin=7&output=1.
-
:
{"status":200,"message":"OK","response"
{"result":0,"time":0.22814512252808,"values":[]}}
,
:
,
URL-. URL-
-
, . ,
URL-, HIGH 7,
LOW .
Arduino .
Arduino - -
. Teleduino, https://www.
teleduino.org/tools/manage-presets/.
, -
, . 19.8.
380 19
. 19.8.
Arduino -
Twitter,
, , -
-
. ,
Arduino
. ,
, ,
.
, , , Arduino -
.
20
, :
99 Arduino ;
99 Arduino ;
99 , Arduino,
.
Arduino ,
Arduino -
. ,
, .
,
, ,
. C :
99 : GSM 850 , GSM 900 , DCS 1800 PCS
1900 .
99 , .
, , -
, .
, , ,
, .
,
, -
SMS (Short Message Service). , SIM-
(Subscriber Identification Module ) -
PIN- (Personal Identification Number ).
( SIM-
, PIN-.)
382 20
- , ,
Arduino Uno (
Arduino Mega ).
, .
,
SM5100 GSM , . 20.1.
SparkFun . (
CEL-09607, (stackable header
set) PRT-10007 CEL-00675.)
. 20.1. GSM
.
GSM 2 ( Arduino)
Arduino,
. , Arduino,
DFRobot (http://www.dfrobot.com/). , ,
DFR0105, . 20.2.
, .
(,
7,2 , , , -
12 , :
35 ),
2 .
383
. 20.2. DFRobot
( )
. -
,
( . 20.2 ).
PWRIN ( . 20.2 ).
PWRIN,
. :
+, .
, PWROUT
,
, . 20.3.
. 20.3.
384 20
-
, 5 . ,
.
:
1. SIM- GSM.
2. GSM Arduino Uno.
3. .
4.
PWROUT 5V,
PWROUT GND. -
5 Arduino.
USB,
Arduino . USB -
.
. ,
GSM Arduino.
SIM- :
20.1. GSM
// 20.1
// (Ryan Owens) - SparkFun CC, v3.0 08.03.10
#include <SoftwareSerial.h> //
SoftwareSerial cell(2,3);
char incoming_char = 0;
void setup()
{
// .
Serial.begin(9600);
cell.begin(9600);
Serial.println("Starting SM5100B Communication...");
}
void loop()
{
// GSM...
if( cell.available() > 0 )
{
// , GSM.
incoming_char = cell.read();
385
// .
Serial.print(incoming_char);
}
// ...
if( Serial.available() > 0 )
{
incoming_char = Serial.read();//
cell.print(incoming_char); // GSM.
}
}
, ,
GSM, . GSM
Arduino,
2 3, -
Arduino ,
0 1. GSM
SoftwareSerial (, ). (
Arduino IDE.)
, 30 .
, . 20.4.
, +SIND: -
GSM .
+SIND: 4, ,
, -
63. +SIND: 8, ,
, .
. 20.4. 20.1
386 20
GSM, :
1. Arduino IDE .
, 48.
2. COM-, Arduino,
Connect ().
3. . 20.1 , GSM-
, .
20.1. , GSM
GSM 900 0
DCS 1800 1
PCS 1900 2
GSM 850 3
GSM 900 DCS 1800 4
GSM 850 GSM 900 5
GSM 850 DCS 1800 6
GSM 850 PCS 1900 7
GSM 900 PCS 1900 8
GSM 850 , GSM 900 DCS 1800 9
GSM 850 , GSM 900 PCS 1900 10
4. AT+SBAND? ENTER.
, . 20.5.
. 20.5.
387
5. GSM,
AT+SBAND=x, x , . 20.1.
OK, . 20.6.
. 20.6.
6. , Arduino , -
, +SIND: 4.
GND RST Arduino ,
,
, . 20.7.
. 20.7.
SBAND ,
GSM. ,
, .
:
, SIM- GSM.
, ,
GSM, . 20.8.
388 20
( , 0452280886, ,
.)
63:
Arduino
, . ,
-
, Arduino 20 -
. Arduino
, , .
, , -
-
. ,
.
, ,
:
.
10 .
100 .
.
.
, . 20.9.
389
3V3 5V V in
D13
RST D12
PWM
AREF D11
PWM
N/C D9
/
D8
D7
PWM
D6 10k0
PWM 100nF
D5
A0 D4
PWM
A1 D3
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 20.9. 63
, :
// 63 -
#include <SoftwareSerial.h>
SoftwareSerial cell(2,3);
void setup()
{
pinMode(7, INPUT);
cell.begin(9600);
delay(30000); // GSM
}
void callSomeone()
{
390 20
cell.println("ATDxxxxxxxxxx"); // xxxxxxxxxx
// xxxxxxxxxx ( )
delay(20000); // 20 .
cell.println("ATH"); //
delay(60000); // 60 , GSM
}
void loop()
{
if (digitalRead(7) == HIGH)
{
callSomeone();
}
}
void setup GSM ()
. Arduino
, 7.
callSomeone (),
GSM ().
xxxxxxxxxx ,
Arduino. ,
. , , Arduino 212-555-12-
12, , :
cell.println("ATD2125551212");
,
.
, ().
, Arduino -
.
Arduino XXI , .
64:
Arduino
. , -
SerialGSM, https://github.com/meirm/SerialGSM/.
Arduino IDE.
,
63.
391
Arduino IDE, :
// 64 -
#include <SerialGSM.h>
#include <SoftwareSerial.h>
SerialGSM cell(2,3);
void setup()
{
pinMode(7, INPUT);
delay(30000); // GSM
cell.begin(9600);
}
void textSomeone()
{
cell.Verbose(true); //
cell.Boot();
cell.FwdSMS2Serial();
cell.Rcpt("+xxxxxxxxxxx"); // xxxxxxxxxx
//
cell.Message("This is the contents of a text message");
cell.SendSMS();
}
void loop()
{
if (digitalRead(7) == HIGH)
{
textSomeone();
}
if (cell.ReceiveSMS())
{
Serial.println(cell.Message());
cell.DeleteAllSMS();
}
}
GSM (), void setup()
.
textSomeone .
, .
, xxxxxxxxxx -
: ,
. ,
212-555-12-12 , +12125551212.
392 20
, . ( -
, 160 1.)
, ,
30 .
, . 20.10.
. 20.10.
64
, -
switch-case.
, , -
SIM-
.
65:
Arduino, -
. ,
.
,
.
( 10- 13-),
Arduino : #axbxcxdx, x
0 1. , ,
#a1b1c1d1.
, , 70 . .
1
.
393
, , -
.
.
:
.
560 .
.
.
, . 20.11.
R4
3V3 5V V in 560
D13
R3
560
RST D12
R2
PWM 560
AREF D11 R1
560
IO REF Arduino D10
PWM
PWM
N/C D9
LED4 LED3 LED2 LED1
/
D8
D7
PWM
D6
PWM
D5
A0 D4
PWM
A1 D3
A2 D2
TX
A3 D1
RX
A4 D0
A5 SCL
SDA
GND
. 20.11. 65
394 20
// 65 -
//
#include <SoftwareSerial.h>
SoftwareSerial cell(2,3);
char inchar;
void setup()
{
//
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(10, LOW); //
digitalWrite(11, LOW); //
digitalWrite(12, LOW); //
digitalWrite(13, LOW); //
// GSM.
cell.begin(9600);
delay(30000);
cell.println("AT+CMGF=1");
delay(200);
cell.println("AT+CNMI=3,3,0,0");
delay(200);
}
void loop()
{
// GSM...
if(cell.available() > 0)
{
inchar = cell.read();
if (inchar == '#') //
{
delay(10);
inchar = cell.read();
if (inchar == 'a')
{
delay(10);
inchar = cell.read();
if (inchar == '0')
{
digitalWrite(10, LOW);
}
else if (inchar == '1')
395
{
digitalWrite(10, HIGH);
}
delay(10);
inchar = cell.read();
if (inchar == 'b')
{
inchar = cell.read();
if (inchar == '0')
{
digitalWrite(11, LOW);
}
else if (inchar == '1')
{
digitalWrite(11, HIGH);
}
delay(10);
inchar = cell.read();
if (inchar == 'c')
{
inchar = cell.read();
if (inchar == '0')
{
digitalWrite(12, LOW);
}
else if (inchar == '1')
{
digitalWrite(12, HIGH);
}
delay(10);
inchar = cell.read();
if (inchar == 'd')
{
delay(10);
inchar = cell.read();
if (inchar == '0')
{
digitalWrite(13, LOW);
}
else if (inchar == '1')
{
digitalWrite(13, HIGH);
}
delay(10);
}
}
cell.println("AT+CMGD=1,4"); // SMS
}
}
}
}
}
396 20
Arduino ,
GSM. GSM -
SMS
(). Arduino
GSM ().
, -
GSM Arduino, #,
().
: 0 1,
.
, b, c d.
, -
, , , ,
.
, , -
,
.
,
-
. : ,
.
, 65 (, , ),
, ,
Arduino. -
,
, ,
.
, ,
- http://www.tronixforum.
com/?forum=376318.
. -
Arduino (, Arduino: http://
forum.arduino.cc/1) .
!
Arduino: http://arduino.ru/. -
1
. .
, ,
comp@piter.com ( , ).
!
, , http://www.
piter.com.
- http://www.piter.com
.
.
Arduino. 65
.
.
.
.
.
. , .
.
, 192102, -, . (. ), 3, , . 7.
034-2014, 58.11.12
, .
06.09.16. 70100/16. . . . . 32,250. 1200. 0000.
. .
142300, , . , . , 1.
: www.chpk.ru. E-mail: marketing@chpk.ru
: 8(496) 726-54-10, : (495) 988-63-87