The code in this book takes advantage of a number of Arduino functions that are summarized in this appendix. See the online Arduino reference for each function if you want more detail.
pinMode(pin, mode);
Configures a digital pin to read (input) or write (output) a digital value; see http://arduino.cc/en/Reference/PinMode
digitalRead(pin);
Reads a digital value (HIGH or LOW) on a pin set for input; see http://arduino.cc/en/Reference/DigitalRead
digitalWrite(pin, value);
Writes the digital value (HIGH or LOW) to a pin set for output; see http://arduino.cc/en/Reference/DigitalWrite
pulseIn(pin, pulseType, timeout);
Returns the pulse width in microseconds of a changing digital signal on the given
pin. pulseType
(either HIGH or LOW) determines if duration is for a
high or low pulse. timout
is an optional value indicating how long
to wait for a pulse (the default is one second); see http://arduino.cc/en/Reference/PulseIn
analogRead(pin);
Reads a value from the specified analog pin. The value ranges from 0 to 1023 for voltages that range from 0 to the reference voltage (5 volts by default, but can be changed by using analogReference; see http://arduino.cc/en/Reference/AnalogRead
analogReference(type);
Configures the reference voltage used for analog input. This is used in the battery monitor code discussed in Appendix D; see http://arduino.cc/en/Reference/AnalogReference
min(x,y);
Returns the smaller of two numbers; see http://arduino.cc/en/Reference/Min
max(x,y);
Returns the larger of two numbers; see http://arduino.cc/en/Reference/Max
constrain(x,lower,upper);
Constrains the value of x to be between the lower and upper range; see http://arduino.cc/en/Reference/Constrain
map(x,fromLow,fromHigh,destLow,destHigh);
Scales a value from one range to another range. The result will have the same proportion within the destination range as in the source range. The following code scales the analogRead value to a percentage of the full scale reading:
int val = analogRead(0); int percent = map(val, 0,1023, 0,100)
The following code scales an analogRead
value to its value in
millivolts (refMv
is the reference voltage expressed in
millivolts):
int mV = map(val, 0,1023, 0, refMv);
switch / case statements
Controls program flow by testing if a number matches one of a number of
alternative values. Here is a simplified example from the remote control sketch that
uses switch
to execute the appropriate function associated with
each command:
void processCommand(int command) { switch(command) { case MOVE_LEFT : moveLeft(); break; case MOVE_RIGHT : moveRight(); break; case MOVE_FORWARD : moveForward(); break; case MOVE_BACK : moveBackward(); break; case PIVOT_CCW : moveRotate(-90); break; case PIVOT_CW : moveRotate(90); break; case HALT : moveStop(); break; } }
The
break
statement is necessary to prevent execution falling through
to the following case statement. See http://arduino.cc/en/Reference/SwitchCase
array
An array is a collection of variables accessed using an index number. The first
element of an Arduino array is accessed using an index of 0. An array can be
initialized when it is declared by placing values in curly brackets. The following
declares an array named motorSpeed
with two elements that will
store the speed for the left and right motors and initialize the speed values to
0:
const int NUMBER_OF_MOTORS = 2; int motorSpeed[NUMBER_OF_MOTORS] = {0,0}; // motor speed stored here (0-100%)
#include "header.h"
This makes functions and variables declared in the specified file available to your sketch. See http://arduino.cc/en/Reference/Include
3.145.170.83