-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrata.tex
More file actions
298 lines (255 loc) · 9.17 KB
/
errata.tex
File metadata and controls
298 lines (255 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\begin{document}
\chapter{Chapter 4: The Microcontroller}
<h2>``Hello World!'' is the Hard Part</h2>
I dont actually subscribe to this supposition. but thats ok.
\section{Identifying the Pins on the Microcontroller (P51)}
\includegraphics[scale=0.25]{arduino_leonardo_pinout}
\section{Hello (P57)}
The BASIC Stamp-like environments described in this section can be replaced by the Arduino IDE. After downloading and running the software according to the instructions at arduino.cc\footnote{http://arduino.cc/en/Main/Software} you should select the correct board and serial port from the tools menu and then select
File?Examples?01.Basics?Blink
\begin{verbatim}
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards including the Leonardo.
// Give it a name:
int led = 13;
// The setup routine runs once when you press the reset button:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// The loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
\end{verbatim}
\chapter{Chapter 5: Programming}
Chapter 5 is a lot of good grounding but its all written in dialects of basic. Here we have to do a little explaining as well as translating the code. I am splitting the chapter in half for this reason.
\section{Loops (p66)}
The event loop that the book refers to is hidden by the Arduino IDE but the actual event loop used by the Arduino core version 1.0.3 uses a ``for'' loop.
\begin{verbatim}
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
\end{verbatim}
This can also be written as
\begin{verbatim}
while(true) {
loop();
if (serialEventRun) serialEventRun();
}
\end{verbatim}
\section{{\texttt If} Statements (p67)}
\begin{verbatim}
if (in6 == 1) {
/*Statement1*/;
}
/*Statement2*/
\end{verbatim}
Please note the \verb|==| to indicate testing for equality. The statement
\verb|if (in6 = 1)|
would set the value of \verb|in6| to 1 and return 1, which would be true.
The brackets above are optional if only one statement is part of the condition as you can see in the code examples above however they do remove any potential ambiguity and I highly suggest you use them
\section{Variables (p69)}
In the C language, variables must be declared with a type. They can be initialized and declared at the same time. In standard C they must be declared at the top of a function or, if global, outside of the function. In C++ (which the Arduino uses) they can be inserted as needed.
\begin{verbatim}
int Date=12;
int ticketValue = 250;
int Fare=125
\end{verbatim}
Variable type sizes can be different between machines. In general an int is a short int which is usually 16 bits and a character is 8 bits but this is not guaranteed. Also integers can be either signed or unsigned. The Arduino provides some types that help you specify exactly what you mean.
\begin{verbatim}
uint8_t Sensor; // an 8 bit unsigned variable
uint8_t Ticket
uint16_t Bigger; // this is a 16 bit unsigned variable
\end{verbatim}
The code on page71 could be written like this:
\begin{verbatim}
uint8_t ticketsSubmitted=0;
uint8_t ticketSensor=0;
void loop() {
if (ticketSensor==1) {
ticketsSubmitted++;
}
if (ticketSubmitted ==3) {
OpenGate();
ticketsSubmitted=0;
}
}
\end{verbatim}
\section{Built-in Routines: Subroutines and Functions.}
In the C language, subroutines are functions which return void as a result. The Arduino core provides a rich set of built in functions to interact easily with pins. In addition the underlying GCC compiler provides \verb|define| statements, allowing you to interact directly with the processor's registers.
\begin{verbatim}
digitalWrite(13,HIGH);
PORTB |= _BV(0);
\end{verbatim}
Both statements set pin 0 of port B on the Arduino to high.
\subsection{Constants (p73)}
Constants in C are handled by the preprocessor which replaces the value before compiling the code. Unlike variables, preprocessor macros do not cost you any memory to store the values. C convention has the macro name in all caps.
The two statements below:
\begin{verbatim}
#define MIN_PULSE 100
PulseWidth = MIN_PULSE + angle;
\end{verbatim}
Would expand to:
\begin{verbatim}
PulseWidth = 100 + angle;
\end{verbatim}
\subsection{Using the pins (p74)}
\begin{verbatim}
#define MY_FAVORITE_PIN_NO 14
#define MY_FAVORITE_PINS PINB
#define MY_FAVORITE_PIN PINB0
printState=digitalRead(MY_FAVORITE_PIN_NO);
printState=MY_FAVORITE_PINS & _BV(MY_FAVORITE_PIN); // similar to the above
\end{verbatim}
C values are passed to routines through a stack and they become local to the function and go away when it exits. To get a function to modify a value like the examples at the bottom of page 74 you would pass the address of the value that needs to be changed. This is called ``pass by reference''.
\begin{verbatim}
int sensor;
rctime(5,1,&sensor); // the and here means "the memory address of sensor"
\end{verbatim}
\subsection{Homemade Routines}
In C, you can not use a function until you have declared it. The Arduino hides this from you by adding the declarations for you. For this reason you can write
\begin{verbatim}
#define THANK_YOU_LIGHT 13
void setup() {
// your setup goes here.
}
void loop() {
if (theySneezeOnYou) {
myThankYouRoutine();
}
if (theySneezeOnYou) {
myThankYouRoutine();
}
if (theySneezeOnYou) {
myThankYouRoutine();
}
}
void myThankYouRoutine() {
digitalWrite(THANK_YOU_LIGHT, HIGH);
delay(1000);
digitalWrite(THANK_YOU_LIGHT, LOW);
delay(1000);
};
\end{verbatim}
However it is good form and never hurts to declare them.
\begin{verbatim}
#define THANK_YOU_LIGHT 13
// routines defined in this file.
void myThankYouRoutine();
void setup();
void loop();
void setup() {
...
\end{verbatim}
\subsection{Advanced Loops}
The code below is the main program file from Arduino 22.
\begin{verbatim}
#include <Wprogram.h>
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
\end{verbatim}
This uses a form of C's \verb|for| loop. This could have been done with a \verb|while(true)| statement as well.
\subsubsection{While-Wend Do-While(p77)}
\begin{verbatim}
while(digitalRead(5)==0) {
digitalWrite(6,HIGH);
delay(250);
digitalWrite(6,LOW);
delay(250);
}
\end{verbatim}
C also supports a do until loop structure.
\begin{verbatim}
do {
//.... stuff to do here ...
} until (someThinIsTrue);
\end{verbatim}
\subsection{For-Next (p78)}
The basic code on p 78 would look like this in Arduino.
\begin{verbatim}
digitalWrite(5,HIGH);
delay(1000);
digitalWrite(6,HIGH);
delay(1000);
digitalWrite(7,HIGH);
delay(1000);
\end{verbatim}
Which in a C for loop would look more like this.
\begin{verbatim}
//... fragment ...
uint8_t counter;
//... later ...
for (counter=0;counter<=15;counter++) {
digitalWrite(counter+5,HIGH);
delay(1000);
}
\end{verbatim}
The for loop in C is a little more flexible than the basic for .. next as we have already seen.
Its basic form is
\begin{verbatim}
for( initialexpression; testexpression; iterateexpression)
{
/*stuff to do*/;
}
\end{verbatim}
Where \verb|initial_expression| is executed before the loop, then \verb|testexpression| is evaluated to see if it is true, if if the stuff to do is done followed by \verb|iterateexpression| .<br />
\emph{Note: that any or all of these expressions can be omitted and that if the test expression is omitted then it evaluates to true.}
\section{Comments (p81)}
Comments in C come in the traditional form /* comment */ and the newer C++ style // comment
\begin{verbatim}
/*
* This is a multi line comment.
* The next line is starts the main "loop"
*/
void loop() {
if (ticketValue > 0){ // check the tickets value
takeFare()
} // endif
} /* end of main "loop" */
\end{verbatim}
\section{Debugging (p82)}
There is a common convention in the Arduino to assume that all debugging information should be printed to using Serial.print() and Serial.println(), this is a bad habit that you will see in almost every Arduino program.
\begin{verbatim}
serial.println("Hello World");
Serial.println("start of routine");
Serial.print("fare = ");
Serial.println(fare, DEC);
\end{verbatim}
With a little work a more flexible system can be worked out that not only distinguishes between printing and debugging but also allows you to point your debuggin messages where you want (or even turn them off).
\begin{verbatim}
#include <stdarg.h>
void SerialPrintFormatted(char *fmt, ... ){
char tmp[128]; // resulting string limited to 128 chars
va_list args;
va_start (args, fmt );
vsnprintf(tmp, 128, fmt, args);
va_end (args);
Serial.println(tmp);
}
#define DEBUG(...) SerialPrintFormatted(__VA_ARGS__);
\end{verbatim}
Then you can write
\begin{verbatim}
serial.println("Hello World");
DEBUG("start of routine");
DEBUG("fare = %d", fare);
\end{verbatim}
\end{document}