Arduino语言参考
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=36990266&auto=1&height=66"></iframe>
Arduino编程语言可以被分为三个主要部分:函数,值(变量和常量),结构。
函数
用来控制主板和进行数值计算。
Digital I/O (数字I/O函数)
digitalRead() 读取数字引脚值
digitalWrite() 向数字引脚写入值
pinMode() 设置引脚状态(工作在输入还是输出)
Analog I/O (模拟I/O函数)
analogRead() 读取模拟引脚值
analogReference()
analogWrite() 向模拟引脚写入值
Zero, Due & MKR Family
analogReadResolution()
analogWriteResolution()
Advanced I/O (高级I/O函数)
noTone()
pulseIn()
pulseInLong()
shiftIn()
shiftOut()
tone()
Time (时间函数)
delay()
delayMicroseconds()
micros()
millis()
Math (数学函数)
abs()
constrain()
map()
max()
min()
pow()
sq()
sqrt()
Trigonometry (三角函数)
cos()
sin()
tan()
Characters(字符函数)
isAlpha()
isAlphaNumeric()
isAscii()
isControl()
isDigit()
isGraph()
isHexadecimalDigit()
isLowerCase()
isPrintable()
isPunct()
isSpace()
isUpperCase()
isWhitespace()
Random Numbers
random()
randomSeed()
Bits and Bytes(位与比特操作函数)
bit()
bitClear()
bitRead()
bitSet()
bitWrite()
highByte()
lowByte()
External Interrupts(中断函数)
attachInterrupt()
detachInterrupt()
Interrupts(中断函数)
interrupts()
noInterrupts()
Communication(通信)
Serial
Stream
USB
Keyboard
Mouse
VARIABLES(值)
Arduino 数据类型与常量
Constants(常量)
Floating Point Constants
Integer Constants
HIGH | LOW
INPUT | OUTPUT | INPUT_PULLUP
LED_BUILTIN
true | false
Conversion(值转换常数)
(unsigned int)
(unsigned long)
byte()
char()
float()
int()
long()
word()
Data Types
String()
array
bool
boolean
byte
char
double
float
int
long
short
size_t
string
unsigned char
unsigned int
unsigned long
void
word
Variable Scope & Qualifiers
const
scope
static
volatile
Utilities
PROGMEM
sizeof()
STRUCTURE
The elements of Arduino (C++) code.
Sketch
loop()
setup()
Control Structure
break
continue
do...while
else
for
goto
if
return
switch...case
while
Further Syntax
#define (define) #include (include) /* */ (block comment) // (single line comment) ; (semicolon) {} (curly braces) Arithmetic Operators % (remainder) * (multiplication) + (addition) - (subtraction) / (division) = (assignment operator) Comparison Operators != (not equal to) < (less than) <= (less than or equal to) == (equal to) > (greater than) > = (greater than or equal to) Boolean Operators ! (logical not) && (logical and) || (logical or) Pointer Access Operators & (reference operator) * (dereference operator) Bitwise Operators & (bitwise and) << (bitshift left) (bitshift right) ^ (bitwise xor) | (bitwise or) ~ (bitwise not) Compound Operators %= (compound remainder) &= (compound bitwise and) *= (compound multiplication) ++ (increment) += (compound addition) -- (decrement) -= (compound subtraction) /= (compound division) ^= (compound bitwise xor) |= (compound bitwise or)
Arduino uno
通用性最好,使用范围最广的开发板,大小适中,集合可以部署在任何的环境。拥有大量的扩展模块,如wifi,NFC,I/O扩展模块板,蓝牙。它有13个数字接口,6个模拟接口(A0-A5),1个5V,一个3.3V电源引脚。
模拟端口和数字端口
arduino uno 开发板的端口在它的两侧。它们作一种连接来实现开发板与传感器、执行器以及其他设备的交互。主板的端口支持数字和模拟两种信号。端口都是双向性的,可以通过编程进行定义。
模拟信息
随着时间持续变化的信号,是随时间变化的电压。Arduino uno接口使用了0到1023来描述一个模拟输入信号。某些被标记PWM或者~的端口可以创建一个0-255之间变化的输出信号。
Analog I/O (模拟I/O函数)
analogRead() 读取模拟引脚值
analogReference()
analogWrite() 向模拟引脚写入值
例子:
void setup() { pinMode(lcdlight,OUTPUT); analogWrite(lcdlight, 130);//用于调节对比度(0-255),替代电位器作用 } ### 数字信息 数字信号只包含两个值0-1。从3.5-5V之间电压被看做是1,从0-2.5V之间的电压被称为0。 **Digital I/O (数字I/O函数)** digitalRead() 读取数字引脚值 digitalWrite() 向数字引脚写入值 pinMode() 设置引脚状态(工作在输入还是输出) ## Python编程 ### PySerial 1. 下载pyserial.exe 2. 双击安装pyserial Python代码,从串口接收消息 ```python from time import sleep import serial port = Serial.Serial(‘/dev/...’,9600) while True: print port.readline() sleep(0.2) 执行python SerialPython.py