Chapter 5 Programming

physical-computingErrata for Chapter 5: Programming

<-back to errata for Physical Computing.

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  current arduino core (1.0.3)  uses a “for” loop.

for (;;) {
  if (serialEventRun) serialEventRun();

This could also be written as

  if (serialEventRun) serialEventRun();

If Statements (p67)

if (in6 == 1) {

Please note the == to indicate testing for equality. The statement

if (in6 = 1)

would set the value of  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 c variables must be declared with a type. They can be initialized and declared at the same time. In straight c they must be declared at the top of a function or, if global, outside of the function. In cpp which the arduino used they can be inserted as needed.

int Date=12;
int ticketValue = 250;
int Fare=125

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.

uint8_t Sensor; // an 8 bit unsigned variable
uint8_t Ticket
uint16_t Bigger; // this is a 16 bit unsigned variable

And the code on page71 could be written.

uint8_t ticketsSubmitted=0;
uint8_t ticketSensor=0;

loop {
  if (ticketSensor==1) {
  if (ticketSubmitted ==3) {

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.

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;


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) {
   if (theySneezeOnYou) {
   if (theySneezeOnYou) {

void myThankYouRoutine() {
digitalWrite(THANK_YOU_LIGHT, HIGH);
   digitalWrite(THANK_YOU_LIGHT, LOW);

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)
	for (;;)
	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) {

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.


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

//... fragment ...
uint8_t counter;
//... later ...
for (counter=0;counter<=15;counter++) {

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
  } // 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);
#define DEBUG(...) SerialPrintFormatted(__VA_ARGS__);

Then you can write

serial.println("Hello World");
DEBUG("start of routine");
DEBUG("fare = %d", fare);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.