Post by curious48 on Jul 28, 2016 2:34:00 GMT
Some people on this forum are new to Arduino despite being expert programmers.
By reading the below, in 35 seconds you will have a complete knowledge of the Arduino programming language specifications. Then scroll to the bottom for how to use it.
- The Arduino Language is a simple macro that creates a loop that a microcontroller runs forever.
- The complete syntax of a valid Arduino program is:
- These two functions must be present. setup() contains the code that the microcontroller runs once upon being powered, the loop() function contains the code that loops tightly forever.
- Arduino-specific libraries are added and a main() function is added as part of compilation, you may not have your own main() function - remember to think of setup() and loop()
- Function prototypes are added, which means you do not need to declare a function before first using it, it can be declared later or elsewhere.
- After the above translations, the Arduino language is C++.
It is simply passed to a C++ compiler to finish compilation.
You have now learned the complete reference for the Arduino Language.
You may find a more detailed breakdown of the steps performed by the compiler here. The arduino libraries that added are instructive and may be read here.
How to use it / background
This is a microcontroller, ATmega328P.
It costs between $3.50 (quantity 1) down to $1.50 or less (in quantity of a few thousand) and there are cheaper and more expensive microchips. It runs at 20 Mhz, with a few kBytes of memory.
When loaded with an Arduino program, whenever powered it will run it forever in a tight loop. It has no 'operating system' per se. The above microcontroller is the heart of the most common Arduino development board, the Uno:
This is the Uno Arduino development board. It costs $24.95 and breaks out the pins to the headers at the top and bottom so it can be wired easily.
For example to read temperature sensors and output a temperature:
Eventually, the chip could be used stand-alone, not attached to an Arduino board.
In the real world, a digital thermometer read-out would use a microchip exactly like this one. Now you know how someone might develop a real-world application. These microchips don't run an operating system and are able to read analog values directly as well as output digital signals. They run all of your electronics, stereo, microwave, wireless mouse, you name it. Anything that isn't running a full operating system like Linux.
Arduino includes a set of libraries for easily interfacing with these.
The default Arduino installation follows the above language specification, and then uses avr-g++ as a compiler to translate the C++ into code that can run on the microncontroller.
The Arduinio IDE is also extremely helpful in bringing to your attention the limitations in use. For example:
Notice the extreme limitations on memory! Most people use the Arduino IDE, but others can be used by including the Arduino libraries. In other words, the language is standard C++.
The Arduino IDE shown above is used to compile the program and upload it to an Arduino board you have selected. Programs written in the Arduino language are frequently called sketches.
Practical consequences
In practical terms the basic Arduino program structure is not event-driven. The loop() should poll heavily for any event it needs, blocking completely. There is no way to add scheduled events except with a library, or keeping track of elapsed milliseconds (the usual approach). All functions are blocking.
Notes from the automatically included Arduino.h library:
- boolean and byte are typedefs for bool and unsigned char and most people use them.
- due to extreme resource constraints, 2 KB of RAM is not untypical, most people code in a C style. Be wary of String objects, you can quickly run out of memory: prefer C-style character arrays.
In terms of general programming approach, most programmers avoid new and delete. Remember, it is simply a microcontroller with a tiny amount of memory, that will turn on and then run your code in a tight loop for days, weeks, or years. By following the above practices and letting C++ destroy local variables once they are out of scope, you will not encounter memory errors.
The rest of the arduino.h header file contains other functions commonly used. From a language specification perspective it is more precise than the official documentation.
By reading the below, in 35 seconds you will have a complete knowledge of the Arduino programming language specifications. Then scroll to the bottom for how to use it.
ARDUINO COMPLETE LANGUAGE SPECIFICATIONS
- The Arduino Language is a simple macro that creates a loop that a microcontroller runs forever.
- The complete syntax of a valid Arduino program is:
void setup() {
}
void loop() {
}
- These two functions must be present. setup() contains the code that the microcontroller runs once upon being powered, the loop() function contains the code that loops tightly forever.
- Arduino-specific libraries are added and a main() function is added as part of compilation, you may not have your own main() function - remember to think of setup() and loop()
- Function prototypes are added, which means you do not need to declare a function before first using it, it can be declared later or elsewhere.
- After the above translations, the Arduino language is C++.
It is simply passed to a C++ compiler to finish compilation.
You have now learned the complete reference for the Arduino Language.
You may find a more detailed breakdown of the steps performed by the compiler here. The arduino libraries that added are instructive and may be read here.
How to use it / background
This is a microcontroller, ATmega328P.
It costs between $3.50 (quantity 1) down to $1.50 or less (in quantity of a few thousand) and there are cheaper and more expensive microchips. It runs at 20 Mhz, with a few kBytes of memory.
When loaded with an Arduino program, whenever powered it will run it forever in a tight loop. It has no 'operating system' per se. The above microcontroller is the heart of the most common Arduino development board, the Uno:
This is the Uno Arduino development board. It costs $24.95 and breaks out the pins to the headers at the top and bottom so it can be wired easily.
For example to read temperature sensors and output a temperature:
Eventually, the chip could be used stand-alone, not attached to an Arduino board.
In the real world, a digital thermometer read-out would use a microchip exactly like this one. Now you know how someone might develop a real-world application. These microchips don't run an operating system and are able to read analog values directly as well as output digital signals. They run all of your electronics, stereo, microwave, wireless mouse, you name it. Anything that isn't running a full operating system like Linux.
Arduino includes a set of libraries for easily interfacing with these.
The default Arduino installation follows the above language specification, and then uses avr-g++ as a compiler to translate the C++ into code that can run on the microncontroller.
The Arduinio IDE is also extremely helpful in bringing to your attention the limitations in use. For example:
Notice the extreme limitations on memory! Most people use the Arduino IDE, but others can be used by including the Arduino libraries. In other words, the language is standard C++.
The Arduino IDE shown above is used to compile the program and upload it to an Arduino board you have selected. Programs written in the Arduino language are frequently called sketches.
Practical consequences
In practical terms the basic Arduino program structure is not event-driven. The loop() should poll heavily for any event it needs, blocking completely. There is no way to add scheduled events except with a library, or keeping track of elapsed milliseconds (the usual approach). All functions are blocking.
Notes from the automatically included Arduino.h library:
- boolean and byte are typedefs for bool and unsigned char and most people use them.
- due to extreme resource constraints, 2 KB of RAM is not untypical, most people code in a C style. Be wary of String objects, you can quickly run out of memory: prefer C-style character arrays.
In terms of general programming approach, most programmers avoid new and delete. Remember, it is simply a microcontroller with a tiny amount of memory, that will turn on and then run your code in a tight loop for days, weeks, or years. By following the above practices and letting C++ destroy local variables once they are out of scope, you will not encounter memory errors.
The rest of the arduino.h header file contains other functions commonly used. From a language specification perspective it is more precise than the official documentation.