Arduino C Grammar

From SainSmart Wiki
Jump to: navigation, search

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.

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