Difference between revisions of "Arduino C Grammar"
(Created page with "Arduino grammar is built on the basis of C/C + +, in fact is also the basic C grammar, Arduino grammar not only put some related parameters Settings are function change, we ha...") |
|||
Line 2: | Line 2: | ||
=== Control Structures === | === Control Structures === | ||
− | If | + | |
+ | If | ||
+ | |||
if...else | if...else | ||
+ | |||
for | for | ||
+ | |||
switch case | switch case | ||
+ | |||
while | while | ||
+ | |||
do... while | do... while | ||
+ | |||
break | break | ||
+ | |||
continue | continue | ||
+ | |||
return | return | ||
+ | |||
goto | goto | ||
=== Further Syntax === | === Further Syntax === | ||
+ | |||
; | ; | ||
+ | |||
{} | {} | ||
+ | |||
// | // | ||
+ | |||
/* */ | /* */ | ||
=== Operators === | === Operators === | ||
+ | |||
++ | ++ | ||
+ | |||
-- | -- | ||
+ | |||
+= | += | ||
+ | |||
-= | -= | ||
+ | |||
*= | *= | ||
+ | |||
/= | /= | ||
+ | |||
= | = | ||
+ | |||
+ | + | ||
+ | |||
- | - | ||
+ | |||
* | * | ||
+ | |||
/ | / | ||
+ | |||
% | % | ||
+ | |||
== | == | ||
+ | |||
!= | != | ||
+ | |||
< | < | ||
+ | |||
> | > | ||
+ | |||
<= | <= | ||
+ | |||
>= | >= | ||
+ | |||
&& | && | ||
+ | |||
|| | || | ||
+ | |||
! | ! | ||
=== Data type === | === Data type === | ||
− | + | ||
− | char | + | ==== boolean ==== |
+ | |||
+ | char | ||
+ | |||
byte | byte | ||
+ | |||
int | int | ||
+ | |||
unsigned int | unsigned int | ||
+ | |||
long | long | ||
+ | |||
unsigned long | unsigned long | ||
+ | |||
float | float | ||
+ | |||
double | double | ||
+ | |||
string | string | ||
+ | |||
array | array | ||
+ | |||
void | void | ||
− | === Constant === | + | ==== Constant ==== |
+ | |||
HIGH | LOW Said digital IO port level, HIGH Said high level(1), LOW Said low electric flat(0). | HIGH | LOW Said digital IO port level, HIGH Said high level(1), LOW Said low electric flat(0). | ||
− | INPUT | OUTPUT Said digital IO port direction, INPUT Said input (high impedance state) | + | |
+ | INPUT | OUTPUT Said digital IO port direction, INPUT Said input (high impedance state) | ||
+ | |||
OUTPUT Said output (AVR can provide 5 v voltage and ma current). | OUTPUT Said output (AVR can provide 5 v voltage and ma current). | ||
+ | |||
TURE | FALSE true(1) , false(0). | TURE | FALSE true(1) , false(0). | ||
All above are the basic c grammar words and symbols, everybody can understand, and the specific use can combine experimental procedure. | All above are the basic c grammar words and symbols, everybody can understand, and the specific use can combine experimental procedure. | ||
− | === Structure === | + | ==== Structure ==== |
− | + | ||
+ | *void setup() | ||
+ | |||
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 power up or reset of the Arduino board. | 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 power up or reset of the Arduino board. | ||
− | + | ||
+ | *void loop() | ||
+ | |||
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. | 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 ==== | ==== Function ==== | ||
− | + | ||
+ | *Digital I/O | ||
+ | |||
pinMode(pin, mode) pin 0~13, mode is input or output. | pinMode(pin, mode) pin 0~13, mode is input or output. | ||
+ | |||
digitalWrite(pin, value) pin 0~13, value is HIGH or LOW. | digitalWrite(pin, value) pin 0~13, value is HIGH or LOW. | ||
+ | |||
int digitalRead(pin) pin 0~13, value is HIGH or LOW. | int digitalRead(pin) pin 0~13, value is HIGH or LOW. | ||
− | + | ||
+ | *Analog I/O | ||
+ | |||
int analogRead(pin) pin 0~5. | int analogRead(pin) pin 0~5. | ||
+ | |||
analogWrite(pin, value) pin 3, 5, 6, 9, 10, 11, value is 0 to 255 | analogWrite(pin, value) pin 3, 5, 6, 9, 10, 11, value is 0 to 255 | ||
==== Time ==== | ==== Time ==== | ||
+ | |||
delay(ms)Pauses the program for the amount of time (in miliseconds) specified as parameter. (There are 1000 milliseconds in a second.)(unit ms). | delay(ms)Pauses the program for the amount of time (in miliseconds) specified as parameter. (There are 1000 milliseconds in a second.)(unit ms). | ||
+ | |||
delayMicroseconds(us) | delayMicroseconds(us) | ||
− | ==== Math ==== | + | ==== Math ==== |
+ | |||
min(x, y) minimum value | min(x, y) minimum value | ||
+ | |||
max(x, y) maximum value | max(x, y) maximum value | ||
+ | |||
abs(x) absolute value | abs(x) absolute value | ||
+ | |||
constrain(x, a, b) Constraint function, lower limit a upper limit b, x must be between a & b to be returned | constrain(x, a, b) Constraint function, lower limit a upper limit b, x must be between a & b to be returned | ||
− | map(value, fromLow, fromHigh, toLow, toHigh) | + | |
+ | map(value, fromLow, fromHigh, toLow, toHigh) | ||
+ | |||
pow(base, exponent) extraction of square root | pow(base, exponent) extraction of square root | ||
+ | |||
sq(x) square | sq(x) square | ||
+ | |||
sqrt(x) Square root | sqrt(x) Square root |
Latest revision as of 06:58, 14 June 2013
Arduino grammar is built on the basis of C/C + +, in fact is also the basic C grammar, Arduino grammar not only put some related parameters Settings are function change, we have no need to understand his bottom, let us to know AVR micro control unit (MCU) friend can also easy to fit in. So here I'll simple comment the Arduino grammar.
Contents
Control Structures
If
if...else
for
switch case
while
do... while
break
continue
return
goto
Further Syntax
;
{}
//
/* */
Operators
++
--
+=
-=
*=
/=
=
+
-
*
/
%
==
!=
<
>
<=
>=
&&
||
!
Data type
boolean
char
byte
int
unsigned int
long
unsigned long
float
double
string
array
void
Constant
HIGH | LOW Said digital IO port level, HIGH Said high level(1), LOW Said low electric flat(0).
INPUT | OUTPUT Said digital IO port direction, INPUT Said input (high impedance state) OUTPUT Said output (AVR can provide 5 v voltage and ma current).
TURE | FALSE true(1) , false(0).
All above are the basic c grammar words and symbols, everybody can understand, and the specific use can combine experimental procedure.
Structure
- void setup()
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 power up or reset of the Arduino board.
- void loop()
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
- Digital I/O
pinMode(pin, mode) pin 0~13, mode is input or output.
digitalWrite(pin, value) pin 0~13, value is HIGH or LOW.
int digitalRead(pin) pin 0~13, value is HIGH or LOW.
- Analog I/O
int analogRead(pin) pin 0~5.
analogWrite(pin, value) pin 3, 5, 6, 9, 10, 11, value is 0 to 255
Time
delay(ms)Pauses the program for the amount of time (in miliseconds) specified as parameter. (There are 1000 milliseconds in a second.)(unit ms).
delayMicroseconds(us)
Math
min(x, y) minimum value
max(x, y) maximum value
abs(x) absolute value
constrain(x, a, b) Constraint function, lower limit a upper limit b, x must be between a & b to be returned
map(value, fromLow, fromHigh, toLow, toHigh)
pow(base, exponent) extraction of square root
sq(x) square
sqrt(x) Square root