What is a Programming Language?
A programming language is a computer language used by programmers (developers) for the implementation of algorithms. It is a collection of specific instructions written in any specific language ( C, C++, Java, Python) to execute a specific task.
The structure of a programme
int LED_BUILTIN=13;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
The code consists of three parts:
Reporting of variables (optional);
The input/output initialization and configuration part: the setup () function;
The main part that runs in a loop is the loop function ().
The code is structured by strict punctuation:
Any line of code ends with a semi-colon “;”
The content of a function is bounded by “{ ” and “} braces;
The parameters of a function are not contained in parentheses «( » and «) ».
In a line of code, anything after “//” will be a comment;
Comments can be framed on several lines between “/*” and “*/”.
Basic functionalities of Arduino
The setup()
function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup()
function will only run once, after each powerup or reset of the Arduino board.
After creating a setup()
function, which initializes and sets the initial values, the loop()
function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.
Function syntax: attachInterrupt(interrupt, routine, mode)
Interruption: interruption number (0 (pin2) or 1 (pin3))
Routine: function to call once the interruption occurs. This function does not
must not take any parameters and does not return anything.
Mode: LOW, CHANGE, RISING, or FALLING.
• LOW: When the pin is 0 (0V).
• RISING: When the pin changes from status 0 (0V) to status 1 (5V) (rising front).
• FALLING: When the pin changes from state 1 (5V) to state 0 (0V) (down front).
• EXCHANGE: When the pin changes state (front up and front down).
The detachInterrupt() function: Disables an interruption.
The function noInterrupts(): disables interruptions. Interruptions are enabled by default.
The interrupts(): function is used to reactivate interrupts.
Serial communication
Used for communication between the Arduino board and a computer or other devices.
All Arduino boards have at least one serial port. It communicates on digital pins 0
(Rx) and 1 (TX) and with the computer via USB.
Serial.begin(): configuration of the data transmission speed. A speed is expressed in bits per second. The most common speeds are 9600, 19200 and 115200 bits per second.
Serial.println(): Transmits the data to the serial port as ASCII readable text followed by a line return at the end of the sent message.
Serial.available(): Get the number of bytes available for serial port playback.
This is data that has already arrived and has been stored in the serial receive buffer (which holds 64 bytes).
Serial.read(): reads the received serial data.
Analog/Digital Conversion
The Arduino A/D converter has a 10-bit resolution, meaning that each actual analog value between 0 and 5V will be represented by an integer number between 0 and 1023.
The analogRead(pin) function:
pin: the number of the analog input.
The returned value will be the result of digital analog conversion (between 0 and 1023)
Pulse Width Modulation (PWM) Power Control
The PWM principle is based on the Duty Cycle, which refers to the fact that Logical Level 1 may not last the same time as Logical Level 0. The cyclical ratio is measured as a percentage (%). The higher the percentage the higher the logical level 1 is present in the period.
The analogWrite(pin, value) function: writes a value as a PWM on one of the output pins.
Engine control
Servomotor control by Arduino: The Servo. h library allows an Arduino board to control servomotors. Standard models allow the shaft to be positioned at various angles, usually between 0 and 180 degrees.
The attach() function: combines a "servomotor" object with an Arduino spindle.
The function write(): controls the servomotor shaft to rotate to the given angle in parameter (0°- 180°).
The read() function: returns the value of the motor shaft angle (0°-180°).
The detach(): function disassociates a servo from a pin.