Arduino programming is fundamentally similar to C programming, as the Arduino IDE uses a subset of C/C++ for writing sketches. This relationship makes it easier for those familiar with C to pick up Arduino programming and vice versa.
2.1.1 Structure
The basic function of the Arduino programming language is fairly simple and runs in at least two parts. These two required parts, or functions, enclose blocks of statements.
void setup( ) {
//code goes here
}
void loop( ) {
//code goes here
}
setup( ): A function present in every Arduino sketch. Run once before the loop() function. The setup() function should follow the declaration of any variables at the very beginning of the program. It is the first function to run in the program, is run only once, and is used to set pinMode or initialize serial communication.
loop( ): A function present in every single Arduino sketch. This code happens over and over again—reading inputs, triggering outputs, etc. The loop() is where (almost) everything happens and where the bulk of the work is performed.
2.1.2 Digital I/O Functions
Digital I/O will allow us to read the state of an input pin as well as produce a logical high or low at an output pin. If every potential external connection between a microcontroller and the outside world had a dedicated wire, the pin count for controller packages would be high. The ATmega 328P in the Romeo board has four 8-bit ports plus connections for power, ground and the like, yet it only has 28 physical pins. In general, each bit of a port can be programmed independently; some for input, some for output, or all of them for the same purpose.
- pinMode(pin, mode)
Before we use a port, we need to inform the controller about how it should operate. In the Arduino system, this is usually done via a call to the library function pinMode( ). Here is a description of the function from online references:
pinMode(pin,mode)
Parameters
pin: the number of the pin whose mode you
wish to set
mode: INPUT, OUTPUT, or INPUT_PULLUP
Returns
None
It should be noted that the pin could be a number or variable with a value ranging from 0 to 13 or A0 to A5 (when using the Analog Input pins for digital I/O) corresponding to the pin number printed on the interface board. Furthermore, Digital pins default as input, so you really only need to set them to OUTPUT in pinMode().
- digitalWrite(pin, value)
Once a pin is established as an OUTPUT, it is then possible to turn that pin on or off using the digitalWrite() function. Its syntax is as follows:
digitalWrite(pin,value)
Parameters
Pin: the number of the pin you want to write
value: HIGH or LOW
Returns
None
- digitalRead(pin)
With a digital pin configured as an INPUT, we can read the state of that pin using the digitalRead() function. Its syntax is as follows:
digitalRead(pin)
Parameters
pin: the number of the pin you want to read (int)
Returns
HIGH or LOW
The pin can be specified as either a variable or constant (0–13) and the result is either HIGH or LOW
2.1.3 Analog I/O Functions
- analogReference(type)
The Arduino interface board, however, has a convenient pin called AREF located near digital pin 13 along with a function called analogReference() to provide the Arduino’s ADC a reference voltage other than +5 V. This function will effectively increase the resolution available to analog inputs that operate at some other range of lower voltages below +5 V. The syntax for this function is as follows
analogReference(type)
Parameters
type: which type of reference to use (DEFAULT,
INTERNAL, INTERNAL1V1, INTERNAL2V56, or EXTERNAL)
Returns
None
DEFAULT: the default analog reference of 5 volts (on 5 V Arduino boards) or 3.3 volts (on 3.3 V Arduino boards)
INTERNAL: a built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)
INTERNAL1V1: a built-in 1.1 V reference (Arduino Mega only)
INTERNAL2V56: a built-in 2.56 V reference (Arduino Mega only)
EXTERNAL: the voltage applied to the AREF pin (0–5 V only) is used as the reference.
The function is only called once in a sketch, but must be declared before analogRead() is used for the first time, making it a suitable candidate for placing in the setup() function. The type specified relates to the kind of reference voltage that we want to use.
- analogRead(pin)
Reads the value from a specified analog pin with a 10-bit resolution. This function works with the above analogy only for pins (0–5). The analogRead() command will return a number including or between 0 and 1023.
analogRead(pin)
Parameters
pin: the number of the analog input pin to read from(0–5)
Returns
int(0 to 1023)
It takes about 100 micro-second to read an analog input, so the maximum reading rate is about 10,000 times a second. Furthermore, analogy pins unlike digital ones, do not need to be first declared as INPUT nor OUTPUT.
- analogWrite(pin,value)
The Arduino also has the capability to output a Digital signal that acts as an Analog signal, this signal is called pulse width modulation (PWM). Digital Pins # 3, # 5, # 6, # 9, # 10, and # 11 have PWM capabilities. To output a PWM signal use the command: analogWrite().
analogWrite(pin,value)
Parameters
pin: the number of the pin you want to write
value: the duty cycle between 0 (always off, 0%) and
255 (always on, 100%)
Returns
None
2.1.4 Timer Functions
- delay(ms)
Pauses the program for the amount of time (in milliseconds) specified as the parameter. The syntax for the function is as follows.
delay(ms)
Parameters
ms: the number of milliseconds to pause (unsigned long)
Returns
None
Time is specified in milliseconds, where a delay of 1000 ms equals 1s, 2000 ms equals 2s, and so on. This value can be expressed as a constant or variable in the unsigned long data type.
The use of delay() in a sketch has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulations can function during the delay function, so in effect, it brings most other activities to a halt.
- delayMicroseconds(us)
Rather than a long delay, the delayMicroseconds() function is used to delay for a much shorter time. The syntax is as follows:
delayMicroseconds(us)
Parameters
us: the number of microseconds to pause (unsigned int)
Returns
None
Unlike delay(), time here is specified in microseconds, or millionths of a second, where a time period of 1000 ls would equal 1 ms or 0.001 of a second, 10,000 would equal 10 ms or 0.01 of a second, and so on.
- millis()
Inside the microcontroller on the Arduino board there are three onboard hardware timers that work in the background to handle repetitive tasks like incrementing counters or keeping track of program operations. Each of these timers is already being used in some capacity, usually for handling hardware PWM and system timing. The millis() function makes use of one of these hardware timers to maintain a running counter of how many milliseconds the microcontroller has been running since the last time it was turned on or reset. Because this function uses a hardware timer, it performs i.e. counting in the background with no impact on the flow or resources of our source code.
millis()
Parameters
None
Returns
Number of milliseconds since the program started
(unsigned long)