learn-arduino-with-me-02

Arduino的程序由三个主要部分构成:

  • 结构
  • 值(变量与常量)
  • 函数

Structure

  • setup()
  • loop()

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 powerup or reset of the Arduino board.

1
2
3
4
5
void ()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const int buttonPin = 3;
void ()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
// loop checks the button pin each time,
// and will send serial if it is pressed
void loop()
{
if (digitalRead(buttonPin) == HIGH)
Serial.write('H');
else
Serial.write('L');
delay(1000);
}

Variables

Constants

  • HIGH | LOW
  • INPUT | OUTPUT | INPUT_PULLUP
  • LED_BUILTIN
  • true | false
  • integer constants
  • floating point constants

Data Types

  • void
  • boolean
  • char
  • unsigned char
  • byte
  • int
  • unsigned int
  • word
  • long
  • unsigned long
  • short
  • float
  • double
  • string - char array
  • String - object
  • array

Conversion

  • char()
  • byte()
  • int()
  • word()
  • long()
  • float()

Variable Scope & Qualifiers

  • variable scope
  • static
  • volatile
  • const

Utilities

  • sizeof()
  • PROGMEM