From 704bc6a6ec830cae96c6a30081335c9423f061ea Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 16 Oct 2014 23:21:56 -0700 Subject: initial commit - added two chapters, cleaned up some formatting (incomplete) --- errata.tex | 245 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 errata.tex diff --git a/errata.tex b/errata.tex new file mode 100644 index 0000000..55ccc95 --- /dev/null +++ b/errata.tex @@ -0,0 +1,245 @@ +\documentclass{article} +\usepackage[utf8]{inputenc} +\begin{document} +\section{Chapter 4: The Microcontroller} +

``Hello World!'' is the Hard Part

+

I dont actually subscribe to this supposition. but thats ok.

+

Identifying the Pins on the Microcontroller (P51)

+Atmega168PinMap2 + +

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#toc1 you should select the correct board and serial port from the tools menue 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} +\endsection +\section{Chapter 5: Programming} +

physical-computingErrata for 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. + +

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 could also be written as +\begin{verbatim} +while(true) + loop(); + if (serialEventRun) serialEventRun(); +} +\end{verbatim} + +

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
+
+

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} + + +

Built-in Routines: Subroutines and Functions.

+

In ‘c’ 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 provides defines allowing you to interact directly with the processors registers.

+
digitalWrite(13,HIGH);
+PORTB |= _BV(0);
+

Both of the above set pin 0 of port b on the arduino to high.

+

Constants (p73)

+

Constants in c are handled by the pre processor which replaces the value before compiling the code. Unlike using variables, using preprocessor macroes does not cost you any memory to store the values. C Convention has macros in all caps.

+
#define MIN_PULSE 100
+PulseWidth = MIN_PULSE + angle;
+

Which would expand to

+
PulseWidth = 100 + angle;
+

(p74)

+
#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); // does more or less the same as the above.
+

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”

+
int sensor;
+rctime(5,1,&sensor); // the and here means "the memory address of sensor"
+

Homemade RoutinesHomemade 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.

+
#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);
+};
+

However it is good form and never hurts to declare them.

+
#define THANK_YOU_LIGHT 13
+// routines defined in this file.
+void myThankYouRoutine();
+void setup();
+void loop();
+
+void setup() {
+   ...
+

Advanced Loops:

+

The code below is the main program file from Arduino 22.

+
#include <Wprogram.h>
+
+int main(void)
+{
+	init();
+	setup();
+	for (;;)
+		loop();  
+	return 0;
+}
+

It uses a form of C’s for loop. This could have been done with a while(true) statement as well.

+

While-Wend Do-While(p77)

+
while(digitalRead(5)==0) {
+    digitalWrite(6,HIGH);
+    delay(250);
+    digitalWrite(6,LOW);
+    delay(250);
+}
+

C also supports a do until loop structure.

+
do {
+    //.... stuff to do here ...
+} until (someThinIsTrue);
+

For-Next (p78)

+

The basic code on p 78 would look like this in Arduino.

+
digitalWrite(5,HIGH);
+delay(1000);
+digitalWrite(6,HIGH);
+delay(1000);
+digitalWrite(7,HIGH);
+delay(1000);
+

Which in a C for loop would look more like this.

+
//... fragment ...
+uint8_t counter;
+//... later ...
+for (counter=0;counter<=15;counter++) {
+   digitalWrite(counter+5,HIGH);
+   delay(1000);
+}
+

The for loop in C is a litte more flexible than the basic for .. next as we have already seen.

+

Its basic form is

+
for( initialexpression; testexpression; iterateexpression)
+{
+   /*stuff to do*/;
+}
+

Where initialexpression is executed before the loop, then testexpression is evaluated to see if it is true, if if the stuff to do is done followed by iterateexpression .
+Note: that any or all of these expressions can be omitted and that if the test expression is omitted then it evaluates to true.

+

Comments (p81)

+

Comments in C come in the traditional form /* comment */ and the newer C++ style // comment

+
/*
+ * 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" */
+

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.

+
serial.println("Hello World");
+Serial.println("start of routine");
+Serial.print("fare = ");
+Serial.println(fare, DEC);
+

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).

+
#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__);
+

Then you can write

+
serial.println("Hello World");
+DEBUG("start of routine");
+DEBUG("fare = %d", fare);
+ +\endsection +\end{document} -- cgit v1.2.3