Basic C language
Keyword:
if…else
A problem representation (expression) must be followed, and if the expression is true, the code after the representation is executed. If this expression is false, it executes the following code . It is allowed to use only if without else.
Example:
if (val == 1) {
digitalWrite(LED,HIGH);
}
for
Determine the code in a section of the area, and repeat the number of times.
Example:
for (int i = 0; i < 10; i++) {
Serial.print(“ciao”);
}
switch case
“Switch case” is the intersection of more options. According to the value of the variable,. “Swith case” makes the program have more choices. The use of “swith case” makes the program code look simple.
Example:
switch (sensorValue) {
case 23:
digitalWrite(13,HIGH);
break;
case 46:
digitalWrite(12,HIGH);
break;
default: // When all the above conditions do not match, the presupposition action is performed.
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}
while
When the condition is followed by “while”, the code in the parentheses is executed. Example:
// When the sensor value is less than 512, the LED light will flicker.
sensorValue = analogRead(1);
while (sensorValue < 512) {
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,HIGH);
delay(100);
sensorValue = analogRead(1);
}
do… while
Similar to while, the program code before “while” is executed first, whether it is true or false. So if there is a program code that needs to be executed at least once, you can use “do…” While “architecture.
Example:
do {
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,HIGH);
delay(100);
sensorValue = analogRead(1);
} while (sensorValue < 512);
break
Let the program code jump off the loop and continue to execute the program code after the loop. In addition, the “break” is also used to separate the “switch case” for different descriptions.
Example:
// When the sensor value is less than 512, the LED light will flicker
do {
// Press the button to leave the cycle
if (digitalRead(7) == HIGH)
break;
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,HIGH);
delay(100);
sensorValue = analogRead(1);
} while (sensorValue < 512);
continue
In a loop, it can force off the next program and execute the next cycle directly.
Example:
for (light = 0; light < 255; light++)
{
// Neglecting the values between 140 and 200
if ((x > 140) && (x < 200))
continue;
analogWrite(PWMpin, light);
delay(10);
}
return
The end of the function can be returned to a value through “return”.
For example, a function that calculates the current temperature is called “computeTemperature ()”. If you want to return the temperature to the temperature variable, you can write as follows.
int temperature = computeTemperature();
int computeTemperature() {
int temperature = 0;
temperature = (analogRead(0) + 45) / 100;
return temperature;
}
goto
Grammatical symbols:
; (semicolon)
Each line in the Arduino language ends with a semicolon. This syntax allows you to arrange the code freely, and you can put the two instructions on the same line, as long as the middle is separated by a semicolon. (but this may reduce the readability of the program.)
Example:
delay(100);
{} (parentheses)
Parentheses are used to divide code into one block after another. As follows, before and after the “loop ()” function, it must be enclosed in braces.
Example:
void loop(){
Serial.pritln(“cial”);
}
The annotation of the program is the explanation and explanation of the code. Writing annotations helps the programmer (or others) understand the function of the code.
Arduino The processor ignores the part of the annotation when compiling the code.
Arduino There are two ways to write annotations in a language
//Single line notes: the whole line of text will be ignored by the processor
/*Multiline annotations:
In this range you can
Wrote a whole poem or article
Operator:
=
+ addition
– subtraction
* multiplying
/ Division
% remainder Division
= = equal
=! not equal to
< less than
> greater than
<= Less than equal to or equal to
>= Greater than equal to or equal to
&& intersection
|| combine
! RP
++ accumulation
— Decline
+=
-=
*=
/=
Data types:
boolean
The values of a boolean variable can only be true (true) or false (false).
char
A single character, such as A, is the same as the usual computer practice that stores a character into a number, even if you see a word clearly.
When a character is represented with a number, its value is valid range from -128 to 127.
Note: there are two kinds of mainstream computer coding systems ASCII and UNICODE. The ASCII represents 127 characters for transferring text between a sequence terminal and a time – sharing calculator.
UNICODE can be represented by a lot of characters, which can be used to represent multi – country languages in modern computer operating systems.
byte
The storage values range from 0 to 255. As a character, the byte type variable needs to be stored in only one byte (8 bit) memory space.
int
Integer data type is used in 2 byte memory space. The integer range can be expressed as — 32768 to 32767; integer variable is the most commonly used data type in Arduino.
unsigned int
A non – number integer uses a 2 byte memory space, which means that it can not store negative values, so a number of integers can be represented as an integer range of 0 to 65535.
long
The memory size used by long integers is two times as large as an integer, so it can represent an integer range from – 2147483648 to 2147483647.
unsigned long
A number of integers that can be represented by a non – long integer is 0 to 4294967295.
float
Floating point numbers are used to express numerical values with decimal points. Each floating point number will use four bytes of RAM, pay attention to the limitation of chip memory space, and use floating-point prudently.
double
It is also called a double precision floating-point number, which can express the maximum value of 1.7976931348623157 x 10308.
string
String is used to express text information, it is composed of multiple ASCII characters. You can send a text message through the serial port, or display it on the LCD. Every character in the string is stored in a group tuple space, and a null character is added at the end of the string to prompt the end of the Ardunio processor string. The following two declaring ways are the same.For example:
char string1[] = “Arduino”;//7 character +1 null character
char string2[8] = “Arduino”; // with the same uplink
array
A string of variables can be obtained directly through the index. If you want to store different degrees of LED brightness, you can declare six variables light01, light02, light03, light04, light05, light06, but you actually have a better choice, for example, declare an integer array variable as follows:
Int light[6] = {0, 20, 50, 75, 100}
The word “array” is not used directly in the variable declaration, but [] and {} declared array.
Control instruction
Data type conversion:
char()
byte()
int()
long()
float()
Constant: in the Arduino language, some reserved words with special purposes are defined beforehand.
HIGH | LOW
The level of the digital IO port is expressed, and the HIGH represents the high level (1), and the LOW represents the low level (0). HIGH and LOW are also used to indicate that you turn on or close a Arduino foot (PIN)
INPUT | OUTPUT
This indicates that the IO port direction, “INPUT” represents the input (high resistance), “OUTPUT” represents the output (AVR can provide 5V voltage 40mA current).
true | false
True is true (1), and false is false (0).
Variable:
Variables are used to specify a location in Arduino memory. Variables can be used to store data. Programmers can go through the script code to limit the number of operations variables.
Because Arduino is a very simple microprocessor, but when you declare a variable, you must first define its data type, so that the microprocessor knows how much space it will prepare to store this variable value.
These are the key words and symbols of the basic C language.