\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 &amp; _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,&amp;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&lt;=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 &gt; 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 &lt;stdarg.h&gt;
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}
